Saturday 2 October 2010

Overloading arithmetic operator ‘ + ‘ using member function



//header files
class Distance
{
private:
int feet;
float  inches;
public:
Distance(int i, float f)        // two argument constructor
{
feet=i;
inches=f;
}
Distance operator +(Distance);     //prototype of operator overloading function for +
};

Distance Distance :: operator + (Distance d2)
{
return Distance (feet+d2.feet , inches+d2.inches);
}

void main()
{
Distance d1(4,5), d2(2,6), d3;
d3=d1+d2;                  // + operator overloaded.
cout<<d3.feet<<d3.inches;
}

Note: From the above class, overloading of operators -, *, / can be easily learnt.

Let us now discuss about some cases related to overloading the arithmetic operator +. These cases are the same even for other operators.
Case 1:
d3=d1+d2;   // d1, d2, d3 are objects of the class Distance.
The compiler will translate the above expression as follows
d3= d1.operator + (d2);
Overloading the arithmetic operator + as a member function of Distance class can handle the above case. The above case can also be handled by implementing the overloading function as a friend function of Distance class.

Case 2:
d3=d1+4;   // d1, d3 are objects of the class Distance.
The compiler will translate the above expression as follows
d3= d1.operator + (4);

An integer value is passed as a parameter to the overloading function. In this situation, the compiler will look for a constructor of Distance class which takes an integer value as parameter. And with the help of such constructor the compiler will try to create an object of distance class, which will be passed as a paramter to the overloading function. If no such constructor is provided the compiler throw an error.
Overloading the arithmetic operator + as a member function of Distance class can handle the above case. The above case can also be handled by implementing the overloading function as a friend function of Distance class. But in either of the cases, there must be a constructor which takes an integer value as a parameter.

Case 3:
d3= 4+d2;   // d2, d3 are objects of the class Distance.
The compiler will translate the above expression as follows
d3= 4.operator + (d2);

Overloading the arithmetic operator + as a member function of Distance class can not handle the above case, because the operator overloading function is not called with respect to an object of the class Distance. The above case can be easily handled by implementing the overloading function as a friend function of Distance class.

When the overloading function is implemented as a friend function, then the compile will translate the above expression as follows.

d3 = operator(4, d2);
It can be handled by friend function.
Case 4:
d1= 4.75 + 3.25;
We may wonder about this case where both operands are of float type values. However in this case the operator overloading mechanism will not be invoked at all. Instead, the float type values will simply get added to each other. And the final statement will look as follows

d1=8.0;

Here, the compiler will look for a constructor which takes a float value as a parameter. And with the help of such a constructor, the compiler will generate the appropriate object. If there is no such constructor available, the compiler will throw an error.

 

No comments:

Post a Comment