OO Programming and Data Structures | CS 241

07 Teach : Team Activity - Polymorphism

This activity is designed to be completed with your team.

Overview

BYU-Idaho is having issues with its payroll system and has asked you to create a new one. You quickly recognize that there are two types of employees: hourly employees (those that get paid a certain amount per hour); and salary employees (those that receive an annual salary).

While these two types of employees have differences, they also have a significant amount in common. So you decide to create a common base class with each of these two types as a separate derived class.

Instructions

For this assignment, you will start with a blank file. To keep our focus directly on the abstract methods we are working with, you may put all of our classes in the same file rather than separate files.

Core Requirements

  1. Create a base class Employee, and two derived classes HourlyEmployee and SalaryEmployee, as follows:

    • Employee should have a string name and a display function that displays the name.

    • HourlyEmployee should have an int hourly_wage and should override the display function to display the name and wage in the format: "John - $8/hour".

    • SalaryEmployee should have an int salary and should override the display function to display the name and salary in the format: "John - $50000/year".

    • You may also find it beneficial to add parameters to your __init__ function, but that is up to you.

  2. Create a main function that does the following:

    • Declares a list for employees.

    • Loops until the user enters "q" and prompts the user for an "h" (hourly employee) or an "s" (salary employee) or a "q" to quit.

    • Then prompts for the name and the hourlyRate/salary.

    • For each employee entered, creates a new employee of the correct type and adds it to the list.

  3. After the user enters "q", have main loop through the list and call the display method for each employee. Run your program and ensure that it looks correct.

Stretch Challenges

After completing the above steps, make sure that everyone on the entire team is to this point and understands the material. Then, if you have time, move onto the following stretch challenges.

  1. Convert your Employee class to an abstract base class and the display function to be an abstract method.

  2. Add to your HourlyEmployee a member variable for hours. Then, add an abstract method to the employee class for get_paycheck(), and override it in the derived classes. For hourly employees, this is calculated as the number of hours multiplied by the hourly rate. For salary employees, this should be the salary divided by 24.

    Change your code that prompts for the hourly employee to also prompt for the number of hours.

    In the loop in main that calls the display function, change it to also call the get_paycheck() function and display the pay check amount.

  3. Add a regular function (not a member function of any class), display_employee_data, that accepts an employee and calls its display function as well as its get_paycheck() function and displays the value. Then, remove this code from main, and replace it with a call to your new function.

Instructor's Solution

As a part of this team activity, you are expected to look over a solution from the instructor, to compare your approach to that one. One of the questions on the I-Learn submission will ask you to provide insights from this comparison.

Please DO NOT open the solution until you have worked through this activity as a team for the one hour period. At the end of the hour, if you are still struggling with some of the core requirements, you are welcome to view the instructor's solution and use it to help you complete your own code. Even if you use the instructor's code to help you, you are welcome to report that you finished the core requirements, if you code them up yourself.

After working with your team for the one hour activity, click here for the instructor's solution.

Submission

You do not need to submit your program (just make sure that everyone has a copy of it for their reference). Instead, answer the accompanying questions in the I-Learn quiz.