In this article we will discuss what is operator overloading in C++, why do we need it and how to overload an operator for a user defined class.

What is Operator overloading ?

Operator Overloading means providing multiple definition for the same operator.

For example, using + operator with two integers will add the numbers, whereas, using + operator with two std::string arguments will concatenate the strings i.e.

#include <iostream>
#include <string>

int main()
{
	int a = 9;
	int b = 10;

	// + operator is by default overloaded for int
	// It will add the numbers
	int c = a + b;

	std::cout<<"c = "<<c<<std::endl;

	std::string str1 = "hello";
	std::string str2 = "world";

	// + operator is by default overloaded for string
	// It will concatenate the strings
	std::string str3 = str1 + str2;

	std::cout<<"str3 = "<<str3<<std::endl;

	return 0;
}

Output:
c = 19
str3 = helloworld

Operators for primitive data types like int, double and string etc. are already overloading. But for user defined classes, we need to overload them.

Need of Overloading Operator for a User Defined Class ?

C++ provides the facility to overload operator for user defined classes too.

Suppose we have a user defined class ComplexNumber and we created 2 objects of it i.e.

/*
 * A class representing complex number
 * It has both real and imaginary part
 */
class ComplexNumber
{
	int real;
	int imaginary;
public:
	ComplexNumber(int r, int i) :
			real(r), imaginary(i)
	{}
};

Now suppose we want to use + operator with its two objects i.e.
ComplexNumber c1(2, 3);
ComplexNumber c2(4, 5);

// Calling + operator with Class objects
ComplexNumber c3 = c1 + c2;

It will show compile error i.e. because like other operators, + operator is by default  overloaded for primitive data types only but not for user defined classes.

Compiler don’t know what to do when we call + operator with operands of type ComplexNumber.

If we want to use + operator with a user defined class then we need to overload it . Let’s see how to overload + operator in class ComplexNumber

How to overload operator in use defined class ?

To overload an operator for user defined class, we will write a separate operator function for it i.e.

operator X(arguments)

Here X represents the operator symbol i.e. +, – , / etc.

These operator functions can be: either global function or class member function

Overload + operator as global function for User Defined Class ComplexNumber

As + operator is a binary operator, so it will take two arguments i.e.

//Overloaded + operator for class
ComplexNumber operator+(ComplexNumber obj1, ComplexNumber obj2)
{
	return ComplexNumber(obj1.real + obj2.real, obj1.imaginary + obj2.imaginary);
}

It accepts two arguments i.e. two objects of our user defined class and returns a new object of same class.

Now as this function is accessing private members of user defined class ComplexNumber. So, we need to declare it as friend function i.e.

Now, when we will use + operator with ComplexNumber class objects, then it will call this global overloaded function i.e.

#include <iostream>
#include <string>
#include <assert.h>
/*
 * A class representing complex number
 * It has both real and imaginary part
 */
class ComplexNumber
{
	int real;
	int imaginary;
public:
	ComplexNumber(int r, int i) :
			real(r), imaginary(i)
	{}
	void print()
	{
		std::cout << real << " + i" << imaginary << std::endl;
	}
	//Overloaded + operator for class
	friend ComplexNumber operator+(ComplexNumber obj1, ComplexNumber obj2);
};

//Overloaded + operator for class
ComplexNumber operator+(ComplexNumber obj1, ComplexNumber obj2)
{
	return ComplexNumber(obj1.real + obj2.real, obj1.imaginary + obj2.imaginary);
}

int main()
{
	ComplexNumber c1(2, 3);
	ComplexNumber c2(4, 5);
	c1.print();
	c2.print();

	// Calling + operator with Class objects
	ComplexNumber c3 = c1 + c2;

	c3.print();
	return 0;
}

Output:
2 + i3
4 + i5
6 + i8

 

Join a list of 2000+ Programmers for latest Tips & Tutorials