Theory
एक button, दो prices
Canteen counter पर, एक student अपना card tap करता है: till thali के लिए Rs 27 charge करता है। एक staff member उसी reader पर tap करता है, वही thali ख़रीदते हुए: Rs 24।
Cashier ने न machines बदलीं, न कोई अलग program खोला। एक action, bill(card), ने इस पर depend करते हुए अलग व्यवहार किया कि किसने tap किया।
आपका code भी यह कर सकता है, और इस idea का एक grand नाम है: polymorphism, OOP का चौथा और आख़िरी pillar।
Theory
एक word, कई performances
अपनी grandmother, अपने professor, और अपने best friend से "namaste" कहिए। वही word, तीन अलग deliveries: tone, gesture, warmth सब receiver के अनुसार adapt होते हैं।
Code में polymorphism बिल्कुल यही है: एक नाम जिसका व्यवहार adapt होता है उस object के अनुसार जो इसे perform कर रहा है। Caller एक चीज़ कहता है; receiver तय करता है इसका उनके लिए क्या मतलब है।
Theory
Polymorphism, formally
Polymorphism Greek से आता है: poly (many) + morphe (form): एक नाम, कई forms।
Exam के लिए definition: polymorphism एक अकेले function name, operator या interface की अलग contexts में अलग व्यवहार करने की क्षमता है।
C++ में "context" दो moments में तय हो सकता है:
- compile करते समय (कौन सा version इन arguments को fit करता है?),
- run करते समय (यह call असल में कौन सा object receive कर रहा है?)।
At a glance
दोनों families (details अगले lessons में)
| Family | तय होता है | इससे achieve होता है |
|---|---|---|
| Compile-time (static) | Compilation के दौरान | Function overloading, operator overloading |
| Run-time (dynamic) | Program चलते समय | Virtual functions + inheritance |
Theory
Counter पर दोनों families
Compile-time flavour: till के पास bill(double amount) और bill(double amount, double discount) हैं। दो functions, एक नाम; compiler आपके पास किए गए arguments देखकर चुनता है। Program कभी run होने से पहले ही हो चुका।
Run-time flavour: Student और Staff दोनों Person से inherit करते हैं और हर एक अपना discount() define करता है। Counter एक Person reference रखता है; एक असली card tap होने पर ही program discover करता है कौन सा discount() run करना है।
वही pillar, दो mechanisms।
Quiz
Till bill(double amt) और bill(double amt, double disc) define करता है। आप bill(50.0) call करते हैं। यह कब तय होता है कि कौन सा version run होगा, और इसे क्या कहते हैं?
- Compile time पर; function overloading (compile-time polymorphism)
- Run time पर; virtual function dispatch
- Compile time पर; inheritance
- Run time पर; operator overloading
Show the answer
Compile time पर; function overloading (compile-time polymorphism)
Argument list (एक double) पहले से compiler को exactly बताती है कौन सा version match करता है, तो decision compilation के दौरान होता है: यह function overloading है, compile-time family। Run-time dispatch (option B) तभी आता है जब virtual functions और base-class pointers शामिल हों, और इस scenario में कोई inheritance है ही नहीं।
Think first
Run-time case पहचानिए
एक function एक बार लिखा गया: void charge(Person &p) { total = total - p.discount(); }। कभी एक Student pass होता है, कभी एक Staff। tap करने से पहले: compiler यह क्यों तय नहीं कर सकता कौन सा discount() run होता है, और program को इसके बजाय क्या करना चाहिए?
Show the answer
Compile time पर parameter सिर्फ़ "कोई Person" है: असली object (Student? Staff?) तभी exist करता है जब program run होता है और एक असली card tap होता है। तो decision को run time तक postpone करना ज़रूरी है, जहाँ call object के अपने discount() तक route होती है। इस routing के लिए C++ को function को virtual mark करना चाहिए, जहाँ से अगले lessons उठाते हैं।
Watch out
Classification trap
Exams पूछते हैं: "क्या function overloading run-time polymorphism है?" नहीं: overloading compiler से resolve होता है, इसे compile-time (जिसे static या early-bound भी कहते हैं) polymorphism बनाते हुए।
और यह दावा मत कीजिए कि polymorphism को हमेशा inheritance चाहिए: run-time family को यह चाहिए, पर overloading एक अकेली class में बिना कोई parent दिखे काम करता है। Family को mechanism से match कीजिए, किसी blanket rule से नहीं।
Theory
Designers इसे क्यों पसंद करते हैं
ऊपर वाला charge() function फिर कभी नहीं बदलेगा: अगले साल अपने discount() के साथ एक Guest card जोड़िए, और charge() इसे बिना एक edit के serve करता है। Extension के लिए open, modification के लिए closed code: यही polymorphism का professional payoff है, और interviewers इसे बिल्कुल इसी तरह describe करते हैं।
Summary
Key takeaways
- Polymorphism = एक नाम, कई forms: चौथा OOP pillar।
- एक call context से अलग व्यवहार करता है: pass किए arguments या receive करता object।
- Compile-time family: function और operator overloading, compiler से resolve।
- Run-time family: inheritance के साथ virtual functions, program चलते हुए resolve।
- Overloading run-time polymorphism NAHI है: classic classification trap।
- नई derived classes पुराने polymorphic code में बिना edit किए plug हो जाती हैं।
- Memory hook: grandmother, professor, friend से namaste।