In the previous article, we have discussed about C++ : How to Find Duplicates in a Vector. Let us learn Operator Overloading in C++ Program.

Operator overloading is a feature of object-oriented programming that enables a programmer to redefine a built-in operator to work with user-defined data types.
Let’s pretend we’ve created a class called Integer to handle integer operations. To handle the various operations, we can use the functions add(), subtract(), multiply(), and divide(). However, it is preferable to use operators that correspond to the given operations(+, -, *, and /, respectively) to make the code more understandable and improve readability, i.e. we can substitute the following code with the following code.

When using the + operator with two integers, for example, the numbers are added, while when using the + operator with two string arguments, the strings are concatenated.

Below is the Implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
string string1 = "BTechGeeks";
string string2 = "Platform";
// For strings, the + operator is overloaded by default.
// It will join the strings together.
string string3 = string1 + string2;
cout << "concatenated string = " << string3 << endl;
return 0;
}

Output:

concatenated string = BTechGeeksPlatform

Operators for primitive data types such as int, double, string, and so on are already overloaded. However, we cannot overload user-defined classes.

The overload operator is also available in C++ for user-defined classes.

Assume we have a user-defined class called ComplexNumber, and we have generated two objects of it, i.e.

class ComplexNum
{
int real;
int imaginary;
public:
ComplexNumber(int re, int im) :
real(re), imaginary(im)
{}
};

Let’s say we want to use the + operator with two objects, i.e.

ComplexNum cm1(5, 2);
ComplexNum cm2(9, 7);
// calling +(operator) for the above objects
ComplexNumber cm3 = cm1 + cm2;

It will show a compile error, indicating that, like other operators, the + operator is overloaded by default for primitive data types only, not for user-defined classes.
When we use the + operator with ComplexNumber operands, the compiler is stumped.

We must overload a user specified class if we want to use the + operator for it. Let’s look at how to overload the + operator in the ComplexNumber class.

### 4)How to use a given class to overload an operator

To use operator overloading for user define class it is implemented as below,

operator X(arguments)

Here X represents the operator symbol, such as +, –, /, and so on.
The following are examples of operator functions: either a global function or a function that is a part of a class.

Since the + operator is a binary one, it can accept two arguments.
It takes two arguments, namely two objects of our custom class, and returns a new object of the same class.

This feature now has access to the private members of the user-defined class ComplexNumber. As a result, we must declare it as a friend function, i.e.

Now, if we use the + operator with ComplexNumber class objects, this global overloaded function, i.e.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;

class ComplexNum {
int real;
int imaginary;

public:
ComplexNum(int re, int im)
: real(re)
, imaginary(im)
{
}
void print()
{ // printing the real and imaginary part of the complex
// number
cout << real << " + " << imaginary << "i" << endl;
}
friend ComplexNum operator+(ComplexNum obj1,
ComplexNum obj2);
};
ComplexNum operator+(ComplexNum object1, ComplexNum object2)
{
return ComplexNum(object1.real + object2.real,
object1.imaginary
+ object2.imaginary);
}
int main()
{
ComplexNum cm1(5, 2);
ComplexNum cm2(9, 7);
cm1.print();
cm2.print();
// calling + opertor for two objects
ComplexNum cm3 = cm1 + cm2;
cm3.print();
return 0;
}

Output:

5 + 2i
9 + 7i
14 + 9i

Here both imaginary and real part of Complex number is added

Similarly we can overload any operator by the same method as given above.

Related Programs: