- Polymorphism in C++ means, the same entity (function or object) behaves differently in different scenarios.
- The term “Polymorphism” is the combination of “poly” + “morphs” which means many forms. It is a greek word
- In object-oriented programming, we use 3 main concepts: inheritance, encapsulation, and polymorphism.
EXAMPLE OF POLYMORPHISM
- A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So the same person exhibits different behavior in different situations. This is called polymorphism.
TWO TYPES OF POLYMORPHISM
- Compile time polymorphism
- Run time polymorphism
Compile time polymorphism
The overloaded functions are invoked by matching the type and number of arguments.
This information is available at the compile time and, therefore, compiler selects the appropriate function at the compile time.
It is achieved by function overloading and operator overloading which is also known as static binding or early binding.
Now, let’s consider the case where the function name and prototype are the same.
Function overloading:-When there are multiple functions with the same name but different parameters, then the functions are said to be overloaded. Functions can be overloaded by changing the number of arguments or/and changing the type of arguments.
EXAMPLE OF FUNCTION OVERLOADING
// C++ program for function overloading
#include <bits/stdc++.h>
using namespace std;
class Learn
{
public:
// function with 1 int parameter
void func(int x)
{
cout << "value of x is " << x << endl;
}
// function with same name but 1 double parameter
void func(double x)
{
cout << "value of x is " << x << endl;
}
// function with same name and 2 int parameters
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y << endl;
}
};
int main() {
Learn obj1;
// Which function is called will depend on the parameters passed
// The first 'func' is called
obj1.func(7);
// The second 'func' is called
obj1.func(9.132);
// The third 'func' is called
obj1.func(85,64);
return 0;
}
- Operator overloading
- C++ also provides the option to overload operators.
- For example, we can make use of the addition operator (+) for string class to concatenate two strings.
- We know that the task of this operator is to add two operands. So a single operator ‘+’, when placed between integer operands, adds them and when placed between string operands, concatenates them
EXAMPLE OF OPERATOR OVERLOADING
// CPP program to illustrate
// Operator Overloading
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
Run time polymorphism
- Run time polymorphism is achieved when the object’s method is invoked at the run time instead of compile time.
- It is achieved by method overriding which is also known as dynamic binding or late binding.
Function overiding:-It occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
EXAMPLE OF OPERATOR OVERIDING
// C++ program for function overriding
#include <bits/stdc++.h>
using namespace std;
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
class derived:public base
{
public:
void print () //print () is already virtual function in derived class, we could also declared as virtual void print () explicitly
{ cout<< "print derived class" <<endl; }
void show ()
{ cout<< "show derived class" <<endl; }
};
//main function
int main()
{
base *bptr;
derived d;
bptr = &d;
//virtual function, binded at runtime (Runtime polymorphism)
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
return 0;
}