Summary: In this tutorial, we will learn what operator overloading is, what its significance is, and how to overload an operator in C++.

Introduction to Operator Overloading

C++ allows us to overload an operator to make it work according to our needs.

One such great use of operator overloading is that we can make operators such as +, -, ++, –, /, *, etc. operational not only with primitive data types but also with user-defined types.

For instance, consider the following example where we are adding two integer variables:

int var1 = 2;
int var2 = 3;
int sum = var1 + var2; //5

But we cannot perform the same arithmetic operation on the user-defined objects, as described below:

class MyClass{
    //Some code
};
    
int main{
    MyClass obj1();
    MyClass obj2();
    MyClass obj3 = obj1 + obj2;    //Error
}     

We can’t use arithmetic operators on user-defined objects as we do with primitive data types, but using operator overloading we can make this possible.

The syntax for overloading any operator depends on its type. So let’s look at the types of operator and operator overloading in C++.

Types of Operator Overloading

Operator overloading in C++ mainly consists of two types, depending on the type of operator:

1. Binary Operator Overloading

A binary operator is an operator that operates on two operands, such as +, -, etc.

When we overload a binary operator, it is known as binary operator overloading.

Syntax:

<class> operator<operator_symbol>(const <class> &obj){
    //code to return new class object
}

Example: Overloading + operator to add properties of two different objects of the same class (Implementation below).

2. Unary Operator Overloading

A unary operator is an operator that operates only on one operand such as ++, –, etc.

When we overload a unary operator, we call it unary operator overloading.

Syntax:

<return_type> operator<operator_symbol>(){
    //code
}

//OR

<return_type> operator<operator_symbol>(<data_type> var){
    //code
}

The first syntax is for prefix unary operator and second syntax is for postfix unary operator.

Example: Overloading ++ operator to increment a property of an object (Implemention below).

Example of Operator Overloading in C++

#include <iostream>
using namespace std;
 
class Stock{
  double price;

public:
  Stock(){
    price = 0.0;
  }

  //parameterized constructor
  Stock(double price){
    this->price = price;
  }

  double getPrice(){
    return this->price;
  }

  //overloading + operator
  Stock operator+(const Stock &obj){
    Stock temp;
    temp.price = this->price + obj.price;
    return temp;
  }

  //overloading postfix ++ operator
  void operator++(int price){
    this->price = this->price + 1.0;
  }

  //overloading prefix ++ operator
  void operator++(){
    this->price = this->price + 1.0;
  }
};
 
int main()
{
  //creating two objects of Stock class
  Stock TATA(42.89);
  Stock Reliance(36.78);

  //using overloaded + operator to get the total value of stocks
  Stock stock_market = TATA + Reliance;
  cout << "Stock Market Value: "<< stock_market.getPrice() << endl;

  //using overloaded ++ operator to increment stock price of TATA by 1.0
  TATA++;
  cout << "TATA stock price: "<< TATA.getPrice() <<endl;
}

Output:

Stock Market Value: 79.67
TATA stock price: 43.89


In the above program, we have overload both unary (i.e. ++) and binary (i.e. +) operators as member methods of the class.

Inside the method, we have modified the property (i.e. price) of the object as per our requirement.

In this tutorial, we learned the significance of operator overloading in C++ and its implementation with the help of an example.

Leave a Reply

four × 1 =