CPP Language / Polymorphism

Poly means many, morphism means forms. It means a function take more than one form in terms of number of arguments and their data types.

Examples
sum(int x, int y)
sum(int x, float y)
sum(int x, float y, double z)

Types of polymorphism
1. Compile time / static / early binding Polymorphism.
2. Runtime / dynamic / late binding Polymorphism.

Compile time Polymorphism
If a call to a function is determined during compile time that’s why it is called compile time polymorphism. Example of Compile time Polymorphism are Function overloading and Operator overloading.

Compile time Polymorphism Example
Program Output

#include
using namespace std;
class Add
{
public:
int sum(int num1, int num2)
{
return num1+num2;
}

int sum(int num1, int num2, int num3)
{
return num1+num2+num3;
}
};

int main()
{
Add obj;
cout<<"Output: "< cout<<"Output: "< return 0;
}



Runtime Polymorphism
If a call to the function is determined at runtime that’s why it is called runtime polymorphism. Example of runtime time Polymorphism is function.

Function Overriding
Parent function is overrides by Child function..The function call determines at runtime determines to call which function (Child / parent function)is called runtime polymorphism.

Program Output

Super Class Function
Sub Class Function



Home     Back