Assignment #8
Problem Statement
This assignment has you play around with inheritance
and polymorphism.
What to Do
-
Define a base class called Employee that contains a name (string),
a social security number (string),
and the respective accessor functions. It contains a function called print
whose task is to output the name and social security number,
and an overloaded operator< that orders employees by name.
You should not use protected members. Include a two-parameter
constructor, using initializer lists, and give all parameters defaults.
- Next, derive a class called Hourly that adds a new data member
to store an hourly wage (double). Its print function
must print the name, social security number, 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.
- Next, derive another class called Salaried that adds a new
data member to store a yearly salary (double). Its print
function must print the name, social security number, and
salary (with phrase "annual"). Provide an accessor and mutator
for the salary, and make sure that its constructor initializes a salary.
Exercise the classes by declaring objects of type
Salaried
and Hourly via constructors and calling their
print methods.
- Provide a single operator<< that prints an
Employee (by calling print).
This method will automatically work for anything in the
Employee hierarchy.
- Make Employee abstract
by providing an abstract salary accessor and mutator, and then
decide which members should be virtual.
- Continue by creating a class called PtrToEmployee that stores
a pointer to an Employee. It should provide both print
and operator< which applies 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.
This class does not provide a destructor, since it does not
actually create new Employees.
- 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 multiset of PtrToEmployee
(ordered by name). Provide the capability to
add an employee, and print the entire roster
of employees. To add an employee, Roster::add
creates a PtrToEmployee object and calls multiset::insert.
Don't worry about error checks.
To summarize, Roster has public methods named
add
and print.
- Write a short test program, in which you create a Roster
object, call new for both
kinds of Employee, sending the result to Roster::add,
and output the Roster via a call to print.
What to Submit
Submit your source code for all classes, the final test program and
its output.