Saturday 2 October 2010

Overloading Post decrementing operator by using member functions



Overloading Post decrementing operator by using member functions:
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 decrement 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