C++ Operator Overloading 

Operator overloading is achieved when we can change the way operators work for the user-defined data types like structures and objects. It is an example of compile time polymorphism.

Operator overloading is used to overload or redefine most of the operators available in C++.  For example, we can overload an operator ‘+’ in a class such as Number so that we can add two numbers by just using + symbol.

Advantage: Operator overloading is used to perform different operations on the same operand.

 
Syntax for Operator Overloading:
class class_name {
    .. .. 
    public:
       return_type operator symbol (argument_list) {
           // code statements
       } 
    .. .. 
};

Here in this syntax,

  • return_type denotes the return type of the function.
  • operator is a keyword.
  • symbol is the operator that we want to overload. Such as: +<-, >, ++, etc.
  • argument_list is the argument list passed to the function.

 

 

Example 1: Unary Operator

Below is an example demonstrating operator overloading concept using the unary operator++” as prefix and postfix both.

operator_overload_unary.cpp

// operator overloading using unary operator ++
#include <iostream>
using namespace std;

class Counter {
private:
int num;

public:
// Constructor to initialize num to 10
Counter() : num(10) {}

// Overload unary operator ++ when used as prefix
void operator ++ () {
++num;
}

// Overload unary operator ++ when used as postfix
void operator ++ (int) { //syntax for unary operators as postfix
num++;
}

void print() {
cout << "Count: " << num << endl;
}
};

int main() {
Counter obj;

// Call the "void operator ++ (int)" function
obj++;
obj.print();

// Call the "void operator ++ ()" function
++obj;
obj.print();

return 0;
}

Output:

Count: 11
Count: 12

 

 

Example 2: Binary Operator

Below is an example demonstrating operator overloading concept using binary operator+”  to add two complex numbers.

operator_overload_binary.cpp

// operator overloading using binary operator +
#include <iostream>
using namespace std;

class Complex {
private:
double real, imaginary;

public:
// Constructor to initialize real and imaginary parts to 0.0
Complex() : real(0.0), imaginary(0.0) {}

void input() {
cout << "Enter real and imaginary parts separately: ";
cin >> real;
cin >> imaginary;
}

// Overload the + operator
Complex operator + (Complex const &obj) {
// using 'const' prevents the operator function from modifying the object
// using '&obj' avoids creating duplicate object by referencing the object
Complex temp;
temp.real = real + obj.real;
temp.imaginary = imaginary + obj.imaginary;
return temp;
}

void display() {
if (imaginary < 0)
cout << "Resulting Complex number: " << real << imaginary << "i";
else
cout << "Resulting Complex number: " << real << "+" << imaginary << "i";
}
};

int main() {
// create the Complex class objects
Complex complex1, complex2, result;

cout << "Enter first complex number: " << endl;
complex1.input();

cout << "Enter second complex number: " << endl;
complex2.input();

// complex1 calls the operator function
// complex2 is passed as an argument to the function
result = complex1 + complex2;

// display the resulting complex number
result.display();

return 0;
}

Output:

Enter first complex number:
Enter real and imaginary parts separately: 15 3
Enter second complex number:
Enter real and imaginary parts separately: 5 -8
Resulting Complex number: 20-5i

 

List of operators that can be overloaded are mentioned below −
+*/%^
&|~!,=
<><=>=++
<<>>==!=&&||
+=-=/=%=^=&=
|=*=<<=>>=[]()
->->*newnew []deletedelete []
 
List of operators that can not be overloaded are also mentioned below −
  • scope resolution (::)
  • member selector (.)
  • indirection (*)
  • ternary operator (?:)

NOTE: Two operators = and & are by default overloaded in C++ language.