Facebook Page Twitter Page LinkedIn Page
× C++ Tutorial C++ Advanced C++ Resources


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: Using class member function

The below examples describes how to overload + operator to add two vectors. Here, the vector v1 is taken as (10, 15) and vector v2 ia 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: Using non-class member function

The below examples 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.

S.N.Operator Overloading Examples
1. Unary Operator Overloading
2. Binary Operator Overloading
3. Relational Operator Overloading
4. ++ and -- Operator Overloading
5. Assignment Operator Overloading
6. Function Call Operator Overloading
7. Subscript Operator Overloading