Polymorphism in C++
Polymorphism is a key concept in Object-Oriented Programming (OOP) that allows one interface to be used for a general class of actions. In simple terms, polymorphism means "many forms". In C++, polymorphism allows objects of different types to be treated as objects of a common base type, providing flexibility and reusability in code. It is one of the four fundamental OOP concepts, alongside encapsulation, inheritance, and abstraction.
There are two main types of polymorphism in C++:
Compile-time polymorphism occurs when the method to be called is resolved at compile time. The most common ways to achieve compile-time polymorphism in C++ are:
Function overloading allows multiple functions with the same name but different parameters (either in number or type) to exist in the same scope. The appropriate function is selected based on the number and types of arguments passed.
Example of Function Overloading:
#include <iostream>
using namespace std;
class Display {
public:
void show(int i) {
cout << "Integer: " << i << endl;
}
void show(double d) {
cout << "Double: " << d << endl;
}
void show(string s) {
cout << "String: " << s << endl;
}
};
int main() {
Display obj;
obj.show(10); // Calls show(int)
obj.show(3.14); // Calls show(double)
obj.show("Hello"); // Calls show(string)
return 0;
}
show function is overloaded with three different versions: one that accepts an integer, one that accepts a double, and one that accepts a string.show function to call based on the argument passed. This decision happens at compile time, so it's compile-time polymorphism.Operator overloading allows you to define custom behavior for operators (like +, -, *, etc.) when they are used with objects of a class.
Example of Operator Overloading:
#include <iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex() : real(0), imag(0) {}
// Overload the "+" operator
Complex operator + (const Complex& other) {
Complex temp;
temp.real = real + other.real;
temp.imag = imag + other.imag;
return temp;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1, c2, result;
c1.real = 3; c1.imag = 4;
c2.real = 1; c2.imag = 2;
result = c1 + c2; // Calls the overloaded "+" operator
result.display(); // Output: 4 + 6i
return 0;
}
operator + is overloaded to add two Complex numbers. When you use the + operator between c1 and c2, the overloaded function is called.Run-time polymorphism occurs when the method to be called is determined at runtime. This type of polymorphism is achieved through inheritance and virtual functions.
Run-time polymorphism allows you to invoke derived class methods through a base class pointer or reference. It is also called method overriding and is commonly used with virtual functions.
A virtual function is a function in the base class that is overridden in the derived class. The key feature of a virtual function is that the type of the object being referred to (rather than the type of the pointer or reference) determines which function is called.
virtual in the base class.When you use a base class pointer or reference to call a virtual function, the appropriate derived class function is called based on the type of the object, not the type of the pointer.
#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* animalPtr;
Dog dog;
Cat cat;
animalPtr = &dog;
animalPtr->sound(); // Calls Dog's sound() function
animalPtr = &cat;
animalPtr->sound(); // Calls Cat's sound() function
return 0;
}
sound() function is virtual in the Animal base class. It is overridden in the Dog and Cat derived classes.Animal* pointer is used to call the sound() function. At runtime, the type of the object (dog or cat) determines which version of sound() is called.animalPtr points to a Dog object, the Dog's version of sound() is called. Similarly, when it points to a Cat object, the Cat's version of sound() is called.Virtual Function:
virtual keyword in the base class.Function Overriding:
Function Overloading (Compile-time Polymorphism):
Operator Overloading (Compile-time Polymorphism):
+, -, *, etc., to perform operations on user-defined types.Code Reusability:
Flexibility and Maintainability:
Improved Design:
Polymorphism is a powerful feature of OOP that allows for the creation of flexible and reusable code.
Open this section to load past papers