Coupon Accepted Successfully!


Operator Return Type

In the last few examples we declared the operator() of void types, that is, it will not return any value. However, it is possible to return value and assign to it other object of the same type. The return value of operator is always of class type, because the operator overloading is only for objects. An operator cannot be overloaded for basic data type. Hence, if the operator returns any value, it will be always of class type. Consider the following program.

10.5 Write a program to return values from operator() function.


Explanation: In the above program class plusplus is declared with one private integer num. The class constructor initializes the object with zero. The member function getnum() returns current value of variable num. The operator ++() is overloaded and it can handle as postfix increment of the objects. In case of an increase in the prefix it will flag an error.

The p1 and p2 are objects of the class plusplus. The statement p1=p2++ first increments the value of p2 and then assigns it to the object p1. The values displayed will be one for the objects. The object p1 is increased. This time, the values of object displayed will be two and one.

Test Your Skills Now!
Take a Quiz now
Reviewer Name