Homework 5 - Polymorphism and Exception Handling

Due: Wednesday, March 2, 2011 at 5:00pm

Coding Conventions

Use the same coding conventions as described in Homework 1. Additionally, make sure to indent each section of the class in the class definition.

Assignment

The purpose of this assignment is to modify Lab 7 to support polymorphism and exception handling.

Polymorphism

Polymorphism enables runtime binding to be used to select function bodies when using a parent class pointer. Change inputEmployee() and printPaycheck() to be polymorphic functions in all three classes.

Also add a new polymorphic function to all three classes (Employee and the two children classes). Call this function employeeType(). In Employee, make this function a pure virtual function. In hourlyEmployee, have this function print out "Hourly". In salariedEmployee, have this function print out "Salaried". Call this function within printPaycheck() to print out the employee type.

Exception Handling

Rather than returning a Boolean on the mutator functions (e.g. setSSN, setSalary and so on), you will use exception handling. Create the following exception classes: badSSN, badWeek, badRate, badHour and badSalary. Within each mutator function, if the input is bad, you will throw the corresponding exception class instead of returning false. For example, if the SSN is not exactly 9 digits, you will throw the badSSN exception class.

In each inputEmployee() function, you will have a try-catch block for each of the mutator functions. You will keep asking the user for input while there is an exception thrown. The pseudocode for doing this for the SSN is as follows:

do
  ask the user for the SSN
  read the SSN into a temporary character array
  set flag to true
  try 
    call setSSN
  catch badSSN
    output that the SSN is bad and must be exactly 9 digits
    set flag to false
while flag is false
Similar logic will be used for the other mutator functions.

NOTE: You do not need to do exception handling for setName() as it just truncates names that are too long.

Main Function

Replace the main() from Lab 7 with a main() that implements the following pseudocode to test the polymorphic functions:
create an Employee pointer
while true
  ask the user if they wish to enter a hourly or salaried employee, tell them to enter "exit" to end the loop
  read their response into a character array

  if the user typed "hourly"
    try
      use new to allocate a new hourlyEmployee to the Employee pointer
    catch bad_alloc
      output that the allocation failed
      exit program
  else if the user typed "salaried"
    try
      use new to allocate a new salariedEmployee to the Employee pointer
    catch bad_alloc
      output that the allocation failed
      exit program
  else if the user typed "exit"
    exit the loop with the break statement
  else
    output that the user selected an invalid employee type
    continue to the next iteration of the loop

  call inputEmployee() on the Employee pointer
  call printPaycheck() on the Employee pointer
  delete the object pointed to by the Employee pointer
end-while
This is a variation on a menu loop where the user types commands instead of menu options. You should use strcmp() from the cstring library to compare what they typed to the available commands.

Email me your updated employee.h, employee.cpp and main.cpp files.