Program #10: Inheritance

This program will ask you to write several classes that make use of inheritances and a pointer wrapper. It has some similiarities to the Shape example. The instructions are very precise, and you need to follow them exactly.

What to Do

Define an abstract base class called Employee that contains a name (string), a social security number (string), a date of hire (Date), and the respective accessor functions. It contains a function called print whose task is to output to a designated ostream the social security number, name, and date of hire (in that order), and it also contains an overloaded operator< that orders employees by social security number. You should not use protected members. Include a three-parameter constructor, using initializer lists, and give all parameters defaults. The Employee class should also declare an abstract salary accessor and mutator, and it should provide a meaningful operator<<. You will need to define a destuctor.  In designing Employee, make sure you have correctly decided which functions need to be declared virtual (part of that decision may be determined by the next two paragraphs).

Next, derive a class called Hourly that adds a new data member to store an hourly wage (double). Its print function must print the social security number, name, date of hire, and salary (with phrase "per hour"). It will certainly want to call the base class print. Provide an accessor and mutator for the salary, and make sure that its constructor initializes a salary (how many parameters will the constructor need?).

Next, derive another class called Salaried that adds a new data member to store a yearly salary (double). Its print function must print the social security number, name, date of hire, and salary (with phrase "annual"). Provide an accessor and mutator for the salary, and make sure that its constructor initializes a salary.

Continue by creating a class called PtrToEmployee that stores a pointer to an Employee. It should provide print, operator<<, and operator< all of which apply the corresponding function to the Employee that is being pointed at, as well as a constructor that can be called to have the pointer point somewhere (presumably the result of a new). The constructor's default parameter should be NULL. Here is the class declaration for PtrToEmployee (seeing it may give you hints for some other stuff).

class PtrToEmployee
{
  public:
    PtrToEmployee( Employee *p = NULL );

    void print( ostream & out = cout ) const;
    bool operator< ( const PtrToEmployee & rhs ) const;

  private:
    Employee *pointee;
};

ostream & operator<< ( ostream & out, const PtrToEmployee & rhs );

In order to hold all employees, create a class called Roster that is able to hold a variable number of PtrToEmployee objects. Roster should have a set of PtrToEmployee as its data member. The set should start with an initial size of 0, and can be expanded by calls to insert. Provide the capability to add an employee and print the entire roster of employees. The printing routine must print the employees in sorted order (as defined by Employee this means in increasing social security number).

To summarize, Roster has public methods add and print, and, as usual, an overloaded operator<<.

You should then be able to write a program that includes the following main:

int main( )
{
    Roster r;

    r.add( new Hourly( "Jim", "400-00-0000", Date( "Nov", 1, 1992 ), 15.60 ) );
    r.add( new Salaried( "Alex", "500-00-0000", Date( "Oct", 1, 1994 ), 81090 ) );
    r.add( new Hourly( "Mark", "600-00-0000", Date( "Jul", 1, 1983 ), 21.75 ) );
    r.add( new Hourly( "Bill", "100-00-0000", Date( "Jan", 1, 1990 ), 10.90 ) );
    r.add( new Salaried( "Bob", "200-00-0000", Date( "Jun", 1, 1993 ), 61090 ) );
    r.add( new Salaried( "Joe", "300-00-0000", Date( "May", 1, 1996 ), 31690 ) );
    r.add( new Salaried( "Tim", "700-00-0000", Date( "Jun", 1, 1973 ), 111577 ) );
 
    cout << r << endl;

    return 0;
}

Due Date

This program is due on Friday, December 3, at 5PM. Submit all your source code (you need not resubmit the Date class), and sample output obtained by a test that is at least as inclusive as the main above.