Imagine a publishing company that markets both book and audiocassette versions of its works. Create a class publication 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 (type int), and tape, which adds a playing time in minutes (type float). Each of these three classes should have a getdata() function to get its data from the user at the keyboard, and a 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 data with getdata(), and then displaying the data with putdata().
#include <iostream>
#include <string>
using namespace std;
class Publication{
protected:
string title = "";
float price;
public:
Publication(){
getdata();
}
void getdata(){
cout<<"\nInput title: ";
char c = cin.get();
getline(cin, title);
if(c != '\n') title = c + title;
cout<<"Input price: ";
cin>>price;
}
};
class Book: public Publication{
int page_count;
public:
Book():Publication(){
cout<<"Input number of pages: ";
cin>>page_count;
}
void putdata(){
cout<<"\nType: Book";
cout<<"\nTitle: "<<title;
cout<<"\nPrice: "<<price;
cout<<"\nNumber of pages: "<<page_count<<endl;
}
};
class Tape:public Publication{
float play_time;
public:
Tape():Publication(){
cout<<"Input play time in mimutes: ";
cin>>play_time;
}
void putdata(){
cout<<"\nType: Tape";
cout<<"\nTitle: "<<title;
cout<<"\nPrice: "<<price;
cout<<"\nPlay time: "<<play_time<<endl;
}
};
int main(){
cout<<"Input book data:\n";
Book book;
cout<<"Input tape data:\n";
Tape tape;
book.putdata();
tape.putdata();
return 0;
}
Comments
Leave a comment