Polymorphism: Polymorphism means multiple forms. It means having more than one function with the same function name but with different functionalities.
Polymorphism is of two types:

- Runtime polymorphism
Runtime polymorphism is also known as dynamic polymorphism. Function overriding is an example of runtime polymorphism. Function overriding means when the child class contains the method which is already present in the parent class. Hence, the child class overrides the method of the parent class. In case of function overriding, parent and child class both contains the same function with the different definition. The call to the function is determined at runtime is known as runtime polymorphism.
Let’s understand this through an example:
- #include <iostream>
- using namespace std;
- class Base
- {
- public:
- virtual void show()
- {
- cout<<“javaTpoint”;
- }
- };
- class Derived:public Base
- {
- public:
- void show()
- {
- cout<<“javaTpoint tutorial”;
- }
- };
- int main()
- {
- Base* b;
- Derived d;
- b=&d;
- b->show();
- return 0;
- }
Output:
javaTpoint tutorial
- Compile time polymorphism
Compile-time polymorphism is also known as static polymorphism. The polymorphism which is implemented at the compile time is known as compile-time polymorphism. Method overloading is an example of compile-time polymorphism.
Method overloading: Method overloading is a technique which allows you to have more than one function with the same function name but with different functionality.
Method overloading can be possible on the following basis:
- The return type of the overloaded function.
- The type of the parameters passed to the function.
- The number of parameters passed to the function.
Let’s understand this through an example:
- #include <iostream>
- using namespace std;
- class Multiply
- {
- public:
- int mul(int a,int b)
- {
- return(a*b);
- }
- int mul(int a,int b,int c)
- {
- return(a*b*c);
- }
- };
- int main()
- {
- Multiply multi;
- int res1,res2;
- res1=multi.mul(2,3);
- res2=multi.mul(2,3,4);
- cout<<“\n”;
- cout<<res1;
- cout<<“\n”;
- cout<<res2;
- return 0;
- }
Output:
6 24
- In the above example, mul() is an overloaded function with the different number of parameters.