Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

12.8 Programming Project #7: An Employee Class Hierarchy Background The code that you wrote for the second Employee project only works for hourly employees. Now

12.8 Programming Project #7: An Employee Class Hierarchy 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. <> Employee empNum: int name: String addreSs : string phone: string # Employee( )-default # readData( : ifstream) : void # ) Employee(num:int, name:string, addr:string, phone:string + virtual Employee() - default + getEmpNum(): int + getName() : string + getAddress() : string + getPhone(): string + setName(string) : void + setAddress(string): void + setPhone(string) : void + write(: ofstream) : void + calcPaiy + printCheck(): void : double HourlyEmployee SalariedEmployee hoursWorked: double salary: double hourlyWage: double # HourlyEmployee( ) = default #readData( : ifstream) : void override + HourlyEmployee(...) + getHoursWorked(): double + getHourlyWage() : double + setHoursWorked(double): void + setHourlyWage(double): void write(: ofstream) : void override + calcPay) : double ov + printCheck(): void override + read(: ifstream) : HourlyEmployee* # SalariedEm ployee( ) default #readData( : ifstream) : void override + SalariedEmployee(...) + getSalary() : double + setSalary(double): void + write(: ofstream ) : void override + calcPay() : double override + printCheck( ) : void override + read(: ifstream) : SalariedEmployee* erride (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 frorm 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 https://repl.it/@Graciasc/prject-128

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

=+2. Identify and analyze your audience.

Answered: 1 week ago