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>
class publication
{
protected:
std::string title;
double price;
};
class book : public publication
{
int page_count;
public:
void get_data()
{
std::cout << "Enter the book title: "<<std::endl;
std::getline(std::cin, title);
std::cout<<"Enter the price: ";
std::cin>>price;
std::cout<<"Enter page count: ";
std::cin>>page_count;
}
void put_data()
{
std::cout<<"Book: " << title <<" has "<<page_count<<" pages and costs "<< price<<std::endl;
}
};
class tape: public publication
{
int playing_time;
public:
void get_data()
{
std::cout << "Enter the tape title: "<<std::endl;
std::cin.ignore(1);
std::getline(std::cin, title);
std::cout<<"Enter the price: ";
std::cin>>price;
std::cout<<"Enter playing time: ";
std::cin>>playing_time;
}
void put_data()
{
std::cout<<"Tape: " << title <<" has "<<playing_time<<" playing time and costs "<< price<<std::endl;
}
};
int main()
{
book b;
b.get_data();
b.put_data();
tape t;
t.get_data();
t.put_data();
return 0;
}
Comments
Leave a comment