-
Hajipur, Bihar, 844101
Polymorphism is one of the core principles of Object-Oriented Programming (OOP) in C++. The term polymorphism means “many forms.” In C++, it allows one interface to be used for different data types or objects, meaning the same function name or operator can perform different tasks based on the context.
In simpler words, polymorphism lets you write flexible and reusable code where a single function behaves differently for different classes. It plays a key role in achieving runtime behavior in C++ programs.
C++ supports two main types of polymorphism:
Compile-Time Polymorphism (Static Polymorphism)
Run-Time Polymorphism (Dynamic Polymorphism)
Let’s understand both in detail.
Compile-time polymorphism is resolved by the compiler before the program runs. It is achieved using:
Function Overloading
Operator Overloading
#include <iostream>
using namespace std;
class PrintData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double d) {
cout << "Printing double: " << d << endl;
}
void print(string s) {
cout << "Printing string: " << s << endl;
}
};
int main() {
PrintData p;
p.print(10);
p.print(10.5);
p.print("Hello World");
return 0;
}
The function print() has the same name but different parameter types.
The compiler decides which version to call based on the arguments passed — this is compile-time polymorphism.
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
Complex operator + (Complex const &obj) {
Complex result;
result.real = real + obj.real;
result.imag = imag + obj.imag;
return result;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
c3.display();
return 0;
}
Here, the + operator is overloaded to work with user-defined objects (Complex numbers). This is another form of compile-time polymorphism.
Run-time polymorphism occurs when a call to an overridden function is resolved at runtime rather than compile-time.
It is implemented using inheritance and virtual functions.
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};
class Cat : public Animal {
public:
void sound() override {
cout << "Cat meows" << endl;
}
};
int main() {
Animal* a;
Dog d;
Cat c;
a = &d;
a->sound();
a = &c;
a->sound();
return 0;
}
The function sound() is declared as virtual in the base class Animal.
At runtime, depending on the object being referred to (Dog or Cat), the appropriate version of sound() is executed.
This allows different classes to respond differently to the same function call — the essence of polymorphism.
| Feature | Compile-Time Polymorphism | Run-Time Polymorphism |
|---|---|---|
| Binding | Early (at compile time) | Late (at runtime) |
| Methods Used | Function & Operator Overloading | Virtual Functions |
| Inheritance Required | No | Yes |
| Flexibility | Less flexible | More flexible |
| Performance | Faster | Slightly slower |
A virtual function allows the derived class to override it. It provides flexibility for runtime decisions.
A pure virtual function is a virtual function with no definition in the base class. It forces derived classes to provide their own implementation.
A class containing a pure virtual function is called an abstract class.
#include <iostream>
using namespace std;
class Shape {
public:
virtual void area() = 0; // Pure virtual function
};
class Circle : public Shape {
private:
int radius;
public:
Circle(int r) {
radius = r;
}
void area() {
cout << "Area of circle: " << 3.14 * radius * radius << endl;
}
};
int main() {
Shape* s;
Circle c(5);
s = &c;
s->area();
return 0;
}
In this example:
The base class Shape cannot be instantiated because it’s abstract.
The derived class Circle provides its own definition of area().
Code Reusability: You can reuse the same function names in different contexts.
Flexibility: Allows writing more general and adaptable code.
Scalability: Easy to extend without modifying existing code.
Maintainability: Reduces redundancy and simplifies debugging.
Dynamic Behavior: Enables runtime decision-making for object behavior.
Consider a Payment system in an e-commerce application:
A base class Payment defines a method makePayment().
Derived classes CreditCard, UPI, and Wallet override makePayment() to provide their own implementation.
The program can handle all payment types through a common interface, using runtime polymorphism.
Polymorphism in C++ allows the same function or operator to perform different actions depending on the object it’s working with.
Compile-time polymorphism is achieved through function or operator overloading.
Run-time polymorphism is implemented using inheritance and virtual functions.
This concept makes code more flexible, maintainable, and scalable — essential for large object-oriented applications.
Create a C++ program that demonstrates function overloading by creating multiple calculate() functions to handle addition of integers, floats, and strings.
Write a program that uses operator overloading to add two objects of a Distance class (in kilometers and meters).
Implement runtime polymorphism using a base class Vehicle and derived classes Car and Bike, each having their own version of a displayInfo() function.
Write a program using virtual functions where a base class Shape has a draw() method, and derived classes Circle and Rectangle override it.
Create an abstract class Employee with a pure virtual function calculateSalary(). Derive two classes FullTime and PartTime and implement the function differently in both.
Implement runtime polymorphism for a Payment system where different classes like CreditCard, UPI, and NetBanking override a base class function makePayment().
Write a program to show function overriding in a base class Animal and derived classes Dog and Cat with a function sound().
Develop a program that demonstrates base class pointers calling derived class functions using virtual functions.
Implement a pure virtual function in a base class Appliance and define it in derived classes WashingMachine and Refrigerator.
Write a C++ program that demonstrates both compile-time and run-time polymorphism in a single code using function overloading and virtual functions.