+2 votes
in Class 12 by kratos

Imagine a publishing company that markets both books and audio-cassette versions of its works. Create a class called Publication that stores the title (a string) and price 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 the three class should have a getdata() function to get its data from the user at the keyboard, and a putdata() function to display the data. Write a main() program 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 a object of type Book or Tape to hold the data. Put the pointer to the object in the data for all books and tapes, display the resulting data for all the books and taps entered, using a for loop and a single statement such as

pubarr[i]->putdata();

to display the data from each object in the array.

1 Answer

+4 votes
by kratos
 
Best answer

include<iostream.h>

include<string.h>

class Publication

{

private:

char title[20];

float price;

public:

void getName()

{

cout<<"Enter Title: "; cin>>title;

cout<<"Enter Price: $"; cin>>price;

}

void putName()

{

cout<<"\nTitle: "<<title;

cout<<", Price: $"<<price;

}

virtual void getData() = 0;

};

class Book : public Publication

{

private:

int pages;

public:

void getData()

{

Publication::getName();

cout<<"Enter Pages: "; cin>>pages;

}

void putData()

{

Publication::putName();

cout<<", Pages: "<<pages<<end1;

}

};

class Tape : public Publication

{

private:

float minutes;

public:

void getData()

{

Publication::getName();

cout<<"Enter Minutes: "; cin>>minutes;

}

void putData()

{

Publication::putName();

cout<<", Minutes: "<<minutes<<end1;

}

};

int main()

{

Publication* ptrPub[100];

int n = 0;

char choice;

do

{

cout<<"Book or Tape? (b/t): "; cin>>choice;

if(choice == 'b')

{ ptrPub[n] = new Book; ptrPub[n]->getData(); }

else

{ ptrPub[n] = new Tape; ptrPub[n]->getData(); }

n++; cout<<"Enter another? (y/n): "; cin>>choice;

} while(choice == 'y');

for(int i=0; i<n; i++)

ptrPub[i]->putName();

cout<<end1;

return 0;

}

...