Operator Overloading
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
Quick recap
This is a compact syntax reference for operator overloading, with a full worked class. For the concept, member-vs-friend explanation, and rules, see the main operator overloading lesson.
A full worked class
A Fraction class overloading *, == and << together:
#include <iostream>
using namespace std;
class Fraction {
int num, den;
public:
Fraction(int n, int d) : num(n), den(d) {}
Fraction operator*(const Fraction &o) const { // binary member
return Fraction(num * o.num, den * o.den);
}
bool operator==(const Fraction &o) const {
return num * o.den == o.num * den;
}
friend ostream& operator<<(ostream &os, const Fraction &f) {
os << f.num << "/" << f.den;
return os;
}
};
int main() {
Fraction a(1, 2), b(2, 3);
cout << (a * b) << "\n"; // 2/6
cout << (a == Fraction(2, 4)) << "\n"; // 1 (true)
}2/6
1
Unary operators
A unary operator (one operand) takes no parameter as a member:
Fraction operator-() const { // negation, e.g. -f
return Fraction(-num, den);
}Which form to use
| Operator kind | Typical form |
|---|---|
Arithmetic (+ - * /) | Member or friend |
Comparison (== < >) | Member or friend, returns bool |
Stream (<< >>) | Non-member friend |
Unary (- !) | Member, no parameter |
Assignment (=) | Member, returns *this |
Overloading ++
Pre- and post-increment differ by a dummy int parameter:
Counter& operator++() { ++val; return *this; } // ++x (pre)
Counter operator++(int) { Counter t = *this; ++val; return t; } // x++ (post)The int is never used — it only tells the two forms apart. See this pointer for return *this.
Common mistakes
- Swapping pre/post increment return types (pre returns a reference, post a value).
- Making
<<a member instead of a friend/non-member. - Comparing fractions with
==without cross-multiplying. - Modifying operands inside a
constoperator.
Extend the Fraction class with + (add fractions with a common denominator) and pre/post ++ that increments the numerator. Print results with the overloaded <<.
Summary
- Binary member operator:
T operatorX(const T&) const. - Unary member operator: no parameter.
- Stream operators are non-member friends returning the stream.
- Pre/post
++differ by a dummyintparameter. - For the concept and rules, see the main operator overloading lesson.
Frequently Asked Questions
What is the syntax to overload an arithmetic operator in C++?
ReturnType operatorX(const T &rhs) const, for example Fraction operator*(const Fraction &o) const. The current object is the left operand and the parameter is the right operand, and the function returns a new result object.How do you overload a unary operator in C++?
Fraction operator-() const, which acts on the current object and returns the negated result. Unary operators take one operand, which is the object itself.How do you overload the ++ operator in C++?
T& operator++() and returns the updated object; the post-increment form is T operator++(int), where the unused int parameter distinguishes it and it returns the old value. This mirrors how ++x and x++ differ for built-in types.Should you overload operators as members or non-members in C++?
<< with a stream, are non-members, often friends. A common guideline is to make operators members when they modify the object and non-members when symmetry matters.Can you overload operators to return different types in C++?
bool, arithmetic operators return a new object of the class, and the stream operator returns a reference to the stream so calls can be chained.त्वरित सार
यह operator overloading के लिए एक संक्षिप्त syntax reference है, एक पूरे चलते class के साथ। Concept, member-बनाम-friend व्याख्या, और नियमों के लिए, मुख्य operator overloading lesson देखें।
एक पूरा चलता class
एक Fraction class जो *, == और << साथ overload करती है:
#include <iostream>
using namespace std;
class Fraction {
int num, den;
public:
Fraction(int n, int d) : num(n), den(d) {}
Fraction operator*(const Fraction &o) const { // binary member
return Fraction(num * o.num, den * o.den);
}
bool operator==(const Fraction &o) const {
return num * o.den == o.num * den;
}
friend ostream& operator<<(ostream &os, const Fraction &f) {
os << f.num << "/" << f.den;
return os;
}
};
int main() {
Fraction a(1, 2), b(2, 3);
cout << (a * b) << "\n"; // 2/6
cout << (a == Fraction(2, 4)) << "\n"; // 1 (true)
}2/6
1
Unary operators
Unary operator (एक operand) member के रूप में कोई parameter नहीं लेता:
Fraction operator-() const { // negation, jaise -f
return Fraction(-num, den);
}कौन-सा रूप इस्तेमाल करें
| Operator प्रकार | सामान्य रूप |
|---|---|
Arithmetic (+ - * /) | Member या friend |
Comparison (== < >) | Member या friend, bool लौटाता |
Stream (<< >>) | Non-member friend |
Unary (- !) | Member, कोई parameter नहीं |
Assignment (=) | Member, *this लौटाता |
++ overload करना
Pre- और post-increment एक dummy int parameter से भिन्न होते हैं:
Counter& operator++() { ++val; return *this; } // ++x (pre)
Counter operator++(int) { Counter t = *this; ++val; return t; } // x++ (post)int कभी इस्तेमाल नहीं होता — यह केवल दो रूपों को अलग बताता है। return *this के लिए this pointer देखें।
आम गलतियाँ
- Pre/post increment return types बदल देना (pre reference लौटाता है, post value)।
<<को friend/non-member के बजाय member बनाना।- Fractions को cross-multiply किए बिना
==से तुलना करना। constoperator के अंदर operands बदलना।
Fraction class को + (साझा denominator से fractions जोड़ें) और pre/post ++ से विस्तारित करें जो numerator बढ़ाए। Overloaded << से परिणाम print करें।
सारांश
- Binary member operator:
T operatorX(const T&) const। - Unary member operator: कोई parameter नहीं।
- Stream operators non-member friends हैं जो stream लौटाते हैं।
- Pre/post
++एक dummyintparameter से भिन्न। - Concept और नियमों के लिए, मुख्य operator overloading lesson देखें।