PREV NEXT
What is polymorphism?
- Polymorphism means having many forms or we can say we can define the polymorphism as the ability to display a message in many form.
- It happens when there is a hierarchy of classes and they are related by inheritance. It also means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
- Let us have a look at the real life example to understand polymorphism, like a person can have different characteristic at the same time.
- Like a woman can behave as a mother, a wife and a employee at the same time so, a same person posses different behavior in different situations. This is known as polymorphism.
Types of Polymorphism
In C++, polymorphism can be divided into two types:
- Compile Time Polymorphism
- Runtime Polymorphism
Compile Time Polymorphism:
The compile time polymorphism can be achieved by function overloading or by operator overloading. The overloaded functions are invoked by matching the type and number of arguments and this is done at the compile time so, compiler selects the appropriate function at the compile time. The operator overloading is also known as static binding.
Function Overloading
When there are multiple functions with same name but have different parameters then these functions are said to be overloaded. They can be overloaded by change in number of arguments or change in type of arguments.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> using namespace std; class Multiplication { public: int mul(int a,int b) { return a*b; } int mul(int a,int b, int c) { return a*b*c; } double mul(double a,double b) { return a*b; } }; int main(void) { Multiplication obj; cout<<obj.mul(2, 5)<<endl; cout<<obj.mul(7, 3, 1)<<endl; cout<<obj.mul(4.4, 10.8); return 0; } |
Operator Overloading
- In C++, operator overloading is a compile time polymorphism in which the operator is overloaded to provide the special meaning to the user defined data type.
- It is used to overload the operator in C++ and perform the operation on the user defined data type.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include<iostream> using namespace std; class Complex { private: int r, i; public: Complex(int a = 0, int b =0) {r = a; i = b;} // This is automatically called when '+' is used with // between two Complex objects Complex operator + (Complex const &obj) { Complex res; res.r = r + obj.r; res.i = i + obj.i; return res; } void print() { cout << r << " + i" << i << endl; } }; int main() { Complex c1(20, 10), c2(5, 7); Complex c3 = c1 + c2; // An example call to "operator+" c3.print(); } |
Runtime Polymorphism:
The runtime polymorphism is achieved when the object method is invoked at the runtime instead of compile time. It is achieved by method overriding which is also known as dynamic binding.
Function Overriding
The function overriding occurs when a derived class has a definition for one of the member functions of the base class. The base function is said to be overridden.
Let us have a look at the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> using namespace std; class Fruits { public: void taste(){ cout<<"Juicy..."; } }; class Apple: public Fruits { public: void taste() { cout<<"Juicy Apple..."; } }; int main(void) { Apple a = Apple(); a.taste(); return 0; } |
Difference between Compile Time Polymorphism and Runtime Polymorphism
Compile Time Polymorphism | Runtime Polymorphism |
The function is invoked at the compile time. | The function is invoked at the runtime. |
It is known as overloading, early binding and static binding. | It is also known as overriding, dynamic binding and late binding. |
It is achieved by function overloading and operator overloading. | It is achieved by virtual functions and pointers. |
It is less flexible. | It is more flexible. |
It provides fast execution. | It provides slow execution. |