Polymorphism in C++
Written and reviewed by Gagan Bhardwaj · 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:
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 versionSee 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:
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
#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;
}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-time | Run-time | |
|---|---|---|
| Also called | Static | Dynamic |
| Achieved by | Overloading | Virtual functions |
| Resolved | At compile time | While running |
| Speed | Faster | Slight 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).
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:
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 versionfunction overloading और operator overloading देखें।
Run-time polymorphism
यहाँ चुनाव program चलते समय होता है। Base-class function को virtual चिह्नित किया जाता है, derived classes इसे override करती हैं, और base pointer के ज़रिए call करना वास्तविक object के आधार पर सही override चुनता है:
class Shape {
public:
virtual void draw() { cout << "Shape\n"; } // virtual
};
class Circle : public Shape {
public:
void draw() override { cout << "Circle\n"; }
};एक run-time उदाहरण
#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;
}Circle
Square
वही call s->draw() इस पर निर्भर अलग code चलाता है कि s किस पर इशारा करता है — यही run-time polymorphism है। यह virtual functions पर निर्भर है।
Compile-time बनाम run-time
| Compile-time | Run-time | |
|---|---|---|
| अन्य नाम | Static | Dynamic |
| प्राप्त | Overloading | Virtual 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 क्या है?
C++ में polymorphism के दो प्रकार क्या हैं?
C++ में compile-time और run-time polymorphism में क्या अंतर है?
C++ में run-time polymorphism कैसे प्राप्त होता है?
virtual घोषित करके और derived classes में override करके प्राप्त होता है, फिर इसे base-class pointer या reference के ज़रिए call करके। सही override run time पर उस object के आधार पर चुना जाता है जिस पर pointer वास्तव में इशारा करता है।