Operator Overloading in C++ (original) (raw)

Last Updated : 17 Jan, 2026

Operator overloading means giving a new meaning to an operator (like +, -, *, []) when it is used with objects.

**Example: In this example, the + operator is overloaded to add two Number objects.

C++ `

#include using namespace std;

struct Number { int value;

Number(int v)
{
    value = v;
}

// Overload + operator
Number operator+(const Number &n)
{
    return Number(value + n.value);
}

void display()
{
    cout << value << endl;
}

};

int main() { Number n1(5), n2(10); Number n3 = n1 + n2; n3.display(); }

`

Why use Operator Overloading?

Operator Functions vs Normal Functions

Feature Operator Function Normal Function
Syntax Uses operator keyword Standard function name
Invocation Triggered by using an operator Called explicitly by name
Purpose Redefines behavior of operators Performs defined actions
Example operator+() add()

Operators That Cannot Be Overloaded

The following operators cannot be overloaded in C++:

  1. sizeof
  2. typeid
  3. Scope resolution (::)
  4. Class member access operators (. and .*)
  5. Ternary / conditional operator (?:)

Why These Operators Cannot Be Overloaded?

1. sizeof Operator

2. typeid Operator

3. Scope resolution (::) Operator

4. Class Member Access Operators (. and .*)

The importance and implicit use of class member access operators can be understood through the following example:

C++ `

#include using namespace std;

class Complex { private: int real; int imaginary;

public: Complex(int real, int imaginary) { this->real = real; this->imaginary = imaginary; } void print() { cout << real << " + i" << imaginary; } Complex operator+(Complex c2) { Complex c3(0, 0); c3.real = this->real + c2.real; c3.imaginary = this->imaginary + c2.imaginary; return c3; } }; int main() { Complex c1(3, 5); Complex c2(2, 4); Complex c3 = c1 + c2; c3.print(); return 0; }

`

**Explanation:

5. Ternary or conditional (?:) Operator

Important Points About Operator Overloading

Conversion Operator Example:

C++ `

#include using namespace std;

class Fraction { private: int num, den;

public: Fraction(int n, int d) { num = n; den = d; }

// Conversion operator
operator float() const
{
    return float(num) / float(den);
}

};

int main() { Fraction f(2, 5); float val = f; cout << val << endl; }

`

**Note: Conversion operators must be member functions.

Conversion Constructors

Any constructor that can be called with a single argument acts as a conversion constructor and enables implicit type conversion.

C++ `

#include using namespace std;

class Point { private: int x, y;

public: Point(int i = 0, int j = 0) { x = i; y = j; } void print() { cout << "x = " << x << ", y = " << y << '\n'; } };

int main() { Point t(20, 20); t.print(); t = 30; t.print(); return 0; }

`

Try It Yourselfredirect icon

Output

x = 20, y = 20 x = 30, y = 0