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.
Frequently Asked Questions
What is operator overloading in C++?
+, == 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++?
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++?
cout << obj, where the left side is a stream.Which operators cannot be overloaded in C++?
::, 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++?
<< 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 करना
#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 नहीं हो सकते; अर्थ सहज रखें।