Lab 7 - Inheritance and Polymorphism

The purpose of this lab is to learn about polymorphism and inheritance by expanding the Employee class example used during lecture.

The Employee class files are:

Lab assignment overview:

For the copy constructor and assignment operator, use the provided copy constructor and assignment operator in hourlyEmployee as a template for what needs to be done in salariedEmployee. Make sure to invoke the parent copy constructor or the parent assignment operator, then copy the child-specific features.

To turn inputEmployee() and printEmployee() into polymorphic functions, add the virtual keyword to the Employee class.

For the destructor, add a destructor to Employee (the parent class) that just prints out to the screen "Employee destructor called". Likewise, add destructors to hourlyEmployee and salariedEmployee that print a similar message to the screen, but using the class name instead of Employee (e.g. "hourlyEmployee destructor called"). Make sure that the destructor in Employee is marked as polymorphic with the virtual keyword.

For your main() function, make a new lab7main.cpp file that includes the employee.h file. Use separate compilation to compile your program (refer to Lab 4 for how to do separate compilation). The main function will be a modified menu program where the user will type a command to get a menu option. It will prompt the user to type "employee", "hourly", or "salaried" to select the type of employee they wish to allocate. It will use the following pseudocode:

create an Employee pointer
create a character array called cmd with at least 129 characters
while true
  ask the user if they wish to enter an 'employee', a 'hourly' employee or 'salaried' employee, tell them to enter "exit" to end the loop
  use cin.getline to read their response into cmd

  if strncmp(cmd, "hourly", 6) is 0
    try
      use new to allocate a new hourlyEmployee to the Employee pointer
    catch bad_alloc
      output that the allocation failed
      exit program
  else if strncmp(cmd, "salaried", 8) is 0
    try
      use new to allocate a new salariedEmployee to the Employee pointer
    catch bad_alloc
      output that the allocation failed
      exit program
  else if strncmp(cmd, "employee", 8) is 0
    try
      use new to allocate a new Employee to the Employee pointer
    catch bad_alloc
      output that the allocation failed
      exit program
  else if strncmp(cmd, "exit", 4) is 0 OR strncmp(cmd, "quit", 4) is 0
    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 printEmployee() on the Employee pointer
  delete the object pointed to by the Employee pointer
  set the Employee pointer to NULL
end-while

Email me your updated employee.h and employee.cpp files along with your new lab7main.cpp file.