Saturday 2 October 2010

Overloading the Post Increementing operator through friend function



Overloading the Post Increementing operator through friend function:

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

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




operator overloading

No comments:

Post a Comment