Thursday 17 August 2017

Derive a class called employee2 from the employee class in the EMPLOY program in this chapter. This new class should add a type double data item called compensation, and also an enum type called period to indicate whether the employee is paid hourly, weekly, or monthly. For simplicity you can change the manager, scientist, and laborer classes so they are derived from employee2 instead of employee. However, note that in many circumstances it might be more in the spirit of OOP to create a separate base class called compensation and three new classes manager2, scientist2, and laborer2, and use multiple inheritance to derive these three classes from the original manager, scientist, and laborer classes and from compensation. This way none of the original classes needs to be modified. SOLUTION:





SOLUTION:
#include "stdafx.h"
#include "iostream"
using namespace std;
const int LEN = 80;
/////////////////////////////////////////////////////////////////////////////////////////
class employee
{
            private:
                        char name[LEN];
                        unsigned long number;
            public:
                        void getdata( )
                        {
                                    cout << "\n Enter last name: "; cin >> name;
                                    cout << " Enter number: "; cin >> number;
                        }
                        void putdata( ) const
                        {
                                    cout << "\n Name: " << name;
                                    cout << "\n Number: " << number;
                        }
};
class employee2 : public employee
{
            private:
                        double compensation;
                        enum period { hourly, weekly, monthly };
                        char ch;
            public:
                        void getdata( )
                        {
                                    cout << " Enter Compensation: ";
                                                cin >> compensation;
                                    cout << " Enter Period of Compensation (hourly(h), weekly(w), monthly(m)):" ;
                                                cin >> ch;
                        }
                        void putdata( )
                        {
                                    cout << "\n Compensation: $" << compensation;
                                    period p;
                                    switch(ch)
                                    {
                                                case 'h':           case 'H':          p = hourly;                   break;
                                                case 'w':          case 'W':         p = weekly;                 break;
                                                case 'm':          case 'M':          p = monthly;    break;
                                    }
                                    switch(p)
                                    {
                                                case 0:            cout << "\n Period: Hourly";                break;
                                                case 1:            cout << "\n Period: Weekly";              break;
                                                case 2:            cout << "\n Period: Monthly";              break;
                                    }
                        }
};
class manager : public employee2
{
            private:
                        char title[LEN];            double dues;
            public:
                        void getdata( )
                        {
                                    employee::getdata( );
                                    cout << " Enter title: ";
                                                 cin >> title;
                                    employee2::getdata( );
                                    cout << " Enter golf club dues: ";
                                                 cin >> dues;
                        }
                        void putdata( )
                        {
                                    employee::putdata( );
                                    cout << "\n Title: " << title;
                                    employee2::putdata( );
                                    cout << "\n Golf club dues: " << dues;
                        }
};
class scientist : public employee2
{
            private:
                        int pubs;
            public:
                        void getdata( )
                        {
                                    employee::getdata( );
                                    employee2::getdata( );
                                    cout << " Enter number of pubs: ";
cin >> pubs;
                        }
                        void putdata( )
                        {
                                    employee::putdata( );
                                    employee2::putdata( );
                                    cout << "\n Number of publications: " << pubs;
                        }
};
class laborer : public employee2
{
            public:
                        void getdata( )
                        {          
employee::getdata( );              employee2::getdata( );
}
                        void putdata( )
                        {
                                    employee::putdata( );              employee2::putdata( );
                        }
};
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            manager m1;              scientist s1;                 laborer l1;
            cout << "Enter data for manager 1";              m1.getdata( );
            cout << "\nEnter data for scientist 1";             s1.getdata( );
            cout << "\nEnter data for laborer 1";               l1.getdata( );
            cout << "\nData on manager 1";                     m1.putdata( );
            cout << "\nData on scientist 1";                      s1.putdata( );
            cout << "\nData on laborer 1";                        l1.putdata( );
            cout << endl;
            system("pause");
}
OUTPUT:

1 comment:

  1. Maybe Github would be the place to post your source code.

    If it was a decent post about linked lists that actually had plain text explaining what it's doing and why it would be of use.

    Currently it just looks like a bit of meaningless copy and paste from some homework sheet.

    There are not even comments in the code. Very unprofessional, very amature.

    ReplyDelete