Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
📘 Lesson  ·  Lesson 70

Operator Overloading

Written and reviewed by · 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:

C++
#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)
}
Output:
2/6
1

Unary operators

A unary operator (one operand) takes no parameter as a member:

C++
Fraction operator-() const {       // negation, e.g. -f
    return Fraction(-num, den);
}

Which form to use

Operator kindTypical 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:

C++
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 const operator.
🏋️ Practice

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 dummy int parameter.
  • 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++?
For a binary arithmetic operator as a member, you write 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++?
A unary operator like negation is overloaded as a member with no parameters, such as 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++?
The pre-increment form is 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++?
Operators whose left operand is your class are usually members, while symmetric operators and those whose left operand is another type, like << 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++?
Yes. An overloaded operator can return any type that makes sense for the operation — comparison operators return 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 करती है:

C++
#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)
}
Output:
2/6
1

Unary operators

Unary operator (एक operand) member के रूप में कोई parameter नहीं लेता:

C++
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 से भिन्न होते हैं:

C++
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 किए बिना == से तुलना करना।
  • const operator के अंदर 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 ++ एक dummy int parameter से भिन्न।
  • Concept और नियमों के लिए, मुख्य operator overloading lesson देखें।
← 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.