Updated April 7, 2023
Definition of Polymorphism in C++
In C++, polymorphism refers to the fact that the same entity (object or function) behaves differently in different situations. In object-oriented programming, polymorphism is a crucial concept. The “Polymorphism” is a mixture of the terms “poly” and “morphs,” which means “multiple types.” It’s a phrase from the Greek language. We use it in three core principles of object-oriented programming which are polymorphism, inheritance, and encapsulation. For example, at the same time, a person may have a variety of characteristics. At the same time, he is a father, a son, a husband, and a worker. As a result, the same person behaves differently in different contexts.
Types of Polymorphism
The types of polymorphism and its working –
Polymorphism in C++ is primarily divided into two types –
1. Compile-time Polymorphism
A function is called during the compilation of a program in compile-time polymorphism. Early binding or static binding is the term used for this type of polymorphism. Function overloading or operator overloading are used to accomplish the compile-time polymorphism
a. Function overloading: When several functions of the same name but different parameters exist, they are said to be overloaded. Changes in the number of arguments or the form of arguments may cause functions to become overloaded.
For example, the two tasks can be accomplished using the sub() function. The two requests will be to subtract two integer values and two float values.
b. operator overloading: Operator overloading refers to assigning additional tasks to operators without altering the meaning of the operation. If we’re working with user-defined types like objects or structures, we can overload an operator in C++.
2. Runtime Polymorphism
Functions are called during the execution of a program in a Runtime polymorphism. As a result, it’s referred to as late binding or dynamic binding. Function overriding or virtual function is used to accomplish the runtime polymorphism.
a. Function overriding: In function overriding, override a base class function in a derived class, to give it a new definition.
b. Virtual function: A virtual function is a base class member function. In a derived class, we can redefine it. The virtual function must be declared in the base class using the keyword virtual.
Examples of Polymorphism in C++
Example of Polymorphism in C++ for function overloading
Example #1
Code:
#include<iostream>
using namespace std;
int add(int n1, int n2) {
return n1 + n2;
}
double add(double num1, double num2) {
return num1 + num2;
}
int add(int n1, int n2, int n3) {
return n1 + n2 + n3;
}
int main() {
// Calling function with 2 int parameters
cout << "Result 1 = " << add(10, 20) << endl;
// Calling function with 2 double parameters
cout << "Result 2 = " << add(10.5, 60.6) << endl;
// Calling function with 3 int parameters
cout << "Result 3 = " << add(50, 10, 20) << endl;
}
An output of the above code is –
As in the above program, three add() functions are created, the first function accepts two integer parameters, the second function accepts two double parameters and the third function accepts the three integer parameters. Finally, in the main function calling all three add() functions. Here which add() function is to be executed is decides based on the parameter passed is called function overloading. And display all the results, as we can see in the above output.
Example of Polymorphism in C++ for operator overloading –
Example #2
Code:
#include<iostream>
#include<string>
using namespace std;
class A
{
public:
string str;
A(){}
A(string i)
{
str = i;
}
A operator+(A a)
{
std::string res= str + a.str;
return res;
}
};
int main() {
A a1("Hello");
A a2(" World");
A res = a1+a2;
cout<< res.str;
}
An output of the above code is –
As in the above program, class A is created and the operator + is overloading by providing the new definition in the class. Next, in the main function creating the two objects of class A and calling the + operator on them. The return result of the + operator is displaying, as we can see in the above output.
Example of Polymorphism in C++ for function overriding –
Example #3
Code:
#include<iostream>
using namespace std;
class A {
public:
void display() {
cout << "This is from the base class function." << endl;
}
};
class B : public A {
public:
void display() {
cout << "This is from the derived class function. " << endl;
}
};
int main() {
B obj;
// Calling display() function of the class
obj.display();
return 0;
}
An output of the above code is –
As in the above program, classes A and B are created, where A is a base class and B is a derived class. The display() function of the base class is redefined in the derived class is called function overriding. In the main function creating the object of class B and calling the display() function, here the display() function of class B gets execute which is decided based on the object on which it is calling. As we can see in the above output.
Example of Polymorphism in C++ for Virtual function –
Example #4
Code:
#include<iostream>
using namespace std;
class A {
public:
virtual void display() {
cout << "This is from the base class function." << endl;
}
};
class B : public A {
public:
void display() {
cout << "This is from the derived class function. " << endl;
}
};
int main() {
B obj1;
// pointer of A type that points to obj1
A* obj2 = &obj1;
// calls member function of the class B
obj2->display();
return 0;
}
An output of the above code is –
As in the above program, classes A and B are created, where A is a base class and B is a derived class. The virtual display() function of the base class is redefined in the derived class. In the main function creating the object of class B, creating pointer of A type that points to class B object and calling the display() function with the pointer operator (“->”), here the display() function of class B gets to execute, as we can see in the above output.
Conclusion
Polymorphism in C++ is referred to the fact that the same entity behaves differently in different situations. There are two types of polymorphism, which are compiled time polymorphism and runtime polymorphism.
Recommended Articles
This is a guide to Polymorphism in C++. Here we discuss description, types of polymorphism, and its working example with code implementation. You may also have a look at the following articles to learn more –