Starting from:
$35

$29

Programming Project #7: An Employee Class Hierarchy Solution

Background

The code that you wrote for the second Employee project only works for hourly employees. Now that we have studied polymorphism, we can begin to make our payroll program more general. The most important thing we will do is enable polymorphism in our Employee class hierarchy, so that employee objects of different types automatically behave correctly.

Objectives

    • Continue gaining experience with object-oriented design

    • Learn to create a class hierarchy that is enabled for polymorphism

    • Learn to use polymorphism in a program.

The Program

We will rename Employee to HourlyEmployee, and then create two new classes, Employee and SalariedEmployee, as depicted in the class diagram below.












































(Items preceded by dashes are private, those preceded by a # are protected, and public members start with a +. Static members appear underlined.)

Employee is an abstract class, meaning that it exists to hold what is common to all its derived classes, and is not to be instantiated directly by users. For this reason, it has no public constructors.

In addition to defining all things common to the derived employee classes, Employee defines a virtual destructor and declares four pure virtual functions, which are overridden in the derived classes (see the italicized functions in Employee above). All of these functions have bodies in Employee except calcPay, and are called explicitly from their derived counterparts. Note that readData functions as well as default constructors have protected access. This is because we don't want users to create instances of the concrete, derived employee objects directly without complete
information.

Also, remember that base classes must always have a virtual destructor, even if the body of that destructor is empty (which is what = default accomplishes here). Finally, the parameterized constructors for the derived classes take all pertinent parameters for objects of their type (which includes data common to all employees).

The key functions work as follows:

read

This static member function creates an empty, derived employee object and then calls readData to initialize it. It returns a pointer to a new object, if data was read correctly, or nullptr if there was an input error. For example, HourlyEmployee::read creates a new, empty HourlyEmployee object on the heap, emp, say. and then calls emp->readData. HourlyEmployee::readData calls Employee::readData to read in the common employee fields from a file, and then reads in the hourly wage and hours worked. The pointer to the initialized object (or nullptr) is then returned.


readData

As explained above, this function first calls the base class implementation


of readData, and then reads data from the file for its type of object (HourlyEmployee or SalariedEmployee).

write

This function works similarly to readData: it first calls Employee::write to write out the common data to the file, then writes out the specific derived class data.


printCheck

This function first calls Employee::printCheck to print the common check header, then prints the rest of the check according to the type of the object. See the sample output below to see how SalariedEmployee checks should look.


calcPay

Hourly Employees: An hourly employee's gross pay is calculated by multiplying the hours worked by their hourly wage. Be sure to give time-and-a-half for overtime (anything over 40 hours). To compute the net pay, deduct 20% of the gross for Federal income tax, and 7.5% of the gross for state income tax.

Salaried Employees: A salaried employee's gross pay is just their weekly salary value. To compute the net pay for a salaried employee, deduct 20% of the gross for Federal income tax, 7.5% of the gross for state income tax, and 5.24% for benefits.
The main() Function

Your driver will provide the same set of options as in the previous project. However, your driver code should be somewhat simpler because you will now create a vector of Employee pointers, and store the pointers to your objects in this vector. Add heap pointers to the following employees to your vector:

HourlyEmployee(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00);

SalariedEmployee(2, "A. Dumbledore", "Hogwarts", "803-1230", 1200);

HourlyEmployee(3, "R. Weasley", "The Burrow", "892-2000", 40, 10.00);

SalariedEmployee(4, "R. Hagrid", "Hogwarts", "910-8765", 1000);


You can then use a simple for-loop to save each object's data to the file.

Here is a sample execution of Option 1:

This program has two options:


1 - Create a data file, or

2 - Read data from a file and print paychecks.

Please enter (1) to create a file or (2) to print checks:1

Please enter a file name: employee.txt

Data file created ... you can now run option 2.


The contents of the file employee.txt should be:

1


    H. Potter

Privet Drive 201-9090 40 12 2


A. Dumbledore Hogwarts 803-1230 1200 3


R. Weasley The Burrow 892-2000
40


10

4

    R. Hagrid

Hogwarts 910-8765 1000

When you run option 2, your program should create a new vector of Employee pointers. Make four calls to

either HourlyEmployee::read or SalariedEmployee::read, according to the same order that you wrote the objects in part 1. Add the pointers returned by read to your vector. Then loop through the vector printing out the checks. Here is an execution of Option 2:

This program has two options:


1 - Create a data file, or

2 - Read data from a file and print paychecks.

Please enter (1) to create a file or (2) to print checks:2 Please enter a file name: employee.dat

....................UVU Computer Science

Dept.................................

Pay to the order of H.
$348.00
Potter....................................



United Community Credit Union

................................................................

..............






Hours worked: 40.00

HourlyWage: 12.00


....................UVU Computer Science

Dept.................................
Pay to the order of A.


Dumbledore....................................$807.12


United Community Credit Union

................................................................

..............






Salary: 1200.00

....................UVU Computer Science

Dept.................................

Pay to the order of R.
$290.00
Weasley....................................



United Community Credit Union

................................................................

..............






Hours worked: 40.00

HourlyWage: 10.00


....................UVU Computer Science

Dept.................................

Pay to the order of R.
$672.60
Hagrid....................................



United Community Credit Union

................................................................

..............





Salary: 1000.00


From the end user's point of view, your program will work just as it did before.

More products