Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 70

Operator Overloading

त्वरित सार

यह 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 दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicVirtual Functions आपकी प्रगति: 70 / 81 (86%) · Lesson 71 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें