25.09.2017 Views

cpp_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C++<br />

-D1;<br />

D1.displayDistance();<br />

// apply negation<br />

// display D1<br />

-D2;<br />

D2.displayDistance();<br />

// apply negation<br />

// display D2<br />

}<br />

return 0;<br />

When the above code is compiled and executed, it produces the following result:<br />

F: -11 I:-10<br />

F: 5 I:-11<br />

Hope above example makes your concept clear and you can apply similar<br />

concept to overload Logical Not Operators (!).<br />

Increment (++) and Decrement (- -) Operators<br />

The increment (++) and decrement (--) operators are two important unary<br />

operators available in C++.<br />

Following example explain how increment (++) operator can be overloaded for<br />

prefix as well as postfix usage. Similar way, you can overload operator (--).<br />

#include <br />

using namespace std;<br />

class Time<br />

{<br />

private:<br />

int hours; // 0 to 23<br />

int minutes; // 0 to 59<br />

public:<br />

// required constructors<br />

Time(){<br />

hours = 0;<br />

minutes = 0;<br />

}<br />

203

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!