Aim: Write a
program to demonstrate the runtime polymorphism.
Ans:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class
category
{
char animal[10],human_being[10];
public:
virtual void task()
{
cout<<"Enter
name of animal:";
cin>>animal;
cout<<"\n";
cout<<"Enter
name of human being:";
cin>>human_being;
cout<<"\n";
cout<<"Enter
name of animal:";
cin>>animal;
cout<<"\n";
cout<<"Enter
name of human being:";
cin>>human_being;
cout<<"\n";
}
};
class derived
:public category
{
char color[10],body_parts[10],i;
public:
void task()
{
cout<<"Enter
color:";
cin>>color;
cout<<"\n";
cout<<"Enter
body parts:";
cin>>body_parts;
cout<<"\n";
cout<<"Enter
color:";
cin>>color;
cout<<"\n";
cout<<"Enter
body parts:";
cin>>body_parts;
cout<<"\n";
}
};
void main()
{
clrscr();
category
*p,o;
derived o1;
p=&o;
p->task();
p=&o1;
p->task();
getch();
}
No comments:
Post a Comment