Saturday, 2 October 2010

Overloading the Pre decrementing operator by using member function



Overloading the Pre decrementing operator by using member function:

If d1,d2 are objects of class ‘Distance’  then the following statement
d2= --sd1;
is interpretated by the compiler as
d2= d1.operator -- ();
Example :
class Distance
{
private:
int feet;
float inches;
public:
Distance operator -- ();  // prototype for overloading the pre decrement operator
/*
    rest of the class
*/
};

Distance Distance :: operator -- ( )
{
return distance (--feet, --inches);
}

void main( )
{
Distance d1,d2;
d2= --d1;
cout<<d2.feet<< endl <<de.inches;
}


 

No comments:

Post a Comment