C++ Tutorial C++ Advanced C++ References

C++ - Operator Overloading



With operator overloading feature in C++, we can make operators to work for user-defined classes. As we create a class, it creates a new type in the code and C++ allows us to specify the operators with a special meaning for a data type, this ability is known as operator overloading. For example - '+' operator can be overloaded for a string class to concatenate two strings.

Overloaded operator is a function with a special name. The name of the function starts with keyword operator followed by operator symbol. Like any other function, the overloaded operator has return type and passing arguments. Mostly, operator overloading is achieved by non-member functions or class member functions.

In the below case, operator overloading is done by class member function. The return type of the function is vector and it requires one argument (a vector object) to pass.

vector operator+ (const vector& v)

The below case describes operator overloading using non-member function. The return type of the function is vector and it requires two argument (two vector objects) to pass.

vector operator+ (const vector& v1, const vector& v2)

Example: overloading + operator (using class member function)

The example below describes how to overload + operator to add two vectors. Here, the vector v1 is taken as (10, 15) and vector v2 is taken as (5, 25). The addition operation of v1 and v2 creates v3 which is (10+5, 15+25) or (15, 40). In this method, operator is overloaded using class member function.

#include <iostream>
using namespace std;
 
class vector {
  public:
    int x, y;
    
    //class constructor
    vector() {}
    vector(int x, int y) {
      this->x = x;
      this->y = y;
    }

    //class function for operator overloading
    vector operator+ (const vector& v) {
      int X =  this->x + v.x;
      int Y =  this->y + v.y;  
      return vector(X, Y);    
    }
};

int main (){
  vector v1(10, 15), v2(5, 25), v3;

  v3 = v1 + v2;
  cout<<"v3 = ("<<v3.x<<", "<<v3.y<<")"; 
  return 0;
}

The output of the above code will be:

v3 = (15, 40)

Example: overloading + operator (using non-class member function)

The example below describes how to overload + operator to add two vectors. In this method, operator is overloaded using non-class member function.

#include <iostream>
using namespace std;
 
class vector {
  public:
    int x, y;
    
    //class constructor
    vector() {}
    vector(int x, int y) {
      this->x = x;
      this->y = y;
    }
};

//non-class function for operator overloading
vector operator+ (const vector& v1, const vector& v2) {
  int X =  v1.x + v2.x;
  int Y =  v1.y + v2.y;  
  return vector(X,Y);    
}

int main (){
  vector v1(10, 15), v2(5, 25), v3;

  v3 = v1 + v2;
  cout<<"v3 = ("<<v3.x<<", "<<v3.y<<")"; 
  return 0;
}

The output of the above code will be:

v3 = (15, 40)

Overloadable operators in C++

Following is the list of operators that can be overloaded in C++.

Overloadable operators in C++
+-*/=<>
+=-=*=/===<<>>
++--<<=>>=!=<=>=
%&^!|~&=
^=|=&&||%=()[]
,->*->newdelete new[]delete[]

Non-overloadable operators in C++

Here is the list of operators that can not be overloaded in C++.

Non-overloadable operators in C++
..*::? :###

Operator Overloading Examples

Here is the list of various operator overloading examples which can be used to understand the concept more deeply.