Imagine BAGADI - a publishing company that markets both books, Tape/drive (audiocassette and Videocassette) 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/drive classes by creating instances of them, asking the user to fill in data with getdata(), and then displaying the data with putdata() as per the order based on the user choice either playing time or number of pages. (Try to use the concept of Constructors, Destructors & Function Overloading).
#include <iostream>
#include <string>
using namespace std;
class publication
{
private:
// stores the title (a string) and price (type float) of a publication.
string title;
float price;
public:
publication(){}
//Destructor
~publication(){}
// a getdata() function to get its data from the user at the keyboard
virtual void getdata()
{
cout << "Enter a title of publication: ";
cin >> title;
cout << "Enter a price of publication: ";
cin >> price;
}
//putdata() function to display its data.
virtual void putdata()
{
cout << "The publication title: " << title << endl;
cout << "The Publication price: " << price<<endl;
}
};
// book, which adds a page count (type int),
class book :public publication
{
private:
int pagecount;
public:
book(){}
//Destructor
~book(){}
void getdata()
{
publication::getdata(); //call publication class function to get data
cout << "Enter Book Page Count: "; //Acquire book data from user
cin >> pagecount;
}
void putdata()
{
publication::putdata(); //Show Publication data
cout << "Book page count: " << pagecount << endl; //Show book data
}
};
class tape :public publication
{
private:
// and tape, which adds a playing time in minutes (type float)
float playingtime;
public:
tape(){}
//Destructor
~tape(){}
void getdata()
{
publication::getdata();
cout << "Enter tape playing time: ";
cin >> playingtime;
}
void putdata()
{
publication::putdata();
cout << "Tape's playing time: " << playingtime << endl;
}
};
int main(void){
book newBook;
tape newTape;
newBook.getdata();
newTape.getdata();
newBook.putdata();
newTape.putdata();
system("pause");
return 0;
}
Comments
Leave a comment