Operator Overloading
Written and reviewed by Gagan Bhardwaj · 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 +
#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;
}4 + 6i
Overloading ==
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:
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
| Member | Friend / non-member | |
|---|---|---|
| Left operand | The object | A parameter |
| Parameters | One (right operand) | Two (both operands) |
| Use when | Left operand is your class | Left 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.
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
operatorplus 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 करना
#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;
}4 + 6i
== overload करना
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:
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
| Member | Friend / non-member | |
|---|---|---|
| बायाँ operand | Object | एक 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 अर्थ देता है।
operatorplus चिह्न वाला function परिभाषित करें।- Members बाएँ operand को object लेते हैं; friends दोनों लेते हैं।
<<non-member/friend होना चाहिए और stream लौटाए।- कुछ operators overload नहीं हो सकते; अर्थ सहज रखें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में 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 करने में क्या अंतर है?
cout << obj, जहाँ बायाँ पक्ष एक stream है।C++ में कौन-से operators overload नहीं हो सकते?
::, 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 लौटाए।