Imagine a publishing company that markets both books and audio-cassette versions of its works. Create a class publication that stores the title (a string) ad 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 their data with getdata(), and then displaying the data with putdata()Read more on Sarthaks.com - https://www.sarthaks.com/436087/imagine-publishing-company-markets-both-books-audio-cassette-versions-works-create-class
#include <iostream>
using namespace std;
class Publication {
protected:
string title;
float price;
public:
// A virtual function, may or may not be overriden by child
virtual void getdata() {
cout << "Enter the tile: ";
cin >> this -> title;
cout << "Enter the price: ";
cin >> this -> price;
};
virtual void putdata() {
cout << "Entered data:" << endl;
cout << "\tTitle: " << this -> title << endl;
cout << "\tPrice: " << this -> price << endl;
};
};
class Book: public Publication {
protected: int page_count;
public: void getdata() {
// Calling Publication's getdata()
Publication::getdata();
cout << "Enter the number of pages: ";
cin >> this -> page_count;
};
void putdata() {
// Calling Publication's putdata()
Publication::putdata();
cout << "\tPage count: " << this -> page_count << endl;
};
};
class Tape: public Publication {
protected: float playing_time;
public: void getdata() {
// Calling Publication's getdata()
Publication::getdata();
cout << "Enter the playing time: ";
cin >> this -> playing_time;
};
void putdata() {
// Calling Publication's putdata()
Publication::putdata();
cout << "\tPlaying Time: " << this -> playing_time << endl;
};
};
// An enum can only hold integer values, here CD is 0, and DVD is 1.
enum DiskType {
CD,
DVD
};
// An array to store String mapping of enum values
const string disktype_strings[] = {
"CD",
"DVD"
};
class Disk: public Publication {
protected: enum DiskType dtype;
public: void getdata() {
// Calling Publication's getdata()
Publication::getdata();
cout << "Enter disktype (C for CD or D for DVD): ";
char ch;
cin >> ch;
if (ch == 'c' || ch == 'C') {
dtype = CD;
cout << dtype;
} else if (ch == 'd' || ch == 'D') {
dtype = DVD;
} else {
cout << "Please enter a valid input.";
}
};
void putdata() {
// Calling Publication's putdata()
Publication::putdata();
cout << "\tDisktype: " << disktype_strings[this -> dtype] << endl;
};
};
int main() {
cout << "-----------Testing Publication----------\n";
Publication p;
p.getdata();
p.putdata();
cout << "-----------Testing Book----------\n";
Book b;
b.getdata();
b.putdata();
cout << "-----------Testing Tape----------\n";
Tape t;
t.getdata();
t.putdata();
cout << "-----------Testing Disk----------\n";
Disk d;
d.getdata();
d.putdata();
return 0;
}
Comments
Leave a comment