
Polymorphism in C++ | OOP Tutorial with Real-Life Examples
Polymorphism is one of the four core concepts of Object-Oriented Programming (OOP), along with encapsulation, inheritance, and abstraction. The term polymorphism is derived from two Greek words; poly (many) and morph (forms). In programming, it means one name, many forms. In simple words, polymorphism allows a single function, operator, or object to behave differently based on the context. This makes our code flexible, reusable, and easy to extend.
Understanding Polymorphism
Think about the word “drive”.
A car drives, a bike drives, and a bus drives. The action “drive” is the same, but the behavior is different depending on the object. This is polymorphism; the same operation acting differently for different entities.
Similarly, in a company, the manager, designer, and developer all respond to the command “Work!”, but each performs a different task. This is how polymorphism works in real life and also in programming.
What Is Polymorphism in C++
Polymorphism in C++ is the ability of a function, operator, or object to take many forms. It allows the same function name to perform different tasks depending on the type of object or the number of parameters. Polymorphism provides the foundation for dynamic behavior in software, allowing us to write programs that can adapt at runtime.

Types of Polymorphism in C++
C++ supports two main types of polymorphism:
| Type | Description | Example |
|---|---|---|
| Compile-Time Polymorphism (Static) | Decided by the compiler during compilation. | Function Overloading, Operator Overloading |
| Run-Time Polymorphism (Dynamic) | Decided during program execution. | Virtual Functions / Function Overriding |
Compile-Time (Static) Polymorphism
Compile-time polymorphism means the decision about which function to call is made before the program runs. This is achieved using function overloading and operator overloading.

Function Overloading Example
#include <iostream>
using namespace std;
class Math {
public:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
};
int main() {
Math m;
cout << "Sum of two integers: " << m.add(2, 3) << endl;
cout << "Sum of two doubles: " << m.add(2.5, 3.8) << endl;
cout << "Sum of three integers: " << m.add(1, 2, 3) << endl;
return 0;
}
Operator Overloading Example

#include <iostream>
using namespace std;
class Plot {
int length, width;
public:
// Constructor
Plot(int l = 0, int w = 0) {
length = l;
width = w;
}
// Operator overloading (+)
Plot operator + (Plot p) {
Plot temp;
temp.length = length + p.length;
temp.width = width + p.width;
return temp;
}
// Display function
void display() {
cout << "Plot: " << length << " x " << width << endl;
cout << "Area: " << length * width << endl;
}
};
int main() {
// Create two plots
Plot p1(10, 5);
Plot p2(6, 3);
// Add plots using overloaded operator
Plot p3 = p1 + p2;
// Display result
cout << "Combined Plot:\n";
p3.display();
return 0;
}
Run-Time (Dynamic) Polymorphism
Run-time polymorphism means the function that gets executed is determined at runtime, not compile time. This is achieved through function overriding using virtual functions and pointers/references to base classes.
Function Overriding Example
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks" << endl;
}
};
class Cat : public Animal {
public:
void sound() {
cout << "Cat meows" << endl;
}
};
int main() {
Animal* a; // Base class pointer
Dog d;
Cat c;
a = &d;
a->sound(); // Calls Dog’s sound()
a = &c;
a->sound(); // Calls Cat’s sound()
return 0;
}
Why Do We Use Pointers in Polymorphism?
When we call a virtual function using an object (e.g., obj.sound()), the compiler already knows its type, so it doesn’t check at runtime. But when we call it through a base class pointer or reference, C++ checks which object the pointer actually refers to and calls the correct version. Hence, pointers or references are used to achieve real dynamic behavior.
Difference Between Function Overloading and Function Overriding
| Feature | Function Overloading | Function Overriding |
|---|---|---|
| Type | Compile-time polymorphism | Run-time polymorphism |
| Parameters | Must differ | Must be same |
| Inheritance | Not required | Required |
| Keyword | No keyword needed | Uses virtual keyword |
| Execution Time | Decided at compile time | Decided at runtime |
Real-World Example: Draw Function
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {
cout << "Drawing a shape" << endl;
}
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle" << endl;
}
};
class Rectangle : public Shape {
public:
void draw() {
cout << "Drawing a rectangle" << endl;
}
};
int main() {
Shape* shape;
Circle c;
Rectangle r;
shape = &c;
shape->draw();
shape = &r;
shape->draw();
return 0;
}
Output:
Drawing a circle
Drawing a rectangle
Advantages of Polymorphism
- Code Reusability: Once a base class or function is written, it can be reused by derived classes without rewriting code. This reduces redundancy and saves development time.
- Flexibility: Polymorphism allows one interface to handle multiple types of behavior. For example, a single pointer of a base class can call different functions depending on which derived object it points to.
- Scalability: New classes and behaviors can be added easily without modifying existing code. This makes software easier to extend and maintain.
- Readability: Code becomes more organized and easier to understand since similar actions are grouped under the same interface or function name.
Key Points to Remember
- Polymorphism = One interface, many implementations.
- Function Overloading and Operator Overloading → Compile-time polymorphism.
- Function Overriding using
virtual→ Run-time polymorphism. - Use base class pointers or references to achieve dynamic behavior.
- Destructors in base classes should be declared
virtualto ensure proper cleanup.
| Aspect | Compile-Time | Run-Time |
|---|---|---|
| How it happens | Function/Operator Overloading | Virtual Functions |
| Decision Time | Before program runs | During program execution |
| Keyword | None | virtual |
| Inheritance needed | No | Yes |
| Binding Type | Early binding | Late binding |
8 thoughts on “Polymorphism in C++ | OOP Tutorial with Real-Life Examples”
Thanks for sharing the information with us.
The app version is so much better than the browser. It is fast, secure, and very convenient for gaming on the go via superjililoginapp.
Straight to the point and no nonsense. The registration was fast and I was playing in minutes. I really appreciate the efficiency of vnnohu001
wild truck demo online wild truck bonus
свежие новости на сегодня последние новости алматы
Увлекаешься охотой? продажа товаров для охоты предоставляет большой ассортимент для охоты, путешествий и outdoor-активностей. В каталоге собраны специализированное снаряжение, одежда, обувь, товары для наблюдения, полезные аксессуары и товары для активного отдыха. Подбирайте нужное снаряжение, сравнивайте характеристики и покупайте продукцию через сайт. Понятная навигация поможет собрать комплект для охоты или отдыха на природе.
Выбрать онлайн купить цветы Омск можно на нашем сайте. Подбирайте авторские букеты для любимых. В каталоге представлены подарочные наборы. Оформляйте заказ и делайте приятные сюрпризы к важной дате.
Сериал в сети смотреть сериал сверхъестественное с хорошей картинкой можно прямо на сайте. Полная коллекция сезонов и эпизодов доступны для просмотра. Наблюдайте за историей братьев Винчестеров, которые защищают людей от сверхъестественных угроз. На протяжении сериала герои сталкиваются с демонами, ангелами, духами и другими существами. Ангел Кастиэль присоединяется к героям, а новые противники угрожают героям. Выбирайте интересующий эпизод и погружайтесь в атмосферу мистического сериала.