Home Uncategorized Write a program to illustrate runtime polymorphism

Write a program to illustrate runtime polymorphism


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)
    return m+n;
float sub(float m,float n)
    return m-n;
float mul(float m,float n)
    return m*n;
int main()
    float x,y;
    cout<<” Enter two numbers:\n”;
    int task;
        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);
    return 0;


Enter two numbers:
96           65
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

Back to Program.