AIM: Write a C++ program to illustrate runtime polymorphism.
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
Real life example of polymorphism, a person at a same time can have different characteristic. Like a man at a same time is a father, a husband, a employee. So a same person posses have different behavior in different situations. This is called polymorphism.
Polymorphism is considered as one of the important features of Object Oriented Programming.
In C++ polymorphism is mainly divided into two types:
- Compile time Polymorphism
- Runtime Polymorphism
using namespace std;
float add(float m,float n)
float sub(float m,float n)
float mul(float m,float n)
cout<<” Enter two numbers:\n”;
cout<<”Enter task(1=add,2=sub,3=mul,4=exit): “;
case 1: pt=add;
case 2: pt=sub;
case 3: pt=mul;
case 4: exit(0);
default: cout<<“You entered a wronge option\n”;
cout<<“Result of operation is:”<<pt(x,y)<<endl;
}while(task<5 && task>0);
Enter two numbers:
Enter task(1=add,2=sub,3=mul,4=exit): 3
Result of operations is: 6240
Enter task(1=add, 2=sub, 3=mul, 4=exit): 2
Result of operation is: 31
Enter task(1=add, 2=sub, 3= mul, 4=exit): 4