Polymorphism allows the same function or operator to perform different operations depending on its arguments or the object involved. It is an important concept of Object-Oriented Programming (OOP).
- Compile-time polymorphism uses function and operator overloading.
- Runtime polymorphism uses virtual functions and function overriding.
#include <iostream>
using namespace std;
class Animal {
public:
// Function to display animal sound
void sound() {
cout << "Animal makes a sound";
}
};
class Dog : public Animal {
public:
// Overriding the sound function
void sound() {
cout << "Dog barks";
}
};
int main() {
Dog d;
// Calling the Dog class function
d.sound();
return 0;
}
Output
Dog barks
Explanation: The Dog class provides its own implementation of the sound() function. When d.sound() is called, the Dog class version is executed, showing how the same function name can have different behavior in different classes.
Types of Polymorphism
Polymorphism is mainly divided into two types

1. Compile-Time Polymorphism
Compile-time polymorphism, also called static polymorphism, allows the same function name or operator to work in different ways based on the arguments or operands. The compiler decides which function or operation to use during compilation.
- Function overloading: Allows multiple functions to share the same name with different parameter lists. The compiler selects the appropriate overload based on the arguments.
- Operator overloading: Allows us to use existing operators, such as +, -, and *, with user-defined data types. It defines how these operators work with objects of a class.
#include <iostream>
using namespace std;
// Function overloading
void show(int x) {
cout << "Integer: " << x << '\n';
}
void show(double x) {
cout << "Double: " << x << '\n';
}
struct Number {
int value;
// Operator overloading
Number operator+(const Number& other) const {
return {value + other.value};
}
};
int main() {
show(5);
show(2.5);
Number a{2}, b{3};
cout << "Sum: " << (a + b).value;
return 0;
}
Output
Integer: 5 Double: 2.5 Sum: 5
Explanation: The compiler selects the appropriate show() function based on the argument type. The overloaded + operator adds the values of two Number objects.
2. Runtime Polymorphism
Runtime polymorphism, also called dynamic polymorphism, allows a virtual function call to select an implementation based on the object's actual type.It is achieved through function overriding and virtual dispatch using base-class pointers or references.
Function overriding: Occurs when a derived class provides its own implementation of a virtual function declared in the base class.
Example: Different animals represent polymorphism, where the same method speak() produces different outputs such as Bark, Meow, and Moo depending on the object calling the method.
#include <iostream>
using namespace std;
class Animal {
public:
// Virtual function
virtual void sound() const {
cout << "Animal sound\n";
}
virtual ~Animal() = default;
};
class Dog : public Animal {
public:
// Override base class function
void sound() const override {
cout << "Dog barks\n";
}
};
class Cat : public Animal {
public:
void sound() const override {
cout << "Cat meows\n";
}
};
int main() {
Dog dog;
Cat cat;
// Base class pointer to Dog object
Animal* animal = &dog;
animal->sound();
// Reassign pointer to Cat object
animal = &cat;
animal->sound();
return 0;
}
Output
Dog barks Cat meows
Explanation: The base-class pointer animal first points to a Dog object and then to a Cat object. Because sound() is virtual, the corresponding derived-class implementation is selected at runtime.

Advantages of Polymorphism
Polymorphism makes programs more flexible and easier to maintain.
- Allows different classes to provide implementations of a common interface.
- Reduces the need for separate code paths for each derived type.
- Makes applications easier to extend with new derived classes.
Related Article: Compile Time And Run Time Polymorphism