Polymorphism
C++ Polymorphism
Definition: The word Polymorphism means "many forms." In C++, it occurs when there is a hierarchy of classes related by inheritance. It allows us to perform a single action in different ways. Most commonly, polymorphism happens when a derived (child) class overrides a method from its base (parent) class to provide its own specific behavior.
Why: Polymorphism is a cornerstone of the beginner C++ OOP syllabus because it allows for high-level abstraction. You can write a single piece of code that works with a general "Parent" type, but it will automatically trigger the correct "Child" behavior depending on which object is actually being used at runtime.
The "Same Name, Different Behavior" Principle
In procedural programming, you might need different function names like catSound() and dogSound(). With polymorphism, you simply call sound(), and the object knows exactly what noise to make based on its specific class type.
Example: Method Overriding
In this example, both the Animal class and the Cat class have a method named sound(). Because the Cat class provides its own version, it "overrides" the parent version when called by a Cat object:
#include <iostream> using namespace std; // Base class class Animal { public: void sound() { cout << "The animal makes a generic sound" << endl; } }; // Derived class class Cat : public Animal { public: // Overriding the base class method void sound() { cout << "Meow" << endl; } }; int main() { Animal myAnimal; Cat myCat; myAnimal.sound(); // Outputs: The animal makes a generic sound myCat.sound(); // Outputs: Meow return 0; }
Types of Polymorphism in C++
| Category | Mechanism | Execution Time |
|---|---|---|
| Compile-time | Function Overloading / Operator Overloading | The compiler determines which function to call before the program runs. |
| Runtime | Function Overriding (using Virtual Functions) | The program determines which function to call while it is running based on the object type. |
Key Notes
- The
virtualKeyword: To achieve true runtime polymorphism where a base class pointer can call a derived class method, you must declare the base method asvirtual(e.g.,virtual void sound()). - Extensibility: Polymorphism makes it easy to add new classes. If you want to add a
Dogclass later, you just create it and give it asound()method; you don't need to change any of the code that already uses theAnimalinterface. - Interface Consistency: It provides a consistent interface for different data types. For example, a
Shapeclass might have anarea()method that works differently for aSquareand aCircle, but the developer only needs to remember one method name. - Memory and Performance: Runtime polymorphism (using virtual functions) has a very small performance cost due to the "vtable" (virtual table) lookup, but the benefits in code organization and flexibility far outweigh the cost in most applications.
🏋️ Test Yourself With Exercises
Take our quiz on Polymorphism to test your knowledge.
Browse Quizzes »