C++ Polymorphism

The term polymorphism is a combination of the words "ploy" + "morphism". This word means having many forms.Polymorphism is one of the main keys to the power of object-oriented programming. The language that supports classes but does not support Polymorphism is known as object based language.

Let us consider a real-life example of polymorphism. A man behaves like a doctor in the clinic, father or son at home and customers in a market. Here, a single person is behaving differently according to situations.

Polymorphism is the ability to process a message or data in more than one form.
For Example :- If you give 10 + 7, the result will be 17 because the sum of 10 and 7 is 17 but if you give '10' + '7' the result will be 107. The same operation symbol '+' can perform two operations (summarization and condensation) depending on the data types. It is polymorphism.

Polymorphism is of two types :- Compile time polymorphism and Run time polymorphism.

Compile Time Polymorphism

Compiled time polymorphism means that the function binds to the data at the time of compilation. The object is bound to its function at compile time. It is achieved by function overloading and operator overloading which is also known as static binding or early binding.

Function Overloading
When there are multiple functions under the same name, but with different parameters, they are said to be overloaded functions.

Example :

#include <bits/stdc++.h> 
using namespace std; 
class Lfc 
{ 
public: 
// function with 1 int parameter 
void fun(int a) 
{ 
cout << "value of a is " << a << endl; 
} 
// function with same name but 1 double parameter 
void fun(double a) 
{ 
cout << "value of a is " << a << endl; 
} 
// function with same name and 2 int parameters 
void fun(int a, int b) 
{ 
cout << "value of a and b is " << a << ", " << b << endl; 
} 
}; 
int main() { 
Lfc obj; 
// Which function is called will depend on the parameters passed 
// The first 'fun' is called  
obj.fun(10); 
// The second 'fun' is called 
obj.fun(15.132); 
// The third 'fun' is called 
obj.fun(100,81); 
return 0; 
}  


Output :

value of x is 7
value of x is 9.132
value of x and y is 85, 64



Operator Overloading

C++ also provide option to overload operators. If you give 15 + 5, the result will be 20 because the sum of 15 and 5 is 20 but if you give '15' + '5' the result will be 155. The same operation symbol '+' can perform two operations (summarization and condensation) depending on the data types. It is polymorphism.

Example :

#include<iostream> 
using namespace std; 
class Lfc { 
private: 
int a, b; 
public: 
Lfc(int r = 0, int i =0)  {a = r;   b = i;} 
// This is automatically called when '+' is used with 
// between two Lfc objects 
Lfc operator + (Lfc const &obj) { 
Lfc res; 
res.a = a + obj.a; 
res.b = b + obj.b; 
return res; 
} 
void print() { cout << a << " + i" << b << endl; } 
}; 
int main() 
{ 
Lfc l1(10, 5), l2(2, 4); 
Lfc l3 = l1 + l2; // An example call to "operator+" 
l3.print(); 
} 


Output :

12 + i9

Runtime polymorphism

This is also known as Function Overriding. When a derived class has a definition for one of the member functions of the base class it is known as runtime polymorphism

Example :

#include <iostream>
using namespace std;
class X {
public:
void fun(){
cout<<"Super Class Function"<< endl;
}
};
class Y: public X{
public:
void fun(){
cout<<"Sub Class Function";
}
};
int main() {
//Parent class object
X obj1;
obj1.fun();
//Child class object
Y obj2;
obj2.fun();
return 0;
}


Output :

Super Class Function
Sub Class Function

Differences b/w compile time and run time polymorphism.

Compile time polymorphism Run time polymorphism
The function to be invoked is known at the compile time. The function to be invoked is known at the run time.
It is less flexible as mainly all the things execute at compile time. It is more flexible as all the things execute at run time.
It is achieved by function overloading and operator overloading. It is achieved by virtual functions and pointers
It is also known as overloading, early binding and static binding. It is also known as overriding, dynamic binding and late binding

Exercise:-

1. Which among the following best describes polymorphism?

A. It is the ability for a message/data to be processed in more than one form
B. It is the ability for a message/data to be processed in only 1 form
C. It is the ability for many messages/data to be processed in one way
D. It is the ability for undefined message/data to be processed in at least one way

View Answer


2. If same message is passed to objects of several different classes and all of those can respond in a different way, what is this feature called?

A. Inheritance
B. Overloading
C. Polymorphism
D. Overriding

View Answer



Program :-

C++ program to print Lucas series upto N terms.

Explanation :- Lucas series is defined as the sum of the previous two terms of the series with the first two terms being 2 and 1 respectively.

#include <iostream>
using namespace std;
int main()
{
int n, i, t1 = 2, t2 = 1, tn;
cout << "Enter the number of terms desired in the lucas series: ";
cin >> n;
if (n == 1)
cout << endl << 2 << endl;
else if (n == 2)
cout << endl << 2 << endl << 1 << endl;
else if (n > 2)
{
cout << endl<< "Lucas series for "<< n<< " terms is:"<< endl<< t1 << endl << t2 << endl;
for (i = 0; i < n-2; i++)
{
tn = t1 + t2;
cout << tn << endl;
t1 = t2;
t2 = tn;
}
}
return 0;
}


Output :

First run:
Enter the number of terms desired in the lucas series: 5
Lucas series for 5 terms is:
2
1
3
4
7
Second run:
Enter the number of terms desired in the lucas series: 10
Lucas series for 10 terms is:
2
1
3
4
7
11
18
29
47
76





Visit :


Discussion



* You must be logged in to add comment.