Imagine a publishing company that markets both book and
audiocassette 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 classes by
creating instances of them, asking the user to fill in data with getdata(), and
then displaying the data with putdata( ).
SOLUTION:
#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////
class publication
{
protected:
char
title[500];
float
price;
public:
publication
( ) : price(0.0)
{
}
void
getdata( )
{
cin
>> title;
cout
<< "Enter Price of the Publication: ";
cin
>> price;
}
void
putdata( )
{
cout
<< "\nTitle:\t\t" << title << endl;
cout
<< "Price:\t\t$ " << price << endl;
}
};
class book : public publication
{
private:
int
page_count;
public:
book(
) : publication( ), page_count(0)
{ }
void
getdata( )
{
cout
<< "Enter Title of the BOOK: ";
publication
:: getdata( );
cout
<< "Enter Page Count of Book: ";
cin
>> page_count;
}
void
putdata( )
{ publication :: putdata( ); cout << "Page
Count:\t" << page_count << endl; }
};
class tape : public publication
{
private:
float
play_time;
public:
tape(
) : publication( ), play_time(0.0)
{
}
void
getdata( )
{
cout
<< "Enter Title of the TAPE: ";
publication
:: getdata( );
cout
<< "Enter Playing Time of Tape (in minutes): ";
cin
>> play_time;
}
void
putdata( )
{
publication
:: putdata( );
cout
<< "Play Time:\t" << play_time << "
minutes" << endl;
}
};
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
book
book1;
tape
tape1;
book1.getdata(
);
cout
<< endl;
tape1.getdata(
);
book1.putdata(
);
cout << endl;
tape1.putdata( );
system("pause");
}
OUTPUT:
0 comments: