Polymorphism means having many forms. The word “poly” means many and “morphs” means forms, which means it is the way of performing some task in many ways. That means a single function or an operator functioning in many ways. It is one of the major concepts of Object-oriented programming.
Take a real-life example: A woman can be a mother or daughter and a teacher in a class, that is she posses a different behaviour at different times or situations. Similarly, a class in a program has a method and the subclass have the same method but with a different task to perform related to the first method. This is called polymorphism.
In C++, we have two types of polymorphism and they are:
- Compile-time Polymorphism or Static (or early) Polymorphism.
- Runtime Polymorphism or Dynamic (or late) Polymorphism.
1. Compile time Polymorphism
In this type of program, the flow of control is decided at compile time itself. It is achieved by function overloading and operator overloading. It is also known as static or early binding.
Function Overloading:
The multiple functions having the same name but with different parameters, then these functions are said to be overloaded. When we call those functions, we need to make sure to enter the proper number of arguments along with the proper data type.
Example: C++ example of function overloading.
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 | // C++ program for function overloading #include <iostream> using namespace std; class OverloadProgram { public: int total(int num1, int num2) { return num1 + num2; } int total(int num1, int num2, float num3) { return num1 + num2 + num3; } }; int main() { OverloadProgram obj; //calling first display function cout << "Result 1: " << obj.total(50, 40) << endl; //calling second display function cout << "Result 2: " << obj.total(7, 15, 5) << endl; return 0; } |
Output:
1 2 | Result 1: 90 Result 2: 27 |
Operator Overloading:
To achieve Compile time Polymorphism, operator overloading is also used. Operator overloading means performing different operations with the same operator. The keyword ‘operator‘ is used to overload an operator.
Example: Consider an operator ‘+‘, it is used to add the two numbers but it is also used with string to concatenate two strings. So the ‘+‘ operator when placed between number operands, addition operation is done but when placed between string operands, it concatenates them.
Let us understand through an example: C++ program for operator overloading.
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 | // C++ program for operator overloading #include <iostream> using namespace std; class Message { string str; public: Message(string i) { str = i; } void operator+(Message); void display(); }; void Message::operator+(Message a) { string msg = str + a.str; cout << "The result: " << msg; } int main() { Message m1("Hi, Welcome "); Message m2("Home"); m1 + m2; return 0; } |
Output:
1 2 | //Output: The result: Hi, Welcome Home |
2. Run time Polymorphism
In run time polymorphism, the program is called at the time of execution that is at rum time. It is also known as the late binding or dynamic binding.
This type of polymorphism is achieved by Method Overriding. We use virtual functions and pointers to achieve this.
Function Overriding:
Function overriding allows us to define a function at a derived class that is already present in a base class. And that base is said to be overridden.
And the decision made to call the required function is done at the run time. Hence, the Runtime polymorphism.
Example: C++ example of function overriding.
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 | // C++ program for function overriding #include <iostream> using namespace std; class Animal { public: void display() { cout << "I am Animal" << endl; } }; class Dog: public Animal { public: void display() { cout << "I am a Dog" << endl; } }; int main(void) { Animal anim = Animal(); anim.display(); //parent class object Dog dg = Dog(); dg.display(); // derived class object return 0; } |
Output:
1 2 | I am Animal I am a Dog |
Differences between Compile time and Run time Polymorphism
Compile-time polymorphism | Run time polymorphism |
---|---|
In this type of program, the flow of control is decided at compile time itself. | The program is called at the time of execution that is at rum time |
It is achieved by function overloading and operator overloading | It is achieved by function overriding and virtual function. |
Also called static or early binding. | Also called late binding or dynamic binding. |
Here, the methods have the same name but with a different number of parameters or the type of parameters. | Here, the methods have the same name, the same number of parameters, or the type of parameters. |
As it happens at compile-time, therefore provides a fast execution. | As it happens at run-time, therefore provides a slow execution. |