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.

Frequently Asked Questions

What is polymorphism in C++?
Polymorphism means "many forms" — the ability of one interface to behave differently depending on the object using it. In C++ it lets the same function name or operator act in different ways, so code can treat related objects uniformly while each responds in its own way.
What are the two types of polymorphism in C++?
There are compile-time (static) polymorphism and run-time (dynamic) polymorphism. Compile-time is achieved with function overloading and operator overloading, resolved by the compiler; run-time is achieved with virtual functions and inheritance, resolved while the program runs.
What is the difference between compile-time and run-time polymorphism in C++?
Compile-time polymorphism is decided by the compiler based on the function signature, as with overloading, and is fast. Run-time polymorphism is decided while the program runs, using virtual functions and base-class pointers, allowing behaviour to depend on the actual object type at that moment.
How is run-time polymorphism achieved in C++?
Run-time polymorphism is achieved by declaring a base-class function virtual and overriding it in derived classes, then calling it through a base-class pointer or reference. The correct override is chosen at run time based on the object the pointer actually points to.
Why is polymorphism useful in C++?
Polymorphism lets you write flexible, extensible code that works with a whole family of related types through a common interface. You can add new derived classes without changing the code that uses the base type, which keeps programs easier to grow and maintain.

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 से विस्तार में आसान बनाता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.