Operator overloading is a very important aspect of object-oriented programming. Binary operators can be overloaded in a similar manner as unary operators. In this C++ tutorial, you will learn about Binary Operators Overloading, explained along with syntax and example.
Operator Overloading – Binary Operators
Binary operators, when overloaded, are given new functionality. The function defined for binary operator overloading, as with unary operator overloading, can be member function or friend function.
The difference is in the number of arguments used by the function. In the case of binary operator overloading, when the function is a member function then the number of arguments used by the operator member function is one (see below example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments.
Binary operator overloading, as in unary operator overloading, is performed using a keyword operator.
Binary operator overloading example:
#include <iostream>
using namespace std;
class Exforsys
{
private:
int x;
int y;
public:
Exforsys() //Constructor
{ x=0; y=0; }
void getvalue( ) //Member Function for Inputting Values
{
cout << "n Enter value for x: ";
cin >> x;
cout << "n Enter value for y: ";
cin>> y;
}
void displayvalue( ) //Member Function for Outputting Values
{
cout << "value of x is: " << x << "; value of y is: " << y;
}
Exforsys operator +(Exforsys);
};
Exforsys Exforsys :: operator + (Exforsys e2)
//Binary operator overloading for + operator defined
{
Exforsys rez; //declaring an Exforsys object to retain the final values
int x1 = x+ e2.x;
int y1 = y+e2.y;
rez.x=x1;
rez.y=y1;
return rez;
}
void main( )
{
Exforsys e1,e2,e3; //Objects e1, e2, e3 created
cout << "nEnter value for Object e1:";
e1.getvalue( );
cout << "nEnter value for Object e2:";
e2.getvalue( );
e3= e1+ e2; //Binary Overloaded operator used
cout << "nValue of e1 is: "; e1.displayvalue();
cout << "nValue of e2 is: " ; e2.displayvalue();
cout << "nValue of e3 is: "; e3.displayvalue();
}
The output of the above program is:
In the above example, the class Exforsys has created three objects e1, e2, e3. The values are entered for objects e1 and e2. The binary operator overloading for the operator ‘+’ is declared as a member function inside the class Exforsys. The definition is performed outside the class Exforsys by using the scope resolution operator and the keyword operator.
The important aspect is the statement:
e3= e1 + e2;
The binary overloaded operator ‘+’ is used. In this statement, the argument on the left side of the operator ‘+’, e1, is the object of the class Exforsys in which the binary overloaded operator ‘+’ is a member function. The right side of the operator ‘+’ is e2. This is passed as an argument to the operator ‘+’ . Since the object e2 is passed as argument to the operator ‘+’ inside the function defined for binary operator overloading, the values are accessed as e2.x and e2.y. This is added with e1.x and e1.y, which are accessed directly as x and y. The return value is of type class Exforsys as defined by the above example.
There are important things to consider in operator overloading with C++ programming language. Operator overloading adds new functionality to its existing operators. The programmer must add proper comments concerning the new functionality of the overloaded operator. The program will be efficient and readable only if operator overloading is used only when necessary.
- Some operators cannot be overloaded:
- scope resolution operator denoted by ::
- member access operator or the dot operator denoted by .
- the conditional operator denoted by ?:
- and pointer to member operator denoted by .*