Book class 2

Imagine a publishing company that markets both books and audio- cassette version of its works. Create a class Publication in C++ that stores the title (a string) and price (type float) of a publication. From this class derive two classes: Book, which adds a page count and Tape, which adds playing time in minutes. These classes should have getdata() function to get its data from the user and the putdata() function to display its data. Write a main() program to test the book and tape classes by creating instances of them, asking the user to fill in their data with getdata() and displaying the data with putdata().

Input Format

1st line Enter title of publication 2nd line Enter price of publication 3rd line Enter Book Page Count 4th line Enter title of publication 5th line Enter price of publication 6th line Enter tap’s playing time

Constraints

Price of the publication and pages of book must not be negative.

Output Format

If Price of the publication and pages of book are not negative then print the read records else print -1 instead of whole record.

Publication title Publication price Book page count Publication title Publication price Tap’s playing time

Sample Input 0

Sample Output 0

include 
 include 
 include 
 include 
 include 
 using namespace std;
 class Publication
 {
     /private:     string title;     float price;/
     public:
     string title;
     float price;
     Publication()
     {
         title="";
         price=0;
     }
     Publication(string s,float p)
     {
         title=s;
         price=p;
     }
     void get_data()
     {
         cin>>title;
         cin>>price;
     }
     void show_data()
     {
         if(price<0)
         {
             cout<<"-1"<<endl;
         }
         else
         {
             cout<<title<<endl<<price<<endl;
         }
     }
     float return_price()
     {
         return price;
     }
 };
 class Book:public Publication
 {
     private:
     int pages;
     public:
     Book():Publication()
     {
         pages=0;
     }
     Book(string s,float p,int pa):Publication(s,p)
     {
         pages=pa;
     }
     void get_data()
     {
         Publication::get_data();
         cin>>pages;
     }
     void show_data()
     {
         if(pages<0 || return_price()<0)
     {
             cout<<"-1"<<endl;
         }
         else
         {
         Publication::show_data();
         cout<<pages<<endl;
         }
     }
 };
 class Tape:public Publication
 {
     private:
     int minute;
     public:
     Tape():Publication()
     {
         minute=0;
     }
     Tape(string s,float p,int m):Publication(s,p)
     {
         minute=m;
     }
     void get_data()
     {
     Publication::get_data();
     cin>>minute;
     }
     void show_data()
     {
         if (price<0 || minute<0)
         {
         cout<<"-1"<<endl;
         }
         else
         {
         Publication::show_data();
         cout<<minute<<endl;
         }
     }
 };
 int main() {
     /* Enter your code here. Read input from STDIN. Print output to STDOUT */
     Book b;
     Tape t;
     b.get_data();
     t.get_data();
     b.show_data();
     t.show_data();
     return 0;
 }   

Leave a comment

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started