AbdulFathaah

Run time polymorphism

Mar 6th, 2024 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. /*program to implement runtime polymorphism*/
  2. #include<conio.h>
  3. #include<iostream>
  4. #include<string.h>
  5. using namespace std;
  6. class media
  7. {
  8. protected:char title[30];
  9. float price;
  10. public :media(char *s,float a)
  11. {
  12. strcpy(title,s);
  13. price=a;
  14. }
  15. virtual void display()
  16. {
  17. }
  18. };
  19. class book :public media
  20. {
  21. int pages;
  22. public :book(char *s,float a,int p):media(s,a)
  23. {
  24. pages=p;
  25. }
  26. void display()
  27. {
  28. cout<<"\nTitle: "<<title;
  29. cout<<"\nPages: "<<pages;
  30. cout<<"\nPrice: "<<price;
  31. }
  32. };
  33. class tape :public media
  34. {
  35. float time;
  36. public:tape(char *s,float a,float t):media(s,a)
  37. {
  38. time=t;
  39. }
  40. void display()
  41. {
  42. cout<<"\nTitle: "<<title;
  43. cout<<"\nRuntime: "<<time<<"hr";
  44. cout<<"\nPrice: "<<price;
  45. }
  46. };
  47. int main()
  48. {
  49. char *title=new char[30];
  50. float price,time;
  51. int pages;
  52.  
  53. cout<<"\nBook details:"<<endl;
  54. cout<<"Enter Title:";
  55. cin>>title;
  56. cout<<"Enter Price:";
  57. cin>>price;
  58. cout<<"Enter Pages:";
  59. cin>>pages;
  60. book book1(title,price,pages);
  61.  
  62. cout<<"\nTape details:"<<endl;
  63. cout<<"Enter Title:";
  64. cin>>title;
  65. cout<<"Enter Price:";
  66. cin>>price;
  67. cout<<"Enter Runtime in hours:";
  68. cin>>time;
  69. tape tape1(title,price,time);
  70.  
  71. media *list;
  72. list=&book1;
  73. cout<<"Book details:"<<endl;
  74. list->display();
  75. list=&tape1;
  76. cout<<"Tape details:"<<endl;
  77. list->display();
  78. getch();
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment