Polymorphism in C++
Introduction
Polymorphism is one of the fundamental concepts of Object-Oriented Programming (OOP) in C++.
The word polymorphism comes from two Greek words:
- Poly = Many
- Morph = Forms
Therefore, polymorphism means "many forms."
In C++, polymorphism allows the same function, operator, or interface to behave differently in different situations.
Definition
Polymorphism is the ability of a single interface or function to have multiple forms or behaviors.
Simple Example
Polymorphism | ┌────────┴────────┐ ↓ ↓ Compile-Time Runtime Polymorphism Polymorphism
Types of Polymorphism
C++ mainly supports two types of polymorphism:
- Compile-Time Polymorphism
- Runtime Polymorphism
1. Compile-Time Polymorphism
Compile-time polymorphism is resolved by the compiler during compilation.
It is also called:
- Static Polymorphism
- Early Binding
Common techniques include:
- Function Overloading
- Operator Overloading
1.1 Function Overloading
Function overloading means defining multiple functions with the same name but different parameter lists.
Example
#include <iostream> using namespace std; class Calculator { public: int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }; int main() { Calculator c; cout << c.add(10, 20) << endl; cout << c.add(10, 20, 30) << endl; return 0; }
Output
30 60
The compiler determines which add() function should be called based on the number of arguments.
2. Operator Overloading
Operator overloading allows an operator such as +, -, *, or == to work with user-defined objects.
Example
#include <iostream> using namespace std; class Number { public: int value; Number(int v) { value = v; } Number operator+(Number n) { return Number(value + n.value); } }; int main() { Number n1(10); Number n2(20); Number n3 = n1 + n2; cout << "Sum = " << n3.value; return 0; }
Output
Sum = 30
Here, the + operator is overloaded to add two Number objects.
3. Runtime Polymorphism
Runtime polymorphism is resolved during program execution.
It is also called:
- Dynamic Polymorphism
- Late Binding
Runtime polymorphism is commonly achieved using:
- Inheritance
- Function Overriding
- Virtual Functions
3.1 Function Overriding
Function overriding occurs when a derived class provides its own implementation of a function already defined in the base class.
Example
#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; } }; int main() { Dog d; d.sound(); return 0; }
Output
Dog barks
4. Runtime Polymorphism Using Base Class Pointer
A base-class pointer can point to an object of a derived class.
A virtual function ensures that the correct overridden function is called at runtime.
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; } }; class Cat : public Animal { public: void sound() override { cout << "Cat meows" << endl; } }; int main() { Animal* animal; Dog d; Cat c; animal = &d; animal->sound(); animal = &c; animal->sound(); return 0; }
Output
Dog barks Cat meows
The same statement:
animal->sound();
produces different results depending on the object.
5. Virtual Function
A virtual function is a member function declared using the virtual keyword in the base class.
Syntax
class Base { public: virtual void display() { // Code } };
A derived class can override the function.
class Derived : public Base { public: void display() override { // Different implementation } };
6. Pure Virtual Function
A pure virtual function is declared using = 0.
Syntax
class Animal { public: virtual void sound() = 0; };
A class containing at least one pure virtual function is called an abstract class.
Example
#include <iostream> using namespace std; class Animal { public: virtual void sound() = 0; }; class Dog : public Animal { public: void sound() override { cout << "Dog barks" << endl; } }; int main() { Dog d; d.sound(); return 0; }
Output
Dog barks
7. Compile-Time vs Runtime Polymorphism
| Compile-Time Polymorphism | Runtime Polymorphism |
|---|---|
| Resolved during compilation. | Resolved during execution. |
| Also called static polymorphism. | Also called dynamic polymorphism. |
| Uses early binding. | Uses late binding. |
| Commonly uses function overloading. | Commonly uses function overriding. |
| Operator overloading is another example. | Virtual functions are commonly used. |
| Generally does not require inheritance. | Usually involves inheritance. |
| Faster dispatch because the call is determined earlier. | Supports dynamic behavior through virtual dispatch. |
8. Real-Life Example
Consider a payment system.
Different payment methods can have a common function:
Payment | ┌────────┼────────┐ ↓ ↓ ↓ Cash Card Online
Each payment method can implement pay() differently.
Example
#include <iostream> using namespace std; class Payment { public: virtual void pay() { cout << "Making payment" << endl; } }; class Cash : public Payment { public: void pay() override { cout << "Payment using cash" << endl; } }; class Card : public Payment { public: void pay() override { cout << "Payment using card" << endl; } }; int main() { Payment* p; Cash cash; Card card; p = &cash; p->pay(); p = &card; p->pay(); return 0; }
Output
Payment using cash Payment using card
The same pay() interface produces different behavior.
9. Advantages of Polymorphism
1. Flexibility
The same interface can work with different types of objects.
2. Code Reusability
Common interfaces and functions can be reused.
3. Easy Maintenance
New derived classes can be added without changing the common interface.
4. Supports Extensibility
Programs can be extended with new implementations.
5. Reduces Complexity
The user can work with a common interface rather than dealing with every implementation separately.
10. Key Points
- Polymorphism means many forms.
- It is an important OOP concept.
- C++ supports compile-time and runtime polymorphism.
- Function overloading is compile-time polymorphism.
- Operator overloading is compile-time polymorphism.
- Function overriding with virtual functions is runtime polymorphism.
- Runtime polymorphism commonly uses inheritance.
- A pure virtual function uses
= 0. - A class with a pure virtual function is an abstract class.
Summary
Polymorphism | ┌──────────┴──────────┐ ↓ ↓ Compile-Time Runtime | | ┌──────┴──────┐ ┌─────┴─────┐ ↓ ↓ ↓ ↓ Function Operator Overriding Virtual Overloading Overloading Function Function
Simple Definition
Polymorphism = One interface or function with multiple forms or behaviors.