Sunday, January 10, 2021

Imagine a publishing company which does marketing for book and audio cassette versions. Create a class publication that stores the title (a string) and price (type float) of publications. From this class derive two classes: book which adds a page count (type int) and tape which adds a playing time in minutes (type float). Write a program that instantiates the book and tape class, allows user to enter data and displays the data members.


/*  Imagine a publishing company which does marketing for book and audio cassette versions. Create a class publication that stores the title (a string) and price (type float) of publications.
From this class derive two classes: book which adds a page count (type int) and tape which adds a playing time in minutes (type float). Write a program that instantiates the book and tape class, allows user to enter data and displays the data members. */

Algorithm:                                                                                                                                           

Step 1: Start the program                                                                                                                        

Step 2: Create a base class publication.                                                                                                       

Step 3: Declare title and price data members as protected members.                                                          

Step 4: Define a default and parameterised  constructor in a base class to initialize data members(i.e title and price)                                                                                                                                                Step 5: Create a derived class book which has pagecount data member.                                                      

Step 6: Define a constructor of book class which has 3 parameters from which 2 are used to initialize data members of base class and one for its own.                                                                                         

Step 7:Define a display function to display a members of book (title and price inherited from base class publication and pagecount(its own))                                                                                                                       

Step 8: Follow the same steps for CD class



#include <iostream>
#include<string>
using namespace std;

class publication
{
    protected:
    string title;
    float price;
   
    public:
    publication()
    {
        price=0.0;
        title=" ";
     }
        publication(string t,float p)
        {
            title=t;
            price=p;
       }   
};
   
class book : public publication
    {
         int pagecount;
                  public:
        book()
        {
            pagecount=0;
        }
        
        book(string t,float p, int pc):publication(t,p)
        {
                 pagecount=pc;
          }
             void display()
     {
        cout<<"title :"<<title<<endl;
                cout<<"Price: "<<price<<endl;
       
          cout<<"Pagecount :"<<pagecount<<endl;
       }    };

 
class CD : public publication
    {
        float time;
  public:
              CD()
        {
            time=0.0;
         }
        
    CD(string t,float p,float tim):publication(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 data"<<endl;
   
    book b("C++",230,500);
    b.display();

    cout<<endl<<"CD Data"<<endl;
    
    CD c("programming",50,120.5);
    c.display();

     return 0;
}





No comments:

Post a Comment