In a C++ language, operator overloading is one of the important feature of c++.
It is an example of polymorphism. Polymorphism means one function having many forms .
There are different operators we use in a traditional programming language the operator such as +,-,*,/ etc.
For example: +( plus ) operator is used to perform addition of two variables , we cant add two object with “+” operator.
In C++ to perform operation with object we redefine the definitions of various operators.
Operator overloading feature helps to use these operators( +,- etc. ) with the object and object can be used in a natural manner as the variables of basic data types.
Example: Write a program to perform the addition of two object and store the result in third object. Display the content of third object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> using namespace std; class xyz { public: int x, y; xyz() // zero argument constructor {} xyz(int a, int b) // two argument constructor { x = a; y = b; } void show() { cout << " x= " << x << " y = " << y << endl; } }; // end of class int main() { xyz A(2, 3), B(3, 4), C; // create three object & constructor called A.show(); B.show(); C.x = A.x + B.x; C.y = A.y + B.y; C.show(); } |
Output
1 2 3 | x= 2 y = 3 x= 3 y = 4 x= 5 y = 7 |
Example: Write a program to perform addition of two objects using “operator” keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> using namespace std; class xyz { public: int x, y; xyz() // zero argument constructor {} xyz(int a, int b) // two argument constructor { x = a; y = b; } xyz operator + (xyz O1) { xyz O2; O2.x = x + O1.x; O2.y = y + O1.y; return O2; } void show() { cout << " x= " << x << " y = " << y << endl; } }; // end of class int main() { xyz A(2, 3), B(3, 4), C; // create three object & constructor called A.show(); B.show(); C = A + B; C.show(); } |
Output
1 2 3 | x= 2 y = 3 x= 3 y = 4 x= 7 y = 3 |
Description : In the above program, A, B, C are the object of class xyz.
When the statement C= A + B is execute, the compiler searches for definition of operator + .
The object A call the operator function and object B is passed as an argument.
The copy of object B is stored in temporary object O1. In the operator function member variable of object A can directly accessible.
Then we have created additional object “O2” which holds the sum of objects A & O1 (B).
The return type of operator function is same as that of its class. The function return object O2 and it is assigned to object C.
Overloading Binary Operator
In a C++ programming, overloading with single operator is called a binary operator overloading.
In C++, like a arithmetic operator binary operator can also be overloaded.
Binary operator are overloaded by using member function and friend function.
1 Binary operator overloading using member function: In a binary operator overloading using member function one argument is required.
This argument contains value of the object.
For example: If we perform the addition of two object o1 & o2, then the overloaded function can declared as:
operator + (xyz o) ;
Where, “xyz” is a class name and “o” is the object. To call operator function the statement is as follow:
o3 = o1 + o2;
Here, object o1 call the operator function and object o2 is passed as an argument & copy of object “o2” store in temporary object ”o”.
The following statement can also be rewrite as:
o3 = o1.operator + (o2);
Example. Write a program to overload + binary operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> using namespace std; class xyz { public: int x, y; void getdata() { x = 5; y = 3; } xyz operator + (xyz O1) { xyz O2; O2.x = x + O1.x; O2.y = y + O1.y; return O2; } void show() { cout << " x= " << x << " y = " << y << endl; } }; // end of class int main() { xyz A, B, C; A.getdata(); B.getdata(); A.show(); B.show(); C = A + B; C.show(); } |
Output
1 2 3 | x= 5 y = 3 x= 5 y = 3 x= 10 y = 6 |
Note: In the above program, A, B, C are the object of class xyz.
When the statement C= A + B is execute, the compiler searches for definition of operator + .
The object A call the operator function and object B is passed as an argument.
The copy of object B is stored in temporary object O1.
In the operator function member variable of object A can directly accessible .
Then we have created additional object “O2” which holds the sum of objects A & O1 (B).
The return type of operator function is same as that of its class.
The function return object O2 and it is assigned to object C.
2. Binary operator overloading using friend function: In a C++ we can also overload binary operator by using friend function.
We can say friend function can be alternate of member function for overloading binary operator.
Both member & friend function gives the same result.
Friend function is generally useful when we perform an operation with operand of two different types.
For example consider the statement:
x = 3 + y ;
Where, x & y are the object of same type. 3 and ‘y’ will pass as an argument to the operator+() function and + operator will call operator+() function.
Example. Write a program to perform multiplication using an integer an object by using friend function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <iostream> using namespace std; class xyz { public: int x, y; void getdata(); void show(); friend xyz operator * (int, xyz); // friend function declaration }; // end of class void xyz::getdata() { x = 5; y = 3; } void xyz::show() { cout << " x= " << x << " y =" << y << endl; } xyz operator * (int a, xyz obj) { xyz temp; temp.x = a * obj.x; temp.y = a * obj.y; return temp; } int main() { xyz A, B; A.getdata(); A.show(); B = 3 * A; B.show(); } |
Output
1 2 | x= 5 y =3 x= 15 y =9 |
In the above program, A & B are the object of class xyz. Statement “ B = 3 * A ;” perform multiplication.
This statement contains both integer and class object.
As we known that left-hand operand is always used to call the function and right-hand operand passed as an argument.
But In this case left-hand operand is integer and cannot call the function.
The operator *( ) function is declared as friend .
When the Statement “ B = 3 * A ;” will execute it will call the “*” operator will call the operator*() function.