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.

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 नहीं हो सकते; अर्थ सहज रखें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में operator overloading क्या है?
Operator overloading आपको +, == या << जैसे C++ operators को आपके अपने class objects के साथ इस्तेमाल होने पर custom अर्थ देने देता है। जैसे, आप + परिभाषित कर सकते हैं ताकि दो Complex numbers स्वाभाविक रूप से जुड़ें, class को built-in type जैसा बनाते हुए।
C++ में operator कैसे overload करते हैं?
आप operator के बाद चिह्न वाला एक विशेष function परिभाषित करते हैं, जैसे operator+। यह member function हो सकता है, जहाँ बायाँ operand object हो, या non-member (अक्सर friend) दोनों operands को parameters के रूप में लेते हुए, और यह operation का परिणाम लौटाता है।
C++ में operator को member बनाम friend के रूप में overload करने में क्या अंतर है?
Member function के रूप में, बायाँ operand वर्तमान object है और operator दाएँ operand के लिए एक parameter लेता है। Friend या non-member के रूप में, दोनों operands parameters हैं, जो तब चाहिए जब बायाँ operand आपकी class न हो — जैसे cout << obj, जहाँ बायाँ पक्ष एक stream है।
C++ में कौन-से operators overload नहीं हो सकते?
कुछ operators overload नहीं हो सकते: scope resolution ::, member access ., member pointer access .*, ternary ?:, और sizeof। अधिकांश अन्य operators, arithmetic, comparison, assignment, और stream operators सहित, overload हो सकते हैं।
C++ में << operator क्यों overload करें?
<< overload करना आपको cout << obj से अपने objects को स्वाभाविक तरीके से print करने देता है। चूँकि बायाँ operand stream है, इसे non-member होना चाहिए, आमतौर पर friend, ताकि यह object के private data तक पहुँच सके जबकि chaining के लिए stream लौटाए।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.