Polymorphism in C++
0 120
What is Polymorphism?
Polymorphism is derived from two Greek words: "poly" (meaning many) and "morph" (meaning forms). So, polymorphism means many forms. In C++, polymorphism allows one function, class, or object to take many different forms depending on the context.
Types of Polymorphism
Polymorphism can be broadly categorized into two types:
- Compile-time Polymorphism: Also known as static polymorphism, this type occurs when the function or operator is resolved during compile time. The most common examples of compile-time polymorphism are function overloading and operator overloading.
- Runtime Polymorphism: Also known as dynamic polymorphism, this type occurs when the function call is resolved during runtime. This is typically implemented using virtual functions and inheritance.
Compile-time Polymorphism
Compile-time polymorphism is achieved using function overloading and operator overloading. In function overloading, multiple functions can have the same name but different parameter lists. Similarly, in operator overloading, we can define how operators like +, -, *, etc., behave with user-defined types.
Function Overloading Example
#include <iostream>
using namespace std;
class Print {
public:
void display(int i) {
cout << "Integer: " << i << endl;
}
void display(double d) {
cout << "Double: " << d << endl;
}
};
int main() {
Print p;
p.display(5); // Calls the function with int
p.display(5.5); // Calls the function with double
return 0;
}
In this example, the function display
is overloaded to accept both integer and double values.
Runtime Polymorphism
Runtime polymorphism is achieved using virtual functions. A virtual function is a function that is declared in the base class and is overridden in the derived class. At runtime, the function that is called is determined by the type of object pointed to by the base class pointer.
Virtual Function Example
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base class show function called" << endl;
}
};
class Derived : public Base {
public:
void show() override {
cout << "Derived class show function called" << endl;
}
};
int main() {
Base* bptr;
Derived d;
bptr = &d;
bptr->show(); // Calls Derived class function
return 0;
}
In this example, even though the pointer is of type Base*
, the function show()
of the Derived
class is called, demonstrating runtime polymorphism.
Advantages of Polymorphism
- Code Reusability: Polymorphism allows you to write more general and reusable code.
- Extensibility: It is easier to add new functionalities without changing the existing code.
- Flexibility: Polymorphism allows a program to be more flexible, as new objects can be added and handled without modifying existing code.
Conclusion
Polymorphism is an essential feature of C++ that enhances code flexibility and maintainability. By understanding and applying both compile-time and runtime polymorphism, you can write cleaner and more efficient code. Mastering polymorphism is crucial for anyone looking to dive deeper into object-oriented programming and leverage its full potential.
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments