Thursday 17 August 2017

Start with the publication, book, and tape classes of Exercise 1. Add a base class sales that holds an array of three floats so that it can record the dollar sales of a particular publication for the last three months. Include a getdata() function to get three sales amounts from the user, and a putdata() function to display the sales figures. Alter the book and tape classes so they are derived from both publication and sales. An object of class book or tape should input and output sales data along with its other data. Write a main() function to create a book object and a tape object and exercise their input/output capabilities.




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 sales
{
            protected:
                        float t_sales[3];
            public:
                        void getdata( )
                        {
                                    for(int i = 0; i < 3; i++)
                                    {           cout << "Enter Sales of Month " << i+1 << ": ";         cin >> t_sales[i];          }
                        }
                        void putdata( )
                        {
                                    for(int i = 0; i < 3; i++)
                                                cout << "Sales for Month " << i+1 << ":\t$" << t_sales[i] << endl;
                        }
};
class book : public publication, public sales
{
            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;
                                    sales::getdata( );
                        }
                        void putdata( )
                        {
                                    publication :: putdata( );
                                    cout << "Page Count:\t" << page_count << endl;
                                    sales::putdata( );
                        }
};
class tape : public publication, public sales
{
            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;
                                    sales::getdata( );
                        }
                        void putdata( )
                        {
                                    publication :: putdata( );
                                    cout << "Play Time:\t" << play_time << "minutes" << endl;
                                    sales::putdata( );
                        }
};
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            book book1;                tape tape1;
            book1.getdata( );         cout << endl;               tape1.getdata( );
            book1.putdata( );         cout << endl;               tape1.putdata( );
            system("pause");
}
OUTPUT:

0 comments: