Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 47

Polymorphism in C++

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

What is polymorphism?

Polymorphism means "many forms." It is the idea that one interface — a function name, an operator — can behave differently depending on the object it acts on. It lets you treat a whole family of related objects the same way, while each responds in its own manner.

Two kinds

  • Compile-time (static) — resolved by the compiler: function overloading and operator overloading.
  • Run-time (dynamic) — resolved while the program runs: virtual functions with inheritance.

Compile-time polymorphism

The compiler picks the right version by the signature. Same name, different parameters:

C++
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
// add(2,3) → int version; add(2.5,1.5) → double version

See function overloading and operator overloading.

Run-time polymorphism

Here the choice happens while the program runs. A base-class function is marked virtual, derived classes override it, and calling through a base pointer picks the right override based on the actual object:

C++
class Shape {
public:
    virtual void draw() { cout << "Shape\n"; }   // virtual
};
class Circle : public Shape {
public:
    void draw() override { cout << "Circle\n"; }
};

A run-time example

C++
#include <iostream>
using namespace std;
class Shape { public: virtual void draw() { cout << "Shape\n"; } };
class Circle : public Shape { public: void draw() override { cout << "Circle\n"; } };
class Square : public Shape { public: void draw() override { cout << "Square\n"; } };

int main() {
    Shape *s;
    Circle c; Square sq;
    s = &c; s->draw();    // Circle
    s = &sq; s->draw();   // Square
    return 0;
}
Output:
Circle
Square

The same call s->draw() runs different code depending on what s points to — that is run-time polymorphism. It relies on virtual functions.

Compile-time vs run-time

Compile-timeRun-time
Also calledStaticDynamic
Achieved byOverloadingVirtual functions
ResolvedAt compile timeWhile running
SpeedFasterSlight overhead

Common mistakes

  • Expecting run-time behaviour without marking the base function virtual.
  • Calling through an object (not a pointer/reference), which does not use dynamic dispatch.
  • Forgetting a virtual destructor in a polymorphic base class.
  • Confusing overloading (compile-time) with overriding (run-time).
🏋️ Practice

Make an Animal base with a virtual speak(), and derive Dog and Cat. Store pointers to different animals in an array and loop over it calling speak() — watch each print its own sound.

Summary

  • Polymorphism means one interface, many forms.
  • Compile-time polymorphism uses overloading, resolved by the compiler.
  • Run-time polymorphism uses virtual functions and inheritance.
  • Run-time dispatch needs a base pointer/reference and virtual.
  • It makes code flexible and easy to extend with new types.

Polymorphism क्या है?

Polymorphism का अर्थ है "कई रूप।" यह विचार है कि एक interface — एक function नाम, एक operator — जिस object पर काम करे उसके अनुसार अलग व्यवहार कर सकता है। यह आपको संबंधित objects के पूरे परिवार को एक समान मानने देता है, जबकि हर एक अपने ढंग से प्रतिक्रिया देता है।

दो प्रकार

  • Compile-time (static) — compiler द्वारा हल: function overloading और operator overloading।
  • Run-time (dynamic) — program चलते समय हल: inheritance के साथ virtual functions।

Compile-time polymorphism

Compiler signature से सही version चुनता है। एक ही नाम, अलग parameters:

C++
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
// add(2,3) → int version; add(2.5,1.5) → double version

function overloading और operator overloading देखें।

Run-time polymorphism

यहाँ चुनाव program चलते समय होता है। Base-class function को virtual चिह्नित किया जाता है, derived classes इसे override करती हैं, और base pointer के ज़रिए call करना वास्तविक object के आधार पर सही override चुनता है:

C++
class Shape {
public:
    virtual void draw() { cout << "Shape\n"; }   // virtual
};
class Circle : public Shape {
public:
    void draw() override { cout << "Circle\n"; }
};

एक run-time उदाहरण

C++
#include <iostream>
using namespace std;
class Shape { public: virtual void draw() { cout << "Shape\n"; } };
class Circle : public Shape { public: void draw() override { cout << "Circle\n"; } };
class Square : public Shape { public: void draw() override { cout << "Square\n"; } };

int main() {
    Shape *s;
    Circle c; Square sq;
    s = &c; s->draw();    // Circle
    s = &sq; s->draw();   // Square
    return 0;
}
Output:
Circle
Square

वही call s->draw() इस पर निर्भर अलग code चलाता है कि s किस पर इशारा करता है — यही run-time polymorphism है। यह virtual functions पर निर्भर है।

Compile-time बनाम run-time

Compile-timeRun-time
अन्य नामStaticDynamic
प्राप्तOverloadingVirtual functions
हलCompile time परचलते समय
गतितेज़थोड़ा overhead

आम गलतियाँ

  • Base function को virtual चिह्नित किए बिना run-time व्यवहार की उम्मीद करना।
  • Object (pointer/reference नहीं) के ज़रिए call करना, जो dynamic dispatch इस्तेमाल नहीं करता।
  • Polymorphic base class में virtual destructor भूलना।
  • Overloading (compile-time) को overriding (run-time) से भ्रमित करना।
🏋️ अभ्यास

एक Animal base बनाएँ जिसमें virtual speak() हो, और Dog तथा Cat derive करें। अलग animals के pointers एक array में रखें और उस पर loop करके speak() call करें — देखें हर एक अपनी आवाज़ print करता है।

सारांश

  • Polymorphism का अर्थ एक interface, कई रूप।
  • Compile-time polymorphism overloading इस्तेमाल करता है, compiler द्वारा हल।
  • Run-time polymorphism virtual functions और inheritance इस्तेमाल करता है।
  • Run-time dispatch को base pointer/reference और virtual चाहिए।
  • यह code को लचीला और नए types से विस्तार में आसान बनाता है।

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

C++ में polymorphism क्या है?
Polymorphism का अर्थ है "कई रूप" — एक interface की उसे इस्तेमाल करने वाले object के अनुसार अलग व्यवहार करने की क्षमता। C++ में यह एक ही function नाम या operator को अलग तरीकों से काम करने देता है, तो code संबंधित objects को एक समान मान सकता है जबकि हर एक अपने तरीके से प्रतिक्रिया देता है।
C++ में polymorphism के दो प्रकार क्या हैं?
Compile-time (static) polymorphism और run-time (dynamic) polymorphism होते हैं। Compile-time function overloading और operator overloading से प्राप्त होता है, compiler द्वारा हल; run-time virtual functions और inheritance से प्राप्त होता है, program चलते समय हल।
C++ में compile-time और run-time polymorphism में क्या अंतर है?
Compile-time polymorphism compiler द्वारा function signature के आधार पर तय होता है, overloading की तरह, और तेज़ है। Run-time polymorphism program चलते समय तय होता है, virtual functions और base-class pointers से, व्यवहार को उस क्षण object के वास्तविक type पर निर्भर होने देते हुए।
C++ में run-time polymorphism कैसे प्राप्त होता है?
Run-time polymorphism base-class function को virtual घोषित करके और derived classes में override करके प्राप्त होता है, फिर इसे base-class pointer या reference के ज़रिए call करके। सही override run time पर उस object के आधार पर चुना जाता है जिस पर pointer वास्तव में इशारा करता है।
C++ में polymorphism क्यों उपयोगी है?
Polymorphism आपको लचीला, विस्तार योग्य code लिखने देता है जो एक साझा interface के ज़रिए संबंधित types के पूरे परिवार के साथ काम करता है। आप base type इस्तेमाल करने वाले code को बदले बिना नई derived classes जोड़ सकते हैं, जो programs को बढ़ाना और बनाए रखना आसान रखता है।
← 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 दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.