Polymorphism is one of the core concepts of Object-Oriented Programming (OOP) in C++. It allows the same function, method, or operator to perform different tasks depending on the situation.
What is Polymorphism?
The word polymorphism means âmany formsâ.
In C++, polymorphism allows one interface to behave differently for different objects or data types.
Why Use Polymorphism?
Polymorphism is useful because it:
- Increases code flexibility
- Reduces code complexity
- Improves code reusability
- Supports scalable applications
- Helps in real-world modeling
Types of Polymorphism in C++
C++ mainly supports two types of polymorphism:
- Compile-Time Polymorphism
- Run-Time Polymorphism
1. Compile-Time Polymorphism
Compile-time polymorphism is achieved during compilation.
It is also called:
- Early Binding
- Static Polymorphism
Achieved Using
- Function Overloading
- Operator Overloading
Function Overloading Example
#include <iostream>
using namespace std;
class Math {
public:
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
};
int main() {
Math obj;
cout << obj.add(2, 3) << endl;
cout << obj.add(2, 3, 4) << endl;
return 0;
}
Output
5
9
How Compile-Time Polymorphism Works
- Same function name
- Different parameters
- Compiler decides which function to call
2. Run-Time Polymorphism
Run-time polymorphism is achieved during program execution.
It is also called:
- Late Binding
- Dynamic Polymorphism
Achieved Using
- Function Overriding
- Virtual Functions
Function Overriding Example
#include <iostream>
using namespace std;
class Animal {
public:
void sound() {
cout << "Animal sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Dog barks" << endl;
}
};
int main() {
Dog d;
d.sound();
return 0;
}
Output
Dog barks
Virtual Function Example
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};
int main() {
Animal *a;
Dog d;
a = &d;
a->sound();
return 0;
}
Output
Dog barks
How Run-Time Polymorphism Works
- Base class pointer is used
- Virtual function enables dynamic binding
- Function call is decided at runtime
Difference Between Compile-Time and Run-Time Polymorphism
| Compile-Time Polymorphism | Run-Time Polymorphism |
|---|---|
| Decided during compilation | Decided during execution |
| Faster execution | Slightly slower |
| Function overloading | Function overriding |
| Static binding | Dynamic binding |
Real-Life Example
Think of a person:
- Teacher at school
- Parent at home
- Employee at office
Same person behaves differently in different situations.
Advantages of Polymorphism
- Flexible code
- Easier maintenance
- Better scalability
- Improved readability
- Supports reusable code
Why Polymorphism is Important
Polymorphism is important because it:
- Simplifies complex systems
- Improves software design
- Supports dynamic behavior
- Enhances object-oriented programming
Applications of Polymorphism
Polymorphism is widely used in:
- Game development
- GUI applications
- Software frameworks
- Banking systems
- Real-time applications
Conclusion
Polymorphism in C++ allows the same interface to perform different actions based on context. Through function overloading, overriding, and virtual functions, polymorphism improves flexibility, scalability, and reusability in Object-Oriented Programming.