What is Polymorphism in OOPs?
The word Polymorphism is derived from two Greek words:
-
Poly → many
-
Morph → forms
So, Polymorphism means having many forms.
In computer programming, polymorphism refers to the property that a function, operator or object will behave differently under different circumstances. That is, the input can be different tasks done by the same name of the function.
Simple Analogy:
Think of the word “run”:
-
Athletes run in a race.
-
Programs run on a computer.
-
Cars run on fuel.
It is repetition of the same word, but the meaning is different depending on the context. This is the polymorphism of OOP.
Types of Polymorphism in C++
The two most common types of polymorphism in C++ are:
Compile-Time Polymorphism (Static Polymorphism)
-
Run-Time Polymorphism (Dynamic Polymorphism)
Let us get to know them by example.
1. Compile-Time Polymorphism (Static Polymorphism)
This type of polymorphism is resolved at compile time.
It is achieved using:
-
Function Overloading
-
Operator Overloading
(a) Function Overloading
In C++, Function Overloading is a feature, which enables you to create several functions of the same name but with different parameter lists (different number or different type of parameters).
At compile time, the compiler will identify the function to be called, based on the arguments being passed.That is why this is an example of Compile-Time Polymorphism.
Rules of Function Overloading in C++
-
Functions must have the same name.
-
They must differ in either:
-
Number of parameters
-
Type of parameters
-
Or both
-
-
The return type alone cannot distinguish overloaded functions.
Real-Life Analogy
Just imagine functional overloading as an ATM machine:
-
On inserting a debit card, it will be affected like a withdrawal.
-
When you insert a credit card, it enables credit transaction.
-
If a passbook is inserted, it prints out.
Depending on the type of input on which the type of input is used, the action (function) remains the same (machine name is the same).
Key Points
-
Function overloading is solved at compile time (therefore static polymorphism).
-
Improves Ease of reading (similar to similar operations).
-
Can not overload functions with the return type only.
In short:
Function Overloading = Same function name + Different parameters → Different tasks
(b) Operator Overloading
The C++ feature of Operator Overloading gives users of C++ the ability to reassign the meaning of operators (such as +, -, *) to their own data types (such as classes).
This causes objects to act as inbuilt data types in operating operators.
What is the point of Operator Overloading?
C++ operators default only on primitive types of data (int, float, etc.).However, what about addition of two Complex numbers or comparing two Strings (objects)?That is where operator overloading comes in.
Code Link
Real-Life Analogy
The redefinition of a symbol is a kind of redefining the operator:
-
In mathematics + is addition of numbers.
In reality, the phone + sign means international dialing.
We can concatenate + (when using strings) or add (when using objects) in programming.
Key Points About Operator Overloading
-
The only operators that can be overloaded are the existing operators. (You can’t create new ones like $$).
-
Some operators cannot be overloaded:
-
.
(dot) -
::
(scope resolution) -
sizeof
-
?:
(ternary operator)
-
The operator overloading will be able to make the code more reusable and readable.
In short:
Operator Overloading = redefining operators (+
, -
, ==
, etc.) for objects, so they work like built-in types.
2. Run-Time Polymorphism (Dynamic Polymorphism)
This type of polymorphism is resolved at runtime.
It is achieved using:
-
Function Overriding
-
Virtual Functions
(a) Function Overriding
Function Overriding occurs when a derived class (child class) implements a particular implementation of a definite function already implemented in its base class (parent class).
Unlike function overloading (compile-time), function overriding is an example of run-time polymorphism, because the function to be executed is decided at runtime.
Rules of Function Overriding in C++
-
The base and derived class functions must have the same name, same parameters, and same return type.
-
The base class function must be marked as virtual (to achieve runtime polymorphism).
-
The function is called through a base class pointer or reference.
The base and derived class functions must have the same name, same parameters, and same return type.
The base class function must be marked as virtual (to achieve runtime polymorphism).
The function is called through a base class pointer or reference.
Code Link
Real-Life Example
Suppose that parent class = Vehicle, and derived classes = Car
, Bike
, Bus
.
-
The parent class has a function
start()
. -
Each child overrides it:
-
Car → "Car starts with a key"
-
Bike → "Bike starts with a kick"
-
Bus → "Bus starts with a button"
-
Although the name of the start method is similar, when invoked at runtime, its behavior varies depending on the object.
In short:
Function Overriding = redefining a base class function in a derived class → supports runtime polymorphism.
(b) Virtual Functions
A Virtual Function in C++ This is a function that is defined in a base class with the keyword virtual and is intended to be overridden in derived classes.
They guarantee that an object of the right kind is invoked at runtime, depending on the kind of object being addressed (not the kind of address).
This is the foundation of the runtime polymorphism in C++.
How Virtual Functions Work (Behind the Scenes)
-
When a class has a virtual function, the compiler creates a hidden table called V-Table (Virtual Table).
-
Each object of that class contains a hidden pointer called vptr, which points to the V-Table.
-
At runtime, the correct function is called using the vptr → V-Table mapping.
This mechanism enables dynamic dispatch (runtime polymorphism).
Key Points About Virtual Functions
-
Stated with the keyword virtual in the base class.
-
Overridden in the derived class by the same signature.
-
Invoked with the help of a base class reference or pointer.
-
When no overriding is done in a derived class, the base one is referred to.
-
Constructors need not be virtual but destructors must be virtual (so as to prevent inheritance memory leaks).
Code Link
Real-Life Examples
Take the example of a remote control (base class pointer):
-
Together with a TV, the button activity = TV on.
-
When used together with an AC, the button action corresponding = “AC on”.
-
Fan on, when coupled with a Fan, = Fan on.
It is the same button (function call), the behavior is defined by the device (object) = That is how virtual functions work.
In short:
Virtual Functions = allow a base class pointer/reference to call the derived class function → enables runtime polymorphism.
Advantages of Polymorphism
-
Improves code reusability
-
Increases flexibility of programs
-
Makes maintenance easier
-
Supports extensibility (adding new classes without modifying existing code)
Conclusion
One of the strongest properties of OOPs is polymorphism.It enables an object, operator, or any other thing to be of various forms within a given context.
-
Function overloading and operator overloading are used to generate compile-time polymorphism.
-
Function overriding and virtual functions are used to perform run-time polymorphism.
With the use of polymorphism, it is also possible to make our code more flexible, modular, and maintainable - and this is what we need in the real-world software development.
If you found this post useful:
⮕ Share it with others who might benefit.
⮕ Leave a comment with your thoughts or questions—I’d love to hear from you.
⮕ Follow/Subscribe to the blog for more helpful guides, tips, and insights.
Comments
Post a Comment