Concepts of polymorphism

Polymorphism का मतलब है एक नाम, कई forms: वही function call अलग व्यवहार करता है इस पर depend करते हुए कि कौन सा object इसे receive करता है या इसे कौन से arguments मिलते हैं।

8 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


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 होगा, और इसे क्या कहते हैं?

  1. Compile time पर; function overloading (compile-time polymorphism)
  2. Run time पर; virtual function dispatch
  3. Compile time पर; inheritance
  4. 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।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Polymorphism

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concepts of polymorphism · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn