Operator Overloding in C++

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.

Output

Operator Overloading
Operator Overloading

Example: Write a program to perform addition of two objects using “operator” keyword.

Output

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.

Output

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.  

Output

 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.          

Categories C++