Saturday, 2 October 2010

Overloading the Pre Increementing operator through a member function



Overloading the Pre Increementing operator through a 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 ++ ();

Example :
class Distance
{
private:
int feet;
float inches;
public:
Distance operator ++ ();  // prototype for overloading the pre increment 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