Theory
दो words जो आपको confuse करने के लिए बने हैं
Overloading। Overriding। दो words जो नौ letters share करते हैं, एक ही unit में रहते हैं, और लगभग हर साल एक exam सवाल में साथ दिखते हैं: "Function overloading और function overriding differentiate कीजिए।"
एक viva में इन्हें mix कीजिए और follow-up सवाल जल्दी असहज हो जाते हैं।
अच्छी ख़बर यह है: आप पहले से बिना नाम लिए दोनों इस्तेमाल कर चुके हैं। Till के दो bill() versions? Overloading। Person के discount() को फिर से लिखता Student? Overriding।
Theory
वही counter बनाम family recipe
Overloading एक counter है जो chai, chai(large), chai(large, extra sugar) offer करता है: एक नाम, और cashier आपने क्या माँगा देखकर recipe चुनता है।
Overriding family की franchise branch है जो parent की standard thali recipe लेकर इसे अपने version से बदल देती है, वही dish नाम, वही order slip, अलग kitchen। आपको कौन सी मिलती है यह depend करता है कौन सी branch आपको serve करती है।
Theory
दोनों, formally
Function overloading: एक ही scope में दो या ज़्यादा functions एक नाम share करते हैं पर अपनी parameter list में अलग होते हैं (parameters की संख्या या types)। Compiler arguments match करके select करता है।
double bill(double amt);
double bill(double amt, double disc);
Function overriding: एक derived class एक base-class function को वही नाम AND वही signature से फिर define करती है। Child का version उस child के लिए parent का replace करता है।
class Person { virtual double discount(); };
class Student : public Person { double discount(); };
At a glance
Overloading बनाम overriding, exam table
| Aspect | Overloading | Overriding |
|---|---|---|
| Signature | DIFFER होनी चाहिए (params) | बिल्कुल MATCH होनी चाहिए |
| कहाँ | वही scope / class | Base class बनाम derived class |
| Inheritance चाहिए | नहीं | हाँ |
| Resolved | Compile time | Run time (virtual के साथ) |
| Polymorphism family | Compile-time (static) | Run-time (dynamic) |
Quiz
एक class में, एक student लिखता है: int calc(int a); और double calc(int a); एक overload की उम्मीद में। क्या होता है?
- Compile error: सिर्फ़ अलग return type से एक function overload नहीं हो सकता
- काम करता है: return types signatures को अलग बनाते हैं
- काम करता है, पर सिर्फ़ int version कभी call होता है
- calc पहली बार call होने पर runtime error
Show the answer
Compile error: सिर्फ़ अलग return type से एक function overload नहीं हो सकता
Overload resolution सिर्फ़ parameter list देखता है। दोनों functions (int) लेते हैं, तो compiler के लिए वे एक ही function है जो clashing return types के साथ दो बार declared है: एक error। यह सबसे-ज़्यादा-tested overloading fact है। Overload करने के लिए, parameters बदलिए: calc(int) और calc(double) ठीक होंगे।
Think first
इन चार को classify कीजिए
tap करने से पहले हर एक को overloading या overriding label कीजिए:
1. एक class में Item() और Item(char n[], double p)
2. Person के show() को वही signature से फिर define करता Staff
3. एक file में print(int) और print(char)
4. Person से discount() फिर define करता Student
Show the answer
1. Overloading (अलग parameter lists वाले constructors, overloading का everyday application)।
2. Overriding (base बनाम derived, वही signature)।
3. Overloading (वही scope, अलग parameter types)।
4. Overriding (card family का पूरा मुद्दा)।
Reuse करने के लिए quick test: एक जगह अलग params = overloading; एक parent-child pair के पार वही signature = overriding।
Watch out
तीन mark-losers
1. यह दावा करना कि overloading को inheritance चाहिए: नहीं चाहिए; यह एक scope के अंदर रहता है।
2. यह दावा करना कि सिर्फ़ return type से overload हो सकता है: नहीं हो सकता; parameters अलग होने चाहिए।
3. virtual भूल जाना: बिना इसके overriding अभी भी compile होता है, पर एक base-class pointer base version call करेगा (early binding)। Overriding तभी run-time polymorphism बनता है जब base function virtual declared हो।
Theory
हर एक कहाँ अपना काम कमाता है
Wild में overloading: constructors (default + parameterized + copy, एक नाम, तीन forms) और operator overloading (total + item को आपके अपने types के लिए meaningful बनाना)।
Wild में overriding: हर framework जो आप कभी इस्तेमाल करेंगे आपको एक base class देता है और उम्मीद करता है आप इसके hooks override करें: Android का onCreate(), एक game engine का update()। Card family का discount() इसी pattern का miniature है।
Summary
Key takeaways
- Overloading: वही नाम, अलग parameter list, वही scope, compile-time choice।
- Overriding: वही नाम, वही signature, base बनाम derived, virtual होने पर run-time choice।
- अकेले return type से कोई function कभी overload नहीं हो सकता।
- Overloading को inheritance नहीं चाहिए; overriding इसके बिना exist नहीं कर सकता।
- Applications: overloaded constructors और operators; discount() जैसे overridden hooks।
- Memory hook: एक counter, कई recipes बनाम franchise family dish फिर से लिखती है।