A publication company that markets both book and audio-cassettes. Create a class “publication” that stores the title and price. From this class derive two classes “book” which adds a page count and “tape” which adds a playing time in minutes. Each of the three classes have a display function to display the details of the book and tape. Implement this with the use of runtime polymorphism.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class publication
{
   protected:
      char title[30];
      float price;
   public:
      publication(char *s, float a){strcpy(title,s);price=a;}
      virtual void display(){}
};
class book: public publication
{
      int pages;
   public:
      book(char *s, float a, int p):publication(s,a){pages=p;}
      void display();
};
class tape: public publication
{
      float time;
   public:
      tape(char *s, float a, float t):publication(s,a){time=t;}
      void display();
};
void book::display(){cout<<endl<<"    Title : "<<title
    <<endl<<"    Pages : "<<pages
    <<endl<<"    Price : "<<price;}
void tape::display(){cout<<endl<<"    Title : "<<title
    <<endl<<"Play Time : "<<time<<" Minutes"
    <<endl<<"    Price : "<<price;}
void main()
{
   char * title = new char[30];
   float price, time;
   int pages;
   clrscr();

   // Book Details
   cout<<endl<<"Enter Book Details"<<endl;
   cout<<"Title : ";gets(title);
   cout<<"Price : ";cin>>price;
   cout<<"Pages : ";cin>>pages;

   book book1(title,price,pages);

   // Tape Details
   cout<<endl<<"Enter Tape Details"<<endl;
   cout<<"Title : ";gets(title);
   cout<<"Price : ";cin>>price;
   cout<<"Play Time(in Mins) : ";cin>>time;

   tape tape1(title,price,time);

   publication* list[2];
   list[0]=&book1;
   list[1]=&tape1;

   cout<<endl<<"------------------------------";
   cout<<endl<<"Media Details";
   cout<<endl<<"-------------";
   cout<<endl<<"Book Details";
   cout<<endl<<"------------------------------";
   list[0]->display();
   cout<<endl<<"==============================";
   cout<<endl<<"Tape Details";
   cout<<endl<<"------------------------------";
   list[1]->display();
   cout<<endl<<"==============================";
   getch();
}

No comments:

Post a Comment