Object Orientated Payroll Program
Solution
p1.cpp
#include
#include
using namespace std;
// define the Employee classes
class Employee
{
private:
string name;
int number; // employee number
string hire; // the hire date
public:
// constructors
Employee(string name, int number, string hire)
: name(name), number(number), hire(hire)
{}
Employee() : name(“”), number(0), hire(“”) {}
// accessor and mutator functions
stringgetName() const { return name; }
intgetNumber() const { return number; }
stringgetHireDate() const { return hire; }
voidsetName(const string &value) { name = value; }
voidsetNumber(constint value) { number = value; }
voidsetHireDate(const string &value) { hire = value; }
};
// inherent class defination
classProductionWorker : public Employee
{
private:
int shift;
doublehourlyRate; // hourly pay rate
public:
// constructors
ProductionWorker(string name, int number, string hire, int shift, double hourlyRate)
: Employee(name, number, hire), shift(shift), hourlyRate(hourlyRate)
{}
ProductionWorker() : Employee(), shift(1), hourlyRate(0.0) {}
// accessor and mutator functions
intgetShift() const { return shift; }
doublegetHourlyRate() const { return hourlyRate; }
voidsetShift(int value) { shift = value; }
voidsetHourlyRate(double value) { hourlyRate = value; }
};
// define ShiftSuppervisor class
classShiftSupervisor : public Employee
{
private:
doubleannualSalary;
doubleannualBonus; // annual production bonus
public:
// constructors
ShiftSupervisor(string name, int number, string hire, double annualSalary, double annualBonus)
: Employee(name, number, hire), annualSalary(annualSalary), annualBonus(annualBonus)
{}
ShiftSupervisor() : Employee(), annualSalary(0.0), annualBonus(0.0) {}
// accessor and mutator functions
doublegetAnnualSalary() const { return annualSalary; }
doublegetAnnualBonus() const { return annualBonus; }
voidsetAnnualSalary(double value) { annualSalary = value; }
voidsetAnnualBonus(double value) { annualBonus = value; }
};
// TeamLeader class
classTeamLeader : public ProductionWorker
{
private:
doublemonthlyBonus; // fixed monthly bonus
intrequiredHours; // minimal required training hours
inttrainedHours; // number of hours the team leader has attended
public:
TeamLeader(string name, int number, string hire, int shift, double hourlyRate,
doublemonthlyBonus, intrequiredHours, inttrainedHours)
: ProductionWorker(name, number, hire, shift, hourlyRate),
monthlyBonus(monthlyBonus), requiredHours(requiredHours), trainedHours(trainedHours)
{}
TeamLeader() : ProductionWorker(), monthlyBonus(0), requiredHours(0), trainedHours(0) {}
// accessor and mutator functions
doublegetMonthlyBonus() const { return monthlyBonus; }
intgetRequiredHours() const { return requiredHours; }
intgetTrainedHours() const { return trainedHours; }
voidsetMonthlyBonus(double value) { monthlyBonus = value; }
voidsetRequiredHours(int value) { requiredHours = value; }
voidsetTrainedHours(int value) { trainedHours = value; }
};
int main() {
// Q1 use the ProductionWorker class
ProductionWorkerp(“Ben J”, 1, “2010-03-12”, 2, 100);
cout<< “Production Worker: ” <
<< “Hired Date “<
<< (p.getShift() == 1 ? “Day Shift Hourly Rate @” : “Night Shift Hourly Rate @”)
<
// Q2 use ShiftSuppervisor class
ShiftSupervisors(“Steven K”, 2, “2008-01-01”, 60000, 20000);
cout<< “Shift Suppervisor: ” <
<< “Hired Date “<
<< “Annual Salary @” <
<< “Annual Production Bonus @” <
// Q3 use TeamLeader
TeamLeadert(“Bryan M”, 3, “2009-08-11”, 1, 120, 2000, 10, 3);
cout<< “Team Leader: ” <
<< “Hired Date “<
<< (t.getShift() == 1 ? “Day Shift Hourly Rate @” : “Night Shift Hourly Rate @”)
<
<< “Monthly Bonus @” <
<< “Required Minimum Training “<
<< “Trained “<
<
}
p2.cpp
#include
#include
using namespace std;
// define the Employee classes
class Employee
{
private:
string name;
int number; // employee number
string hire; // the hire date
public:
classInvalidEmployeeNumber {};
// constructors
Employee(string name, int number, string hire)
: name(name), number(number), hire(hire)
{
if (number < 0 || number > 9999) {
throwInvalidEmployeeNumber();
}
}
Employee() : name(“”), number(0), hire(“”) {}
// accessor and mutator functions
stringgetName() const { return name;
intgetNumber() const { return number; }
stringgetHireDate() const { return hire; }
voidsetName(const string &value) { name = value; }
voidsetNumber(constint value) {
if (value < 0 || value > 9999) {
throwInvalidEmployeeNumber();
}
number = value;
}
voidsetHireDate(const string &value) { hire = value; }
};
// inherent class defination
classProductionWorker : public Employee
{
private:
int shift;
doublehourlyRate; // hourly pay rate
public:
classInvalidShift {};
classInvalidPayRate {};
// constructors
ProductionWorker(string name, int number, string hire, int shift, double hourlyRate)
: Employee(name, number, hire), shift(shift), hourlyRate(hourlyRate)
{
if (shift != 1 && shift != 2) {
throwInvalidShift();
}
if (hourlyRate< 0) {
throwInvalidPayRate();
}
}
ProductionWorker() : Employee(), shift(1), hourlyRate(0.0) {}
// accessor and mutator functions
intgetShift() const { return shift; }
doublegetHourlyRate() const { return hourlyRate; }
voidsetShift(int value) {
if (value != 1 && value != 2) {
throwInvalidShift();
}
shift = value;
}
voidsetHourlyRate(double value) {
if (value < 0) {
throwInvalidPayRate();
}
hourlyRate = value;
}
};
int main() {
// demonstrate the use of exceptions
try {
ProductionWorkerp1(“Ben J”, -1, “2010-03-12”, 2, 100);
} catch (Employee::InvalidEmployeeNumber&e) {
cout<< “Employee::InvalidEmployeeNumber is caught!” <
}
try {
ProductionWorkerp1(“Ben J”, 9999, “2010-03-12”, 0, 100);
} catch (ProductionWorker::InvalidShift&e) {
cout<< “Employee::InvalidShift is caught!” <
}
try {
ProductionWorkerp1(“Ben J”, 9999, “2010-03-12”, 1, -10);
} catch (ProductionWorker::InvalidPayRate&e) {
cout<< “Employee::InvalidPayRate is caught!” <
}
return 0;
}