Polymorphism in C++
In this lesson: Function overloading, operator overloading and virtual functions — both types of polymorphism in C++ with code. In Hindi & English.
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.
Frequently Asked Questions
What is polymorphism in C++?
What are the two types of polymorphism in C++?
What is the difference between compile-time and run-time polymorphism in C++?
How is run-time polymorphism achieved in C++?
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.