Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 50

Operator Overloading

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

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.

Operator overloading क्या है?

Operator overloading आपको +, == और << जैसे operators को आपकी अपनी classes के साथ काम करना सिखाने देता है। c3 = add(c1, c2) के बजाय, आप c3 = c1 + c2 लिख सकते हैं — आपकी class built-in type जैसी लगने लगती है। यह compile-time polymorphism का एक रूप है।

यह कैसे काम करता है

आप operator के बाद चिह्न वाला एक function परिभाषित करते हैं। यह member (बायाँ operand object) या non-member/friend (दोनों operands parameters) हो सकता है।

+ overload करना

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;               // operator+ istemaal
    cout << c.re << " + " << c.im << "i\n";
    return 0;
}
Output:
4 + 6i

== overload करना

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

<< overload करना

cout << obj से print करने को, बायाँ operand stream है — तो यह non-member होना चाहिए, आमतौर पर friend:

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

Member बनाम friend

MemberFriend / non-member
बायाँ operandObjectएक parameter
Parametersएक (दायाँ operand)दो (दोनों operands)
कब इस्तेमालबायाँ operand आपकी classबायाँ operand नहीं (जैसे stream)

संक्षिप्त syntax reference और पूरे चलते class के लिए, operator overloading syntax page देखें।

नियम और सीमाएँ

  • आप नए operators नहीं बना सकते या उनकी precedence/arity नहीं बदल सकते।
  • कुछ overload नहीं हो सकते: ::, ., .*, ?:, sizeof
  • अर्थ सहज रखें — + को जोड़ना चाहिए, घटाना नहीं।

आम गलतियाँ

  • operator<< को member बनाना (stream बायाँ operand होना चाहिए)।
  • operator<< से stream लौटाना भूलना।
  • Operator को अप्रत्याशित अर्थ देना जो पाठकों को भ्रमित करे।
  • Operator से local object का reference लौटाना।
🏋️ अभ्यास

एक Vector2D class लिखें जिसमें x, y हो। जोड़ के लिए +, समानता के लिए ==, और इसे (x, y) के रूप में print करने को friend << overload करें। तीनों test करें।

सारांश

  • Operator overloading operators को आपकी classes के लिए custom अर्थ देता है।
  • operator plus चिह्न वाला function परिभाषित करें।
  • Members बाएँ operand को object लेते हैं; friends दोनों लेते हैं।
  • << non-member/friend होना चाहिए और stream लौटाए।
  • कुछ operators overload नहीं हो सकते; अर्थ सहज रखें।
← 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.