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.

त्वरित सार

यह 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 देखें।

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

C++ में arithmetic operator overload करने का syntax क्या है?
Member के रूप में binary arithmetic operator के लिए, आप ReturnType operatorX(const T &rhs) const लिखते हैं, जैसे Fraction operator*(const Fraction &o) const। वर्तमान object बायाँ operand है और parameter दायाँ operand, और function एक नया परिणाम object लौटाता है।
C++ में unary operator कैसे overload करते हैं?
Negation जैसा unary operator बिना parameters के member के रूप में overload होता है, जैसे Fraction operator-() const, जो वर्तमान object पर काम करता है और negated परिणाम लौटाता है। Unary operators एक operand लेते हैं, जो object खुद है।
C++ में ++ operator कैसे overload करते हैं?
Pre-increment रूप T& operator++() है और अद्यतन object लौटाता है; post-increment रूप T operator++(int) है, जहाँ अप्रयुक्त int parameter इसे अलग करता है और यह पुराना मान लौटाता है। यह दर्शाता है कि built-in types के लिए ++x और x++ कैसे भिन्न हैं।
C++ में operators को members या non-members के रूप में overload करना चाहिए?
जिन operators का बायाँ operand आपकी class हो वे आमतौर पर members होते हैं, जबकि symmetric operators और वे जिनका बायाँ operand दूसरा type हो, जैसे stream के साथ <<, non-members होते हैं, अक्सर friends। एक आम मार्गदर्शन है operators को members बनाना जब वे object बदलें और non-members जब symmetry मायने रखे।
क्या C++ में operators को अलग types लौटाने को overload कर सकते हैं?
हाँ। Overloaded operator कोई भी type लौटा सकता है जो operation के लिए समझ बने — comparison operators bool लौटाते हैं, arithmetic operators class का नया object लौटाते हैं, और stream operator stream का reference लौटाता है ताकि calls chain हो सकें।
← 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 दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.