Saturday, 2 October 2010

Overloading the Post Increementing operator through member function



Overloading the Post Increementing operator through member function

If d1,d2 are objects of class ‘Distance’  then the following statement
d2= d1++;
is interpretated by the compiler as
d2= d1.operator ++ (0);

Example :
class Distance
{
private:
int feet;
float inches;
public:
Distance operator ++ (int); // prototype for overloading the post increment operator
/*
    rest of the class
*/
};
Distance Distance :: operator ++ (int)
{
return distance (feet++, inches++);
}
void main( )
{
Distance d1,d2;
d2= d1++;
}




 

No comments:

Post a Comment