Programming Inheritance and Polymorphism: A Practice
I.
Overview:
You need to develop a monthly payroll program for a company. The
programming task in the following requires the use of inheritance and
polymorphism in the monthly payroll program. The figures-and-polymorphism sample project you
studied earlier can serve as the basic framework of the payroll program. You may
reuse and adapt the code in the figures-and-polymorphism
sample project to help you set a basic framework, but you need to write
everything on your own to make it work.
II.
Context of the payroll program:
Company X has three categories of employees and pays the employees in
different ways.
(i)
Full time staff:
an employee in this category receives a
fixed monthly pay each month according to the contract,
(ii)
Hourly workers:
an employee in this category is paid by
hour according to the work hours
he/she got this month and the hourly
rate stated in the contract, and
(iii)
Sales persons:
an employee in this category receives a
fixed monthly base salary according to the contract and in addition for each new customer he/she brings to the company he/she receives
an additional $1000 commission.
III.
Programming Task:
Company X wants a payroll program that creates a two-level class
hierarchy and uses polymorphism to handle monthly payroll processing in the
following way:
- Create an Employee class that has a string data member id for recording the employee
id, an int data member monthlyPay
for storing the employee’s pay this month, and three virtual methods getBasicInfo(
), calculateMonthlyPay( ), printMonthlyPay( ) to get the basic
information of the employee for this month, to calculate the current
monthly pay, and to print the current monthly pay + the basic information
of the employee respectively.
- Derive from the Employee class a FullTimer
class that has an additional data member fixedSalary for
recording the employee’s fixed monthly salary and implement accordingly
the methods getBasicInfo( ), calculateMonthlyPay( ), printMonthlyPay( )
to get the basic information of the employee for this month, to calculate
the current monthly pay, and to print the current monthly pay + the basic
information of the employee respectively.
- Derive from the Employee class a HourlyWorker
class that has two additional data members workHours for
recording the employee’s work hours this month and hourlyRate for
recording the employee’s hourly rate this month and implement accordingly
the methods getBasicInfo( ), calculateMonthlyPay( ), printMonthlyPay( )
to get the basic information of the employee for this month, to calculate
the current monthly pay, and to print the current monthly pay + the basic
information of the employee respectively.
- Derive from the Employee class a SalesPerson
class that has two additional data members baseSalary for
recording the employee’s fixed monthly base salary and numberOfNewCustomers
for recording the number of new customers the salesperson got this month
and implement accordingly the methods getBasicInfo( ), calculateMonthlyPay(
), printMonthlyPay( ) to get the basic information of the employee
for this month, to calculate the current monthly pay, and to print the
current monthly pay + the basic information of the employee respectively.
- Write a main function that (1)
declares a vector of pointers to Employee
for storing pointers to dynamically created Employee objects and (2) provides a menu of three services, i.e.
adding a new employee, calculate the current monthly pay for
every employee, and display the
current monthly pay + the basic information of the employee for every
employee as described in vi, vii, and viii below.
- Add a new employee: Continue from
v above. If the user chooses to add a new employee record, the main
function should (1) ask the user what type of employee the person belongs
to, and then (2) dynamically create a new object of the corresponding
child class derived from the Employee
class and call the object to run its getBasicInfo( ) member function to get the specific basic
information of the new employee from the user. Store the pointer to the
new object in the vector of pointers to Employee objects.
- Calculate the current monthly pay:
Continue from v above. If the user chooses to calculate the current
monthly pay for every employee, the main function should iterate through
the vector of pointers to Employee objects
and call each object to run its own
calculateMonthlyPay( ) member function to calculate the current
monthly pay and store it in the monthlyPay
data member.
- Display the current monthly pay +
basic information for every
employee: Continue from v above. If the user chooses to print the
current monthly pay + the basic information of the employee for every
employee, the main function should iterate through the vector of pointers
to Employee objects and call
each object to run its
printMonthlyPay( ) member function to print the current monthly pay +
the basic information of the employee.
IV.
Testing your implementation:
Run and test
your program:
·
add several employees, including all three
categories (full-timer, hourly workers, and salespersons) in the mix,
·
ask the program to calculate the current monthly
pay, and
·
ask the program to display the current monthly
pay + basic information to see whether it can do the job correctly for
employees in all three categories correctly.