Binary Operator Overloading In C++

In the case of a unary operator we generally use no arguments. In the case of a binary operator we need to use one argument.
Out put of the program
The above program is incomplete. This is just to illustrate overloading
Let’s take a closer look at the program.
The values of feet and inches for dist1 are got from the user while dist2 is initialised to 11 feet and 6.25 inches.
The complier reads: dist3 = dist1 + dist2;
What does it do? As usual it knows that + is overloaded and so it goes to the definition of the operator function. But we have a problem. + works on two operands, dist1 and dist2.
So we need two values to be passed to the operator function so that it can add these two values.
But the operator function has only one argument. Hence it is clear that only one of the values are passed ( i.e. only one object is passed). S, out of the two (dist1 and dist2) how do we know which one is passed?
And what about the other value?
Answer is simple. The operand to the right of the operator is passed as an argument. Hence the values of feet and inches of dist2 are passed to the operator function. Let’s take a look at the first two lines within the operator function.
int f = feet + d2.feet;
float i = inches + d2.inches; ;
First of all, you know that dist2 is passed as an argument. Hence, d2 actually represents dist2 in our case.
f and i are two variables declared as integer and float respectively.
In the expression : int f = feet + d2.feet; what does the feet stand for?
This feet is the value of dist1’s feet. dist1 is on the left of the operator and it is not passed as argument. Hence feet and inches refer to the values of dist1.
In technical terms: When an overloaded operator is invoked, the object on the left of the operator is the object of which the operator is a member and the object on the right must be provided as argument to the operator.
The rest of the program is just as normal.
Overloading Binary Operator using Friends
As we study earlier, friend function may be used in the place of member functions for overloading a binary operator.
The only difference being that a friend function requires two argument to be explicitly passed to it while a member function requires only one.
Here below given is the program which shows overloading operator using friends:
The program overloads the operator * two times , thus overloading the operator function .operator*()itself.
In both the cases, the function are explicitly passed two arguments and they are invoked like any other overloaded function, based on the types of its arguments. This enable us to both the forms of scalar multiplication such as
p=2*m; //equivalent to p=operator*(2,m);
q=n*2;//equivalent to q=operator*(n,2);
The prog. and its output are largely self-explanatory. The first constructor
Vector( );
constructs a vector whose elements are all zero.
Thus vector m; creates a vector m ands initialize all its element to 0.The second operator-vector(int*x);creates a vector and copies the elements pointed to by the pointer argument x into it.
Therefore, the statements
vector n=x;
creates n as a vector with components 2,4 and 6.
Note that we have used vector variables like m and n in input and output statements just like simple variables. This has been made possible by overloading the operator >> and<< using the functions:
friend istream & operator>>(istream &,vector&);
friend ostream & operator<<(ostream &, vector &);
istream ands ostream are classes defined in the iostream.h file which has been included in the program.
Beware: There are some operator which can’t be overloaded e.g.:- sizeof,.,.*,::,?: etc.
Scroll to Top