Polymorphism in C++
What is Polymorphism?
Polymorphism means “many forms“. In C++, it allows a single function or operator to behave differently depending on the context in which it is used. For example, a function makeSound() can behave differently for a Dog object and a Cat object.
Types of Polymorphism
Compile-Time Polymorphism
This type of polymorphism is resolved during compile time. It includes:
Function Overloading: Defining multiple functions with the same name but different parameters.
Operator Overloading: Defining custom behavior for operators.
Example of Function Overloading:
#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 main() {
Math math;
cout << math.add(2, 3) << endl; // Output: 5
cout << math.add(2.5, 3.5) << endl; // Output: 6
return 0;
}
Runtime Polymorphism
This type of polymorphism is resolved during runtime. It is achieved using:
Virtual Functions: Functions in the base class that can be overridden in derived classes.
Function Overriding: Redefining a function in a derived class that is already defined in the base class.
Virtual Functions:
A virtual function is a function in a base class that is declared using the virtual keyword. It allows derived classes to provide their own implementation of the function.
Syntax
virtual returnType functionName(parameters) {
// Base class implementation
}
Function Overriding
Function overriding occurs when a derived class provides a specific implementation of a function that is already defined in its base class.
Rules for Function Overriding:
The function in the derived class must have the same name, return type, and parameters as the function in the base class.
The function in the base class must be declared as virtual.
Example:
#include <iostream>
using namespace std;
class Animal {
public:
virtual void makeSound() {
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void makeSound() override {
cout << "Dog barks" << endl;
}
};
class Cat : public Animal {
public:
void makeSound() override {
cout << "Cat meows" << endl;
}
};
int main() {
Animal* animal1 = new Dog();
Animal* animal2 = new Cat();
animal1->makeSound(); // Output: Dog barks
animal2->makeSound(); // Output: Cat meows
delete animal1;
delete animal2;
return 0;
}
Pure Virtual Functions and Abstract Classes
A pure virtual function is a virtual function with no implementation in the base class. It is declared using = 0.
Syntax
virtual returnType functionName(parameters) = 0;
A class containing at least one pure virtual function is called an abstract class. Abstract classes cannot be instantiated directly.
Example
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a Circle" << endl;
}
};
class Square : public Shape {
public:
void draw() override {
cout << "Drawing a Square" << endl;
}
};
int main() {
Shape* shape1 = new Circle();
Shape* shape2 = new Square();
shape1->draw(); // Output: Drawing a Circle
shape2->draw(); // Output: Drawing a Square
delete shape1;
delete shape2;
return 0;
}