Project #7 Solution

$30.00 $24.90

Description

Background

The code that you wrote for the previous 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 behave correctly automatically. We will rename Employee to HourlyEmployee, and then create two new classes, Employee and SalariedEmployee, as depicted in the UML diagram below.

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 construtors. In addition to defining all things common to the derived employee classes, it defines 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 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 form 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.

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 Driver

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.txt

——————–FluffShuffle Electronics——————————-

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

United Bank of Eastern Orem

Hours worked: 40.00

HourlyWage: 12.00

——————–FluffShuffle Electronics——————————-

Pay to the order of A. Dumbledore………………………………$807.12

United Bank of Eastern Orem

Salary: 1200.00

——————–FluffShuffle Electronics——————————-

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

United Bank of Eastern Orem

Hours worked: 40.00

HourlyWage: 10.00

——————–FluffShuffle Electronics——————————-

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

United Bank of Eastern Orem

Salary: 1000.00

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

Submitting Your Assignment

After you are satisfied that your program works correctly, submit it to Canvas as project #6. Create a zip file for this project and include the following:

The complete source code for your Employee classes (employee.h and employee.cpp)

The source code for your driver.

Your data file used by the program.

A screen shot of your execution results.

Grading Guidelines:

Assignment meets the submission guidelines.

Source code files contain a declaration that you did not copy any code.

Project meets style guidelines.

Code is properly documented

The Employee classes in your program provide all of the data and functionality described in these instructions. Inheritance is correctly used when defining the Hourly and Salaried classes. The classes are written so as to enable polymorphism.

Your program correctly uses polymorphism.

Your program successfully saves the employee data to a file, using polymorphism. The original employee objects are deleted and the vector is emptied (call the clear member function after you have delete the pointers in the vector).

Your program successfully reads the employee data back in from the file, using the read functions in your employee class hierarchy. New objects are created and pointers to the objects are stored in the vector of Employee pointers.

Your program successfully displays a paycheck for each employee in the proper format for its type.