3460:430/530:080 Theory of Programming Languages Summer
2004
Dr. C.-C. Chan Due:
Project #4 (30 pts)
Problem:
Write a menu-driven C++ program that allow
a user to create a list of employees of different categories interactively
based on the employee class hierarchy provided in the appendix.
Menu Items:
1. Create a list.
2. Add an employee to the
list.
2.1 Boss:
// prompt for weekly salary
2.2 Commision
Worker:// prompt for base salary, commission, and total items sold
2.3 Hourly Worker: // prompt for wage per hour and hours worked
for week
3. Find an employee by Last
Name: // prompt for last name, if found
print name and category of the employee, else print error messages.
4. Print all employees in
the list ordered by Last Name using the following format:
Last Name First Name Category Earnings
5. Quit.
Implementation:
You must use the header
files provided in the appendix.
Bonus: (6 pts)
Submissions:
1. Name your header files as those provided in
the appendix.
2. Send the source file by e-mail to chan@cs.uakron.edu
3. The project is due by midnight of the due
day.
APPENDIX
// EMPLOY2.H
// Abstract base class Employee
#ifndef EMPLOY2_H
#define EMPLOY2_H
class Employee {
public:
Employee(const char *, const char *);
~Employee();
const char * getFirstName() const;
const char * getLastName() const;
// Pure virtual functions make Employee abstract base class.
virtual float earnings() const = 0; // pure virtual
virtual void print() const = 0; // pure virtual
private:
char * firstName;
char * lastName;
};
#endif
// BOSS1.H
// Boss class derived from Employee
#ifndef BOSS1_H
#define BOSS1_H
#include "employ2.h"
class Boss : public Employee {
public:
Boss(const char *, const char *, float = 0.0);
void setWeeklySalary(float);
virtual float earnings() const;
virtual void print() const;
private:
float weeklySalary;
};
#endif
// COMMIS1.H
// CommissionWorker class derived from Employee
#ifndef COMMIS1_H
#define COMMIS1_H
#include "employ2.h"
class CommissionWorker : public Employee {
public:
CommissionWorker(const char *, const char *,
float = 0.0, float = 0.0, unsigned = 0);
void setSalary(float);
void setCommission(float);
void setQuantity(unsigned);
virtual float earnings() const;
virtual void print() const;
private:
float salary; // base salary per week
float commission; // amount per item sold
unsigned quantity; // total items sold for week
};
#endif
// HOURLY1.H
// Definition of class HourlyWorker
#ifndef HOURLY1_H
#define HOURLY1_H
#include "employ2.h"
class HourlyWorker : public Employee {
public:
HourlyWorker(const char *, const char *,
float = 0.0, float = 0.0);
void setWage(float);
void setHours(float);
virtual float earnings() const;
virtual void print() const;
private:
float wage; // wage per hour
float hours; // hours worked for week
};
#endif
// EMPLOY2.CPP
// Member function definitions for
// abstract base class Employee.
//
// Note: No definitions given for pure virtual functions.
#include <iostream.h>
#include <string.h>
#include <assert.h>
#include "employ2.h"
// Constructor dynamically allocates space for the
// first and last name and uses strcpy to copy
// the first and last names into the object.
Employee::Employee(const char *first, const char *last)
{
firstName = new char [ strlen(first) + 1 ];
assert(firstName != 0); // test that new worked
strcpy(firstName, first);
lastName = new char[ strlen(last) + 1 ];
assert(lastName != 0); // test that new worked
strcpy(lastName, last);
}
// Destructor deallocates dynamically allocated memory
Employee::~Employee()
{
delete [] firstName;
delete [] lastName;
}
// Return a pointer to the first name
const char *Employee::getFirstName() const
{
// Const prevents caller from modifying private data.
// Caller should copy returned string before destructor
// deletes dynamic storage to prevent undefined pointer.
return firstName; // caller must delete memory
}
// Return a pointer to the last name
const char *Employee::getLastName() const
{
// Const prevents caller from modifying private data.
// Caller should copy returned string before destructor
// deletes dynamic storage to prevent undefined pointer.
return lastName; // caller must delete memory
}