Polymorphism means "many forms.
"
In programming, it refers to the ability of a function, method, or object to take different forms
depending on the context.
It allows the same interface (method name, operator, or class) to behave differently based on
the input or object that is using it.
This makes code more flexible, reusable, and easier to maintain.
Types of Polymorphism
1. Compile-time polymorphism (Static binding)
o Also called method overloading or operator overloading.
o Decided at compile time.
o Example: A function with the same name but different parameters.
2. Runtime polymorphism (Dynamic binding)
o Also called method overriding.
o Decided at runtime.
o Example: A subclass provides its own version of a method defined in the parent
class.
1. Function Overloading (Compile-time polymorphism)
Same function name, but different parameter types/numbers.
#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 << [Link](2, 3) << endl; // Calls int version
cout << [Link](2.5, 3.5) << endl; // Calls double version
cout << [Link](1, 2, 3) << endl; // Calls 3-parameter version
return 0;
}
2. Operator Overloading (Compile-time polymorphism)
Operators can work differently for user-defined types.
#include <iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
// Overload '+' operator
Complex operator + (const Complex &obj) {
return Complex(real + [Link], imag + [Link]);
}
};
int main() {
Complex c1(3, 4), c2(1, 2);
Complex c3 = c1 + c2; // Calls overloaded +
cout << [Link] << " + " << [Link] << "i" << endl; // 4 + 6i
return 0;
}
3. Virtual Functions (Runtime polymorphism)
Same function call behaves differently based on the object type at runtime.
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { // Virtual function
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* a1 = new Dog();
Animal* a2 = new Cat();
a1->sound(); // Dog barks
a2->sound(); // Cat meows
delete a1;
delete a2;
return 0;
}
Here, the method sound() works differently depending on the object.
3. Abstract Classes (Pure Virtual Functions)
Used when you want to enforce polymorphism.
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function (abstract)
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};
class Square : public Shape {
public:
void draw() override {
cout << "Drawing Square" << endl;
}
};
int main() {
Shape* s1 = new Circle();
Shape* s2 = new Square();
s1->draw(); // Drawing Circle
s2->draw(); // Drawing Square
delete s1;
delete s2;
return 0;
}
draw() is polymorphic across different shapes.
Task:
I. Write a C++ program that creates a class Calculator with an overloaded function
multiply ():
One version should multiply two integers.
Another version should multiply two doubles.
Call both versions in main ().
II. Create a class Math Operation with an overloaded function area ():
One function takes one argument (radius) and calculates the area of a circle.
Another function takes two arguments (length, width) and calculates the area of
a rectangle.
III. make a class Printer with an overloaded function print ():
One function prints an integer.
One function prints a string.
One function prints a double.
IV. Create a class Bank Account with a function interest Rate ().
Override it in Saving Account and Current Account
Demonstrate overriding using a virtual function.
V. Make a base class Vehicle with a function fuel Type ().
Override the function in Car (return "Petrol") and Electric Car (return "Electric").
Call the overridden methods using base class references.
to return different interest rates.
VI. Define a base class Employee with a function calculate Salary().
Override it in Manager and Developer classes to calculate salary differently.
Call the function using polymorphism.