Compile Time Polymorphism in C++
What is Polymorphism?
Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in real life is the simultaneous existence of many traits in one individual. A man simultaneously serves as a parent, a partner, and an employee. Therefore, the same person exhibits diverse behaviour depending on the circumstance. This is known as polymorphism. Polymorphism is one of the essential elements of object-oriented programming.
In C++, a polymorphic member function behaves differently based on the entity that calls or activates it. The Greek word polymorphism implies to have various forms. It happens when there is an inheritance-related hierarchy of classes.
What is Compile Time Polymorphism?
To create this form of polymorphism, either operators or functions must be overloaded. The overload functions are invoked when the type and number of arguments match. The data is available at compile time. This indicates that the C++ compiler will choose the appropriate function during compilation. Compile-time polymorphism can be achieved by the use of function overloaded and operator overloading also referred to as static binding or initial binding.
Compile Time Polymorphism Types
There are two varieties of compile-time polymorphism in C++:
1. Overloading of Functions
These functions are referred to as being overloaded when they share the same name but have different argument lists.Functions may become overloaded when the number or type of arguments changes. Function overloading occurs in C++ when there are numerous functions that have the same identity but different arguments.The kind ofnumber of the arguments may vary
Advantages of Function Overloading
The benefit of that is that we won't constantly need to think of new names.We don't need to assign different names for the two functions because they are both used to add integers. Writing programs is simple, and there aren't many function names to keep in mind. That is the advantage we currently enjoy.
Example program 1
#include <iostream>
using namespace std;
void add(int x, int y)
{
int sum = x + y;
cout<< "The value of the result is " << sum <<endl;
}
void add(int x, int y, int z)
{
int sum = x + y + z;
cout<< "The value of the result is " << sum <<endl;
}
void Add(double x, double y)
{
double sum = x + y;
cout<< "The value of the result is " << sum <<endl;
}
int main()
{
Add (10, 20);
Add (10, 5, 20);
Add (3.3, 10.7);
return 0;
}
Output
The value of the result is 30
The value of the result is 35
The value of the result is 14
Example 2
#include<iostream>
using namespace std;
int Sum(int x, int y)
{
return x + y;
}
float Sum(float x, float y)
{
return x + y;
}
int Sum(int x, int y, int z)
{
return x + y + z;
}
int main()
{
cout<<Sum(10, 10) <<endl;
cout<<Sum(17.5f, 3.4f) <<endl;
cout<<Sum(11, 20, 3) <<endl;
return 0;
}
Output
20
20.9
34
2. Overloading of operator
The possibility to overload operators is likewise available in C++. We define a new meaning for a C++ operator in Operator Overloading. Additionally, it alters the operator's workflow. For instance, we can concatenate two strings using the string class's operator (+).
We are aware that the purpose of this operator, which has two operands, is to add them. Therefore, the single operator "+" adds and concatenates strings when it is used between operands that are integers.
Advantages of operator overloading
To conduct operations like addition (+), multiplication (*), subtraction (-), conversion ("()"), increment and decrement (++, -), new operator, delete operator, and so on, we have several operators. This means that C++ has operators that can be used to do a variety of tasks. And certain data types are intended for these operators.
Example 1
#include<iostream>
using namespace std;
class Complex
{
private:
int real;
int img;
public:
Complex (int r = 0, int i = 0)
{
real = r;
img = i;
}
Complex operator + (Complex x)
{
Complex temp;
temp.real = real + x.real;
temp.img = img + x.img;
return temp;
}
void Display()
{
cout<< real << "+i" <<img<<endl;
}
};
int main()
{
Complex C1 (3, 7);
C1.Display();
Complex C2 (5, 2);
C2.Display();
Complex C3;
C3 = C1+C2;
C3.Display();
}
Output
3+i7
5+i2
8+i9
Example 2
#include <iostream>
using namespace std;
class Count
{
private:
int value;
public:
Count(int num = 0)
{
value = num;
}
void operator ++ (){
++value;
}
void Display()
{
cout<< "Count: " << value <<endl;
}
};
int main() {
Count count1(10);
count1.Display();
++count1;
count1.Display();
return 0;
}
Output
Count: 10
Count: 11