Polymorphism in cpp is one of the important feature of cpp (object oriented programming )
In this tutorial we will see what is polymorphism, types of polymorphism.
What is polymorphism
Polymorphism means many and morp means forms it means one name multiple forms.
It means multiple jobs are performed by single entity.
Types of polymorphism

In Cpp, polymorphism is implemented in two ways
- Compile time polymorphism
- Run time polymorphism
Compile time polymorphism
This is also known ad early binding or static binding.
Polymorphism is resolved at compile time.
Example of compile time polymorphism
Method overloading
Function overloading
Writing the same function name with different arguments or different argument list is known as function overloading.
Function overloading definition A set of function which having same name but performs different tasks by passing different parameters.
Operator overloading
To redefine the meaning of operator is known as operator overloading.
Operator overloading is one of the important feature of capital with the help of which the operation which are possible on user defined data type or built in data type means just like you can add two integers.
Program for compile time polymorphism
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 |
#include <iostream> using namespace std; void square(double); void square(int); int main() { square(10.5); square(10); return 0; } void square(double s) { float val= s*s; cout<<"square of float numbers= "<<val; } void square(int s) { int res=s*s; cout<<"\nsquare of int numbers= "<<res; } |
Output :

Run time polymorphism
This is also known ad late binding or dynamic binding.
Polymorphism is resolved at run time.
Example of run time polymorphism : virtual function
Virtual Function in cpp
When we use the same function name in both based and derived class the function in based class is declared as virtual preceding it normal declaration.
When a function is made virtual, copy determines which version of the function to use at run time based on the type of object pointed by the Base pointer.
We can execute different versions of function by making the Base pointer to point to different object.
Program for run time polymorphism
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 46 |
#include <iostream> using namespace std; class base { public: virtual void display(void) { cout<<"\nIn base class"; } }; class derived:public base { public: void display(void) { cout<<"\nIn derived class"; } }; int main() { base b1; base *bp; bp=&b1; bp->display(); derived d1; bp=&d1; bp->display(); return 0; } |
Output :

Explanation :
1. Execution starts with main function.
2. First we create object of base class and base class pointer
base b1;
base *bp;
bp=&b1; => Assign base class object to base class pointer
3. Using base class pointer we call function.
bp->display();
virtual void display(void) { //its virtual function
cout<<“\nIn base class”; => In base class
}
4. Next we create object of derived class
derived d1;
bp=&d1; => Assign derived class object to base class pointer.
5. Now base class holds the reference to derived class, call function in derived class
bp->display(); => this calls the derived class function
void display(void) {
cout<<“\nIn derived class”; => In derived class
}
6. End with the program.
Leave a Reply