EXERCISES - A SOLUTION

 

6.    Imagine a publishing company that markets both book and audio cassette versions of its works. Create a class Cpublication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: cbook which adds a page count (type int) and ctape which adds a playing time in minutes (type float). Each of these three classes should have a get() function to input data from the keyboard and a put() 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 get and then displaying the data with put.

 

PUBLISH.H

 

#if !defined (PUBLICATION_SEEN)

 

#define PUBLICATION_SEEN

 

class CPublication

{

protected:

 

char m_szTitle [40];

float m_fPrice;

 

public:

void get (void);

void put (void);

};

 

#endif

 

BOOK.H

 

#include "publish.h"

 

class CBook : public CPublication

{

protected:

int m_iPageCount;

 

public:

void get (void);

void put (void);

};

 

TAPE.H

 

#include "publish.h"

 

class CTape : public CPublication

{

protected:

float m_fPlayTime;

 

public:

void get (void);

void put (void);

};

 

 

 

 

PUBLISH.CPP

 

#include <iostream.h>

 

#include "publish.h"

 

void CPublication::get (void)

{

cout << "Enter title ";

cin >> m_szTitle;

cout << endl;

 

cout << "Enter price ";

cin >> m_fPrice;

cout << endl;

}

 

void CPublication::put (void)

{

cout << "Title " << m_szTitle << endl;

cout << "Price " << m_fPrice << endl;

}

 

BOOK.CPP

 

#include <iostream.h>

 

#include "book.h"

 

void CBook::get (void)

{

CPublication::get ();

 

cout << "Enter number of pages ";

cin >> m_iPageCount;

cout << endl;

}

 

void CBook::put (void)

{

CPublication::put ();

cout << "Pages " << m_iPageCount << endl;

}

 

TAPE.CPP

 

#include <iostream.h>

 

#include "tape.h"

 

void CTape::get (void)

{

CPublication::get ();

 

cout << "Enter play time ";

cin >> m_fPlayTime;

cout << endl;

}

 

void CTape::put (void)

{

CPublication::put ();

cout << "Play time " << m_fPlayTime << endl;

}

 

MAIN.CPP

 

#include "book.h"

#include "publish.h"

#include "tape.h"

 

void main ()

{

CBook aBook;

CPublication aPublication;

CTape aTape;

 

aBook.get ();

aBook.put ();

 

aPublication.get ();

aPublication.put ();

 

aTape.get ();

aTape.put ();

}

 

 

 

 

Tuition

home
home
tuition
index
Last updated: 11th July 2006.
copyright © 2006 Greystoke Systems Ltd.
Web address: http://www.gsys.biz/Documents/Services/Tuition/CityAndGuilds/7261-249/ExerciesSolution.htm