Answer to Question #272289 in C++ for Santhosh

Question #272289

Practice programs: Consider a book shop which sells both books and video tapes. Create a class media that stores the title and price of a publication. Derive two classes from media, one for storing the number of pages in a book and another for storing playing time of a tape. Write a C++ program using run time polymorphism to implement it.


1
Expert's answer
2021-11-27T11:34:45-0500
#include <iostream>
#include<string>
using namespace std;
class media
{
    protected:
    string title;
    float price;
   
    public:
    media()
    {
        title=" ";
        price=0.0;
        
        }
        media(string t,float P)
        {
            title=t;
            price=P;
            
            }
    };
    class book : public media
    {
        int P_count;
        public:
        book()
        {
            P_count=0;
            
        }
        book(string t,float P,int pc):media(t,P)
        {
            
            P_count=pc;
            
        }
     
     void display()
     {
        
          cout<<"title :"<<title<<endl;
                cout<<"Price: "<<price<<endl;
       
          cout<<"Pagecount :"<<P_count<<endl;
         
        }
        
    };


 class CD : public media
    {
        float time;
        public:
        CD()
        {
            time=0.0;
            
        }
        CD(string t,float p,float tim):media(t,p)
        {
            
            time=tim;
            
        }
     
     void display()
     {
         cout<<"title :"<<title<<endl;
                cout<<"Price: "<<price<<endl;
       
          cout<<"time in minutes :"<<time<<endl;
         
        }
        
    };
int main()
{
    cout<<endl<<"Book information"<<endl;
    book Bo("programming in java",1000,500);
    Bo.display();
    cout<<endl<<"video information"<<endl;
    CD cd("programming in c++",100,125);
    cd.display();
    return 0;
        }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS