25.09.2017 Views

cpp_tutorial

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C++<br />

}<br />

};<br />

int main()<br />

{<br />

Time T1(11, 59), T2(10,40);<br />

++T1;<br />

T1.displayTime();<br />

++T1;<br />

T1.displayTime();<br />

// increment T1<br />

// display T1<br />

// increment T1 again<br />

// display T1<br />

}<br />

T2++;<br />

T2.displayTime();<br />

T2++;<br />

T2.displayTime();<br />

return 0;<br />

// increment T2<br />

// display T2<br />

// increment T2 again<br />

// display T2<br />

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

H: 12 M:0<br />

H: 12 M:1<br />

H: 10 M:41<br />

H: 10 M:42<br />

Binary Operators Overloading<br />

The unary operators take two arguments and following are the examples of<br />

Binary operators. You use binary operators very frequently like addition (+)<br />

operator, subtraction (-) operator and division (/) operator.<br />

Following example explains how addition (+) operator can be overloaded.<br />

Similar way, you can overload subtraction (-) and division (/) operators.<br />

#include <br />

using namespace std;<br />

205

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

Saved successfully!

Ooh no, something went wrong!