Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 50

Operator Overloading in C++

In this lesson: Overload operators in C++ — +, ==, <<, [] — member vs friend functions, with examples and output. In Hindi & English.

What is operator overloading?

Operator overloading lets you teach operators like +, == and << how to work with your own classes. Instead of c3 = add(c1, c2), you can write c3 = c1 + c2 — your class starts to feel like a built-in type. It is a form of compile-time polymorphism.

How it works

You define a function named operator followed by the symbol. It can be a member (left operand is the object) or a non-member/friend (both operands are parameters).

Overloading +

C++
#include <iostream>
using namespace std;
class Complex {
public:
    double re, im;
    Complex(double r, double i) : re(r), im(i) {}
    Complex operator+(const Complex &o) const {   // member
        return Complex(re + o.re, im + o.im);
    }
};
int main() {
    Complex a(1, 2), b(3, 4);
    Complex c = a + b;               // uses operator+
    cout << c.re << " + " << c.im << "i\n";
    return 0;
}
Output:
4 + 6i

Overloading ==

C++
bool operator==(const Complex &o) const {
    return re == o.re && im == o.im;
}
// now: if (a == b) ...

Overloading <<

To print with cout << obj, the left operand is the stream — so this must be a non-member, usually a friend:

C++
friend ostream& operator<<(ostream &os, const Complex &c) {
    os << c.re << " + " << c.im << "i";
    return os;                       // return stream for chaining
}
// now: cout << a << "\n";

Member vs friend

MemberFriend / non-member
Left operandThe objectA parameter
ParametersOne (right operand)Two (both operands)
Use whenLeft operand is your classLeft operand is not (e.g. a stream)

For a compact syntax reference and a full worked class, see the operator overloading syntax page.

Rules and limits

  • You cannot invent new operators or change their precedence/arity.
  • A few cannot be overloaded: ::, ., .*, ?:, sizeof.
  • Keep the meaning intuitive — + should add, not subtract.

Common mistakes

  • Making operator<< a member (the stream must be the left operand).
  • Forgetting to return the stream from operator<<.
  • Giving an operator a surprising meaning that confuses readers.
  • Returning a reference to a local object from an operator.
🏋️ Practice

Write a Vector2D class with x, y. Overload + for addition, == for equality, and a friend << to print it as (x, y). Test all three.

Summary

  • Operator overloading gives operators custom meaning for your classes.
  • Define a function named operator plus the symbol.
  • Members take the left operand as the object; friends take both.
  • << must be a non-member/friend and return the stream.
  • A few operators cannot be overloaded; keep meanings intuitive.

Frequently Asked Questions

What is operator overloading in C++?
Operator overloading lets you give C++ operators like +, == or << a custom meaning when used with your own class objects. For example, you can define + so that two Complex numbers add naturally, making the class behave like a built-in type.
How do you overload an operator in C++?
You define a special function named operator followed by the symbol, such as operator+. It can be a member function, where the left operand is the object, or a non-member (often a friend) taking both operands as parameters, and it returns the result of the operation.
What is the difference between overloading an operator as a member vs a friend in C++?
As a member function, the left operand is the current object and the operator takes one parameter for the right operand. As a friend or non-member, both operands are parameters, which is needed when the left operand is not your class — for example cout << obj, where the left side is a stream.
Which operators cannot be overloaded in C++?
A few operators cannot be overloaded: the scope resolution ::, member access ., member pointer access .*, the ternary ?:, and sizeof. Most other operators, including arithmetic, comparison, assignment, and stream operators, can be overloaded.
Why overload the << operator in C++?
Overloading << lets you print your own objects with cout << obj in a natural way. Because the left operand is the stream, it must be a non-member, usually a friend, so it can access the object's private data while returning the stream for chaining.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicTemplates in C++ Your progress: 50 / 81 (62%) · Lesson 51 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free