Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - OPERATOR OVERLOADING


We have already seen that the + operatior is used to add to numbers.


Example :



#include <iostream>
using namespace std;

int main() {	

    int x = 5;
	int y = 7;
	int z = x + y;
	cout << "The added result is : " << z;

	return 0;
}


Output :



  The added result is : 12

That was quite easy.


Now, let us say, we have a class that has an integer value. And we want to create two objects and add them.


Let me make it a little simpler with the below example.


Example :



#include <iostream>
using namespace std;

class MyClass
{
    int x;
    
    public:
    	MyClass() 
    	{
    	}
    
    	MyClass(int temp)
    	{
        	x = temp;
    	}
};

int main() {	

    MyClass obj1(5);
    MyClass obj2(7);
    MyClass obj3 = obj1 + obj2;
    cout << "The added result " << obj3;

	return 0;
}


Output :



error: no match for operator+ (operand types are MyClass and MyClass)

So, in the above code, we have created a class named MyClass that has an attribute named x. And we have defined a Constructor that initialises the attribute x.


class MyClass
{
	int x;

	public:
		MyClass()
		{
		}

		MyClass(int temp)
		{
			x = temp;
		}
};

Then we have created an object obj1 of the class MyClass.


MyClass obj1(5);

And initialised the value of x with 5(For object obj1).

java_Collections

Similarly, we have created a second object obj2.


var obj2 = new MyClass(7);

And initialised the value of x with 7(For object obj2).

java_Collections

Now, we tried to add two objects obj1 and obj2. Just to add 5 and 7.


MyClass obj2(7);

But we end up with the below error,


error: no match for operator+ (operand types are MyClass and MyClass)

This is because C++ is confused that how can adding two objects add their values?


With this we come to the concept of Overloading the operators. i.e. We will make the + operator act the way, we want it to act.


Let us modify the above example.


Example :



#include <iostream>
using namespace std;

class MyClass
{
    int x;
    
    public:
        MyClass()
        {
        }
        
        MyClass(int temp)
        {
            x = temp;
        }
    
        MyClass operator + (MyClass myClass1) 
        {
    		MyClass myClass;
    		myClass.x = x + myClass1.x;
            return myClass;
        }
        
        void showResult()
        {
            cout << "The added result is : " << x;
        }
};

int main() {	

    MyClass obj1(5);
    MyClass obj2(7);
    MyClass obj3 = obj1 + obj2;
    obj3.showResult();

	return 0;
}


Output :



  The added result : 12

And all we have done is, defined a method called public static MyClass operator + (MyClass myClass1, MyClass myClass2).


	MyClass operator + (MyClass myClass1)
	{
		MyClass myClass;
		myClass.x = x + myClass1.x;
		return myClass;
	}

This operator + is already defined by C++. When we specify operator before the + sign. C++ understands, the + needs to be Overloaded.


And all we are doing is, redefining the + sign. So that it works exactly the way we want it to work.


So, the MyClass operator + (MyClass myClass1) has a parameter, MyClass myClass1.


Now, the operator + method gets called when the below statement executes.


MyClass obj3 = obj1 + obj2;

And what happens is, the value of the object, obj1 is assigned to the current object and obj2 is assigned to obj.

java_Collections

And right now, current object has the contents of obj1,

java_Collections

And obj has the contents of obj2,

java_Collections

Now, since, the operator + (MyClass myClass1) method has the contents,


MyClass operator + (MyClass myClass1)
{
	MyClass myClass;
	myClass.x = x + myClass1.x;
	return myClass;
}

The next statement,


myClass.x = x + myClass1.x;

Adds the value of x i.e. 5 and obj.x i.e. 7.


And the added value is returned,


return myClass;

To obj3,


MyClass obj3 =  obj1 + obj2
java_Collections


So, the next statement calls the method showResult,


obj3.showResult();

And the method showResult,


public void showResult()
{
	cout << "The added result is : " << x;
}

Prints the value of x from the object obj3. And we get the below output.


The added result : 12

But have you thought, when we are executing the statement,


MyClass obj3 =  obj1 + obj2

How come the MyClass operator + (MyClass myClass1) gets called?


This is because, for every operator, be it a + or - or any other operator, C++ uses a method for each.


Similarly, for subtraction, multiplication and division C++ has the below Overloaded method.


So, if you want to overload, any of the operators(Like -, *, /), you can define and customise it your own way.


Overloading '-' operator


Example :



#include <iostream>
using namespace std;

class MyClass
{
    int x;
    
    public:
        MyClass()
        {
        }
        
        MyClass(int temp)
        {
            x = temp;
        }
    
        MyClass operator - (MyClass myClass1) 
        {
    		MyClass myClass;
    		myClass.x = x - myClass1.x;
            return myClass;
        }
        
        void showResult()
        {
            cout << "The subtracted result is : " << x;
        }
};

int main() {	

    MyClass obj1(15);
    MyClass obj2(7);
    MyClass obj3 = obj1 - obj2;
    obj3.showResult();

	return 0;
} 


Output :



  The subtracted result is : 8