Imagine a publishing company that markets both book and audiocassette versions of its work. 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 page count (type int) and author (a string); 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 keyboard, and a putData() function to display its data. getData() and putData() should be overloaded in both derived class.
Add a member function having return type bool called isOversize() to the book and tape classes. Let’s say that a book with more than 800 pages, or a tape with a playing time longer than 90 minutes (which would require two cassettes), is considered oversize. You can access this function from main() and display the string “Oversize” for oversize books and tapes when you display their other data.
#include <iostream>
#include <string>
using namespace std;
class publication
{
private:
string title;
float price;
public:
publication(){
}
~publication(){}
virtual void getdata()
{
cout << "Enter a title of publication: ";
cin >> title;
cout << "Enter a price of publication: ";
cin >> price;
}
virtual void putdata()
{
cout << "The publication title: " << title << "\n";
cout << "The Publication price: " << price<<"\n";
}
};
class book :public publication
{
private:
int pagecount;
public:
book(){}
~book(){}
void getdata()
{
publication::getdata();
cout << "Enter Book Page Count: ";
cin >> pagecount;
}
void putdata()
{
publication::putdata();
cout << "Book page count: " << pagecount <<"\n";
}
bool isOversize(){
if(pagecount>800){
return true;
}
return false;
}
};
class tape :public publication
{
private:
float playingtime;
public:
tape(){}
~tape(){}
void getdata()
{
publication::getdata();
cout << "Enter tape playing time: ";
cin >> playingtime;
}
void putdata()
{
publication::putdata();
cout << "Tape's playing time: " << playingtime << "\n";
}
bool isOversize(){
if(playingtime>90){
return true;
}
return false;
}
};
int main(void){
book* newBook=new book();
newBook->getdata();
tape* newTape=new tape();
newTape->getdata();
cout<<"\n";
newBook->putdata();
if(newBook->isOversize()){
cout<<"Oversize\n\n";
}
newTape->putdata();
if(newTape->isOversize()){
cout<<"Oversize\n\n";
}
delete newBook;
delete newTape;
system("pause");
return 0;
}
Comments
Leave a comment