Thursday 17 August 2017

Start with the publication, book, and tape classes of Exercise 1. Suppose you want to add the date of publication for both books and tapes. From the publication class, derive a new class called publication2 that includes this member data. Then change book and tape so they are derived from publication2 instead of publication. Make all the necessary changes in member functions so the user can input and output dates along with the other data. For the dates, you can use the date class from Exercise 5 in Chapter 6, which stores a date as three ints, for month, day, and year.




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 publication2 : public publication
{
            private:
                        int month,day,year;
            public:
                        publication2( ) : month(0), day(0), year(0)                 { }
                        void getdata( )
                        {
                                    cout << "Enter Date of Publication (DAY, MONTH & YEAR Respectively): ";
                                    cin >> day >> month >> year;
                        }
                        void putdata( ) const
                        {
                                    cout << "Date is :\t"<< day << "/" << month << "/" << year << endl;
                        }
};
class book : public publication2
{
            private:
                        int page_count;
            public:
                        book( ) : publication2( ), page_count(0)                     { }
                        void getdata( )
                        {
                                    cout << "Enter Title of the BOOK: ";
                                    publication :: getdata( );                      publication2:: getdata( );
                                    cout << "Enter Page Count of Book: ";
                                                cin >> page_count;
                        }
                        void putdata( )
                        {
                                    publication :: putdata( );                      publication2:: putdata( );
                                    cout << "Page Count:\t" << page_count << endl;
                        }
};
class tape : public publication2
{
            private:
                        float play_time;
            public:
                        tape( ) : publication2( ), play_time(0.0)           { }
                        void getdata( )
                        {
                                    cout << "Enter Title of the TAPE: ";
                                    publication :: getdata( );                      publication2:: getdata( );
                                    cout << "Enter Playing Time of Tape (in minutes): ";             cin >> play_time;
                        }
                        void putdata( )
                        {
                                    publication :: putdata( );                      publication2:: 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: