A publishing company markets both books and audiocassette versns of its works. Create a class called publicasn that stores the title (a string) and price (type float) of a publicsn. 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 the 3 class should have a getdata() funsn to get its data from the user, and a putdata() funsn to display data. Also include two constants publisher name (a string) and address (type int) in the class publication. These constants should be initialized at the time of instantiating an object of class publicasn.
Wap main() that creates an array of pointers to publication. In a loop ask the user for data about a particular book or tape, and use new to create an object of type book or tape to hold the data. Put the pointer to the object in the array. When the user has finished entering the data for all books and tapes, display the resulting data for all books and tapes entered using a loop.
#include <iostream>
#include <string>
using namespace std;
class publication
{
private:
string title;
float price;
public:
// two constants publisher name (a string) and address (type int) in the class publication.
static const string publisherName;
static const int address;
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";
}
};
string const publication::publisherName="Publisher Name";
int const publication::address=175;
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";
}
};
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";
}
};
int main(void){
//creates an array of pointers to publication.
int totalNumberPublications=0;
cout<<"Enter the total number of publications: ";
cin>>totalNumberPublications;
publication** publications=new publication*[totalNumberPublications];
cout<<publication::publisherName<<"\n";
cout<<"Publication address: "<<publication::address<<"\n";
for(int i=0;i<totalNumberPublications;i++){
int ch=-1;
publication* newPublication=NULL;
while(ch<1 || ch>2){
cout<<"1. Add a new book\n";
cout<<"2. Add a new tape\n";
cout<<"> ";
cin>>ch;
}
if(ch==1){
newPublication=new book();
newPublication->getdata();
}else{
newPublication=new tape();
newPublication->getdata();
}
publications[i]=newPublication;
}
//display data
for(int i=0;i<totalNumberPublications;i++){
publications[i]->putdata();
cout<<"\n";
delete publications[i];
}
delete[] publications;
system("pause");
return 0;
}
Comments
Leave a comment