Thursday 17 August 2017

Assume that the publisher in Exercises 1 and 3 decides to add a third way to distribute books: on computer disk, for those who like to do their reading on their laptop. Add a disk class that, like book and tape, is derived from publication. The disk class should incorporate the same member functions as the other classes. The data item unique to this class is the disk type: either CD or DVD. You can use an enum type to store this item. The user could select the appropriate type by typing c or d.




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 << "Title:\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;
                        }
};
class disk : public publication
{
            private:
                        char c;
            public:
                        void getdata( )
                        {
                                    cout << "Enter Type of Disk (c for CD/d for DVD): ";
                                                cin >> c;
                                    switch(c)
                                    {
                                                case 'c':           case 'C':
                                                            cout << "Enter Title of the CD: ";
                                                            publication :: getdata( );                      break;
                                                case 'd':           case 'D':
                                                            cout << "Enter Title of the DVD: ";
                                                            publication :: getdata( );                      break;
                                    }
                        }
                        void putdata( )
                                    {           publication :: putdata( );          }
};
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            book book1;                tape tape1;                  disk disk1;
            book1.getdata( );                     cout << endl;
            tape1.getdata( );                      cout << endl;
            disk1.getdata( );                      cout << endl;
            book1.putdata( );                     tape1.putdata( );                      disk1.putdata( );
            system("pause");
}
OUTPUT:

0 comments: