What is polymorphism in C++?
Polymorphism in C++ is one of the four key principles of Object-Oriented Programming (OOP), along with encapsulation, inheritance, and abstraction. Polymorphism allows objects of different types to be treated as objects of a common base type. It enables a single function, operator, or method to work in different ways depending on the context, such as the object calling the method or the data types involved.
Types of Polymorphism in C++:
1. Compile-Time Polymorphism (Static Polymorphism)
This type of polymorphism is resolved at compile time and is typically achieved through function overloading or operator overloading. The function or operator behaves differently based on the number or type of arguments passed.
-
Function Overloading: Multiple functions with the same name but different parameter lists.
void display(int a) { cout << "Integer: " << a << endl; } void display(double a) { cout << "Double: " << a << endl; }
Here,
display
behaves differently based on the argument type. -
Operator Overloading: Defining custom behavior for standard operators (like
+
,-
) for user-defined types.class Complex { public: int real, imag; Complex operator + (const Complex& obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } };
2. Run-Time Polymorphism (Dynamic Polymorphism)
Run-time polymorphism is achieved through function overriding with virtual functions. This type of polymorphism allows derived classes to provide specific implementations of methods that are declared in a base class. The method to be called is determined at runtime based on the type of the object, not the type of the reference.
- Virtual Functions: When a function in the base class is declared as
virtual
, it can be overridden by a function with the same signature in a derived class.class Animal { public: virtual void sound() { cout << "Some animal sound" << endl; } }; class Dog : public Animal { public: void sound() override { cout << "Bark" << endl; } }; Animal* animal = new Dog(); animal->sound(); // Outputs "Bark"
In this example, sound()
behaves differently based on the actual object type (Dog) even though the pointer is of type Animal*
.
Benefits of Polymorphism:
- Code Reusability: Polymorphism allows code to be more flexible and reusable. You can write more generic functions or classes that work with multiple types or objects.
- Extensibility: Polymorphism makes it easy to extend and maintain code. Derived classes can override base class methods, providing custom behavior while keeping a consistent interface.
- Maintainability: Changes in one part of the code (like a base class) do not necessarily affect other parts that rely on polymorphic behavior.
Conclusion:
Polymorphism in C++ is a powerful feature that enables flexibility, code reuse, and cleaner design by allowing methods to behave differently depending on the object invoking them. Compile-time polymorphism (function/operator overloading) provides early binding, while run-time polymorphism (function overriding) supports dynamic behavior.
Sources:
GET YOUR FREE
Coding Questions Catalog