Poly means many, morphism means forms. Polymorphism in C++means one function behaving differently under different situations.
1. Compile time polymorphism
a. Function overloading
b. Operator overloading
c. Function overriding
2. Run time polymorphism
a. Virtual functions
3. Explicit and Implicit casting
1. Compile time polymorphism:
The binding happens at compile time is called as Compile time polymorphism. Binding means, the particular function that is to be called when a function call is made is decided at the compile time itself. This kind of binding done at the compile time is called as compile time polymorphism.
There are 3 different ways that we can achieve compile time polymorphism:.
1. Function overloading
2. Function overriding
3. Operator Overloading [explained in further chapter]
We shall see the first 2 types with help of an example;
1. Function overloading
This type will have the same function name, but difference in number of parameters and type of parameter being called. This is called as function overloading. Here the binding for the function call and the function definition being called occurs at the compile time.
Hence it is compile time polymorphism.
Example:
We have 2 overloaded add functions, one takes 2 parameters another will take 3 parameters. The function call is decided at compile time.
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout<<"In add function that takes 2 parameters"<< (a+b)<<endl;
}
void add(int a, int b, int c)
{
cout<<"In add function that takes 3 parameters"<< (a+b+c)<<endl;
}
int main(void)
{
add(1, 2);
add(1, 2, 3);
return 0;
}
Output:
In add function that takes 2 parameters, sum is 3
In add function that takes 3 parameters, sum is 6
2. Function overriding
In function overriding, the base class function is overridden in derived class. It means, a base class function with same name and parameters will be replaced in derived class in inheritance.
#include <iostream>
using namespace std;
class Base
{
public:
void dispaly()
{
cout<<"In base class"<<endl;
}
};
class Derived : public Base
{
public:
void dispaly()
{
cout<<"In derived class"<<endl;
}
};
int main(void)
{
Derived obj;
obj.dispaly();
Base obj1;
obj1.dispaly();
return 0;
}
Output:
In derived class
In base class
2. Runtime Polymorphism.
Runtime Polymorphism is achieved by using virtual functions. We shall study about virtual function in next chapters.
Example for Runtime Polymorphism:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void dispaly()
{
cout<<"In base class"<<endl;
}
};
class Derived : public Base
{
public:
void dispaly()
{
cout<<"In derived class"<<endl;
}
};
int main(void)
{
Derived obj;
Base *obj1 = &obj;
obj1->dispaly();
return 0;
}
Output:
In derived class
3. Explicit and Implicit casting
Consider below example:
float f = 10; // here int will be implicitly converted into float
int a = 10.10; // here float will be implicitly converted into int.
This is an example for explicit and implicit casting.