EXAMPLE PROGRAM OF RUNTIME POLYMORPHISM USIN CPP
#include<iostream.h>
#include<conio.h>
#include<string.h>
class m
{
protected :
char tit[50];
float rs;
public :
m(char *s,float a)
{
strcpy(tit,s);
rs=a;
}
virtual void display () {}
};
class book: public m
{
int pages;
public :
book (char *s,float a,int
p):m(s,a)
{
pages=p;
}
void display();
};
class tape :public m
{
float time;
public :
tape (char *s,float a,float
tt):m(s,a)
{
time=tt;
}
void display();
};
void book :: display()
{
cout<<" Title:"<<tit;
cout<<" Pages:"<<pages;
cout<<" Price:"<<rs;
}
void tape:: display()
{
cout<<"\n
Title:"<<tit;
cout<<"\n Play
time:"<<time<<"mins";
cout<<"\n
Price:"<<rs;
}
void main()
{
clrscr;
char*title;
float pr,ti;
int pg;
cout<<"\n Enter
book details";
cout<<"\n Title
:";
cin>>title;
cout<<"\nPrice
:";
cin>>pr;
cout<<"\n
Pages:";
cin>>pg;
book book1(title,pr,pg);
cout<<"\n Enter
tape details \n";
cout<<"\n Title
:" <<title;
cout<<"\n Price
:"<<pr;
cout<<"\n Play
time(mins) :";
cin>>ti;
tape tape1(title,pr,ti);
m*list[2];
list[0]=&book1;
list[1]=&tape1;
cout<<"\n Media
details";
cout<<"\n.........book.........";
list[0]->display();
cout<<"\n........tape.........";
list[1]->display();
getch();
}
OUTPUT:
ENTER BOOK DETAILS
TITLE: BILTECHNOLOGY
PRICE:180
PAGES:650
ENTER TAPE DETAILS:
TITLE:BILTECHNOLOGY
PRICE:180
PLAY TIME (MINS):120
MEDIA DETAILS:
..............BOOK............
TITLE:BIOTECHNOLOGY PAGES:650 PRICE:180
...................TAPE..........................
TITLE:BIOTECHNOLOGY
PLAY TIME:120MINS
PRICE:180
No comments: