Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*program to implement runtime polymorphism*/
- #include<conio.h>
- #include<iostream>
- #include<string.h>
- using namespace std;
- class media
- {
- protected:char title[30];
- float price;
- public :media(char *s,float a)
- {
- strcpy(title,s);
- price=a;
- }
- virtual void display()
- {
- }
- };
- class book :public media
- {
- int pages;
- public :book(char *s,float a,int p):media(s,a)
- {
- pages=p;
- }
- void display()
- {
- cout<<"\nTitle: "<<title;
- cout<<"\nPages: "<<pages;
- cout<<"\nPrice: "<<price;
- }
- };
- class tape :public media
- {
- float time;
- public:tape(char *s,float a,float t):media(s,a)
- {
- time=t;
- }
- void display()
- {
- cout<<"\nTitle: "<<title;
- cout<<"\nRuntime: "<<time<<"hr";
- cout<<"\nPrice: "<<price;
- }
- };
- int main()
- {
- char *title=new char[30];
- float price,time;
- int pages;
- cout<<"\nBook details:"<<endl;
- cout<<"Enter Title:";
- cin>>title;
- cout<<"Enter Price:";
- cin>>price;
- cout<<"Enter Pages:";
- cin>>pages;
- book book1(title,price,pages);
- cout<<"\nTape details:"<<endl;
- cout<<"Enter Title:";
- cin>>title;
- cout<<"Enter Price:";
- cin>>price;
- cout<<"Enter Runtime in hours:";
- cin>>time;
- tape tape1(title,price,time);
- media *list;
- list=&book1;
- cout<<"Book details:"<<endl;
- list->display();
- list=&tape1;
- cout<<"Tape details:"<<endl;
- list->display();
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment