C++ Polymorphism is the capability of objects of different types to respond to functions of the same name. The program does not know the actual object type in advance.
The word polymorphism is a mixture of two works ploy and morphism where poly means many and morphism means forms. It is applicable when the classes have interacted with each other through inheritance.
To understand the polymorphism concept we have to take three sentences which are given below.
1) Your right to know this
2) Please turn right
3) We are in a right place
In all the above conditions, the single word “right” behaves differently in different conditions. If we treat right as a single word we don’t know the actual meaning to consider.
C++ Polymorphism can be achieved in the following 4 ways
- Function overloading
- Operator overloading
- Function Overriding
- Using Virtual Function
Here we discuss one by one
1. Function overloading:
It can be achieved by using the same name functions but with different parameters. Below is the example
Example 1:
//Function overloading in C++
#include <iostream>
using namespace std;
class Addition {
public:
int sum(int number1, int number2){
return number1+number2;
}
int sum(int number1, int number2, int number3){
return number1+number2+number3;
}
};
int main() {
Addition add;;
cout<<"Output: "<<add.sum(55, 45)<<endl;
cout<<"Output: "<<add.sum(55, 45, 100);
return 0;
}
Output
Output: 100
Output: 200
2. Operator overloading
It can be achieved by using operator overloading. For example, we use the “+” operator for the addition of two operands. But at the same time, we use it to concatenate two strings. Below is the example
Example 2:
// Operator overloading in C++
#include <iostream>
using namespace std;
class Sum {
int number1;
public:
Sum(int n)
{
number1 = n;
}
Sum operator+(Sum s2)
{
Sum s3(0);
s3.number1 = number1+s2.number1;
return s3;
}
void Show()
{
cout<<"Result will be: "<<number1<<endl;
}
};
int main() {
Sum s1(5);
Sum s2(25);
Sum result(0);
result=s1+s2;
result.Show();
return 0;
}
Output
Result will be: 30
3. Function Overriding
It can be achieved by declaring two methods having the same definition in parent and child class. This is called function overriding. It can be determined at run time which definition of the function is going to call.
Example 3:
// Function Overriding in C++
#include <iostream>
using namespace std;
class Parent {
public:
int Sum(int Number1, int Number2) {
int result = Number1+Number2;
cout << "Sum at parent class: " <<result<< endl;
}
};
class Child : public Parent {
public:
int Sum(int Number1, int Number2) {
int result = Number1+Number2;
cout << "Sum at child class: " <<result<< endl;
}
};
int main() {
Parent parent1;
parent1.Sum(5,3);
return 0;
}
Output
Sum at parent class: 8
4. Using Virtual Function
As its name concerns, these functions are effective but in reality, do not exist known as virtual functions. These functions enable users to execute completely different functions with the same name from different classes by the same function call. These functions are defined with the keyword “virtual” in the parent class and can be overridden in the child class.
Example 4
// Polymorphism in C++
#include <iostream>
using namespace std;
class Parent {
public:
virtual int Sum(int Number1, int Number2) {
int result = Number1+Number2;
cout << "Sum at parent class: " <<result<< endl;
}
};
class Child : public Parent {
public:
int Sum(int Number1, int Number2) {
int result = Number1+Number2;
cout << "Sum at child class: " <<result<< endl;
}
};
int main() {
Child child1;
Parent* parent1 = &child1;
parent1->Sum(5,15);
return 0;
}
Output
Sum at child class: 20