Polymorphism in C++ – Complete Tutorial
Last Updated on: 16th Dec 2025 22:22:46 PM
Polymorphism is one of the core principles of Object Oriented Programming.
The word polymorphism means “many forms”.
In C++, polymorphism allows the same function name or operation to behave differently depending on the situation.
Polymorphism makes programs flexible, scalable, and easy to extend, especially when working with inheritance.
Polymorphism
Polymorphism is the ability of a single function, method, or operator to perform different actions based on the object that calls it.
Real-life example (Basic Idea)
A payment method behaves differently:
-
Cash payment
-
Card payment
-
UPI payment
The action is the same (pay), but the implementation is different.
Types of Polymorphism in C++
C++ supports two major types of polymorphism:
-
Compile-time Polymorphism
-
Run-time Polymorphism
1. Compile-time Polymorphism
Compile-time polymorphism is resolved during compilation. It does not depend on object type at runtime.
Types
-
Function Overloading
-
Operator Overloading
1.1 Function Overloading
Function overloading allows multiple functions with the same name but different parameter lists. The compiler decides which function to call based on arguments.
Real-life example
A calculator performs addition:
-
Add two integers
-
Add two decimal values
Program
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
};
int main() {
Calculator c;
cout << c.add(10, 20) << endl;
cout << c.add(5.5f, 4.5f) << endl;
return 0;
}
Output
30
10
Short Explanation of Output
The compiler selects the appropriate add() function based on the data type of arguments passed.
1.2 Operator Overloading
Operator overloading allows operators to work with user-defined data types.
Real-life example
Adding two distance values measured in meters.
Program
#include <iostream>
using namespace std;
class Distance {
public:
int meters;
Distance(int m) {
meters = m;
}
Distance operator+(Distance d) {
return Distance(meters + d.meters);
}
};
int main() {
Distance d1(10);
Distance d2(25);
Distance d3 = d1 + d2;
cout << d3.meters << endl;
return 0;
}
Output
35
Short Explanation of Output
The + operator is overloaded to add distance values instead of primitive integers.
2. Run-time Polymorphism
Run-time polymorphism is resolved during program execution.
It is achieved using inheritance and virtual functions.
2.1 Function Overriding
Function overriding occurs when a derived class provides a new definition for a function already defined in the base class.
Real-life example
-
A general delivery service
-
A same-day delivery service with faster processing
Program
#include <iostream>
using namespace std;
class Delivery {
public:
virtual void deliver() {
cout << "Standard delivery in 5 days" << endl;
}
};
class ExpressDelivery : public Delivery {
public:
void deliver() {
cout << "Express delivery in 1 day" << endl;
}
};
int main() {
Delivery* d;
ExpressDelivery e;
d = &e;
d->deliver();
return 0;
}
Output
Express delivery in 1 day
Short Explanation of Output
The base class pointer calls the derived class function due to the use of the virtual keyword.
2.2 Virtual Functions
A virtual function ensures that the correct function is called at runtime, based on the object type, not pointer type.
Real-life example
Different shapes calculate area differently.
Program
#include <iostream>
using namespace std;
class Shape {
public:
virtual void area() {
cout << "Area formula not defined" << endl;
}
};
class Rectangle : public Shape {
public:
void area() {
cout << "Area = length × breadth" << endl;
}
};
class Circle : public Shape {
public:
void area() {
cout << "Area = π × r × r" << endl;
}
};
int main() {
Shape* s;
Rectangle r;
Circle c;
s = &r;
s->area();
s = &c;
s->area();
return 0;
}
Output
Area = length × breadth
Area = π × r × r
Short Explanation of Output
The correct area() function is executed based on the object assigned to the base class pointer.
Advantages of Polymorphism
-
Improves code flexibility
-
Supports scalability
-
Reduces code duplication
-
Makes programs easier to extend
Disadvantages of Polymorphism
-
Slight performance overhead
-
Increased complexity
-
Requires careful design
Summary
-
Polymorphism means one interface, many implementations
-
C++ supports compile-time and run-time polymorphism
-
Function and operator overloading are compile-time techniques
-
Virtual functions enable run-time behavior
-
Polymorphism closely models real-world behavior
Keep practicing — you're doing amazing!
Happy Coding! ![]()