Modify the payroll system in figures 13.13.-13.23 to include private data member
ID: 653764 • Letter: M
Question
Modify the payroll system in figures 13.13.-13.23 to include private data member birthDate in class Employee. Use class Date from Figs. 11.9-11.10 to represent an employees birthday. Assume that payroll is processed once per month. Create a vector of Employee references to store the various employee objects. In a loop, calculate the payroll for each Employee (polymorphically), and add $100.00 bonus to the person's payroll amount if the current month is the month in which the Employee's birthday occurs.
What I have now for 13.13-13.23:
===================================================================
BasePlusCommissionEmployee.h
===================================================================
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include "CommissionEmployee.h" // CommissionEmployee class definition
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee(const string &, const string &,
const string &, double = 0.0, double = 0.0, double = 0.0);
void setBaseSalary(double); // set base salary
double getBaseSalary() const; // return base salary
// keyword virtual signals intent to override
virtual double earnings() const; // calculate earnings
virtual void print() const; // print BasePlusCommissionEmployee object
private:
double baseSalary; // base salary per week
}; // end class BasePlusCommissionEmployee
#endif // BASEPLUS_H
===================================================================
BasePlusCommissionEmployee.cpp
===================================================================
#include "BasePlusCommissionEmployee.h"
#include<iostream>
using namespace std;
// constructor
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary)
: CommissionEmployee(first, last, ssn, sales, rate)
{
setBaseSalary(salary); // validate and store base salary
} // end BasePlusCommissionEmployee constructor
// set base salary
void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
baseSalary = ((salary < 0.0) ? 0.0 : salary);
} // end function setBaseSalary
// return base salary
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
} // end function getBaseSalary
// calculate earnings;
// override virtual function earnings in CommissionEmployee
double BasePlusCommissionEmployee::earnings() const
{
return getBaseSalary() + CommissionEmployee::earnings();
} // end function earnings
// print BasePlusCommissionEmployee's information
void BasePlusCommissionEmployee::print() const
{
cout << "base-salaried ";
CommissionEmployee::print(); // code reuse
cout << "; base salary: " << getBaseSalary();
} // end function print
===================================================================
CommissionEmployee.h
===================================================================
#ifndef COMMISSION_H
#define COMMISSION_H
#include "Employee.h" // Employee class definition
class CommissionEmployee : public Employee
{
public:
CommissionEmployee(const string &, const string &,
const string &, double = 0.0, double = 0.0);
void setCommissionRate(double); // set commission rate
double getCommissionRate() const; // return commission rate
void setGrossSales(double); // set gross sales amount
double getGrossSales() const; // return gross sales amount
// keyword virtual signals intent to override
virtual double earnings() const; // calculate earnings
virtual void print() const; // print CommissionEmployee object
private:
double grossSales; // gross weekly sales
double commissionRate; // commission percentage
}; // end class CommissionEmployee
#endif // COMMISSION_H
===================================================================
CommissionEmployee.cpp
===================================================================
#include "CommissionEmployee.h" // CommissionEmployee class definition
#include<iostream>
using namespace std;
// constructor
CommissionEmployee::CommissionEmployee(const string &first,
const string &last, const string &ssn, double sales, double rate)
: Employee(first, last, ssn)
{
setGrossSales(sales);
setCommissionRate(rate);
} // end CommissionEmployee constructor
// set commission rate
void CommissionEmployee::setCommissionRate(double rate)
{
commissionRate = ((rate > 0.0 && rate < 1.0) ? rate : 0.0);
} // end function setCommissionRate
// return commission rate
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
} // end function getCommissionRate
// set gross sales amount
void CommissionEmployee::setGrossSales(double sales)
{
grossSales = ((sales < 0.0) ? 0.0 : sales);
} // end function setGrossSales
// return gross sales amount
double CommissionEmployee::getGrossSales() const
{
return grossSales;
} // end function getGrossSales
// calculate earnings; override pure virtual function earnings in Employee
double CommissionEmployee::earnings() const
{
return getCommissionRate() * getGrossSales();
} // end function earnings
// print CommissionEmployee's information
void CommissionEmployee::print() const
{
cout << "commission employee: ";
Employee::print(); // code reuse
cout << " gross sales: " << getGrossSales()
<< "; commission rate: " << getCommissionRate();
} // end function print
===================================================================
Employee.h
===================================================================
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include<string> // C++ standard string class
using namespace std;
class Employee
{
public:
Employee(const string &, const string &, const string &);
void setFirstName(const string &); // set first name
string getFirstName() const; // return first name
void setLastName(const string &); // set last name
string getLastName() const; // return last name
void setSocialSecurityNumber(const string &); // set SSN
string getSocialSecurityNumber() const; // return SSN
// pure virtual function makes Employee abstract base class
virtual double earnings() const = 0; // pure virtual
virtual void print() const; // virtual
private:
string firstName;
string lastName;
string socialSecurityNumber;
}; // end class Employee
#endif // EMPLOYEE_H
===================================================================
Employee.cpp
===================================================================
#include "Employee.h" // Employee class definition
#include<iostream>
using namespace std;
// constructor
Employee::Employee(const string &first, const string &last,
const string &ssn)
: firstName(first), lastName(last), socialSecurityNumber(ssn)
{
// empty body
} // end Employee constructor
// set first name
void Employee::setFirstName(const string &first)
{
firstName = first;
} // end function setFirstName
// return first name
string Employee::getFirstName() const
{
return firstName;
} // end function getFirstName
// set last name
void Employee::setLastName(const string &last)
{
lastName = last;
} // end function setLastName
// return last name
string Employee::getLastName() const
{
return lastName;
} // end function getLastName
// set social security number
void Employee::setSocialSecurityNumber(const string &ssn)
{
socialSecurityNumber = ssn; // should validate
} // end function setSocialSecurityNumber
// return social security number
string Employee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
} // end function getSocialSecurityNumber
// print Employee's information (virtual, but not pure virtual)
void Employee::print() const
{
cout << getFirstName() << ' ' << getLastName()
<< " social security number: " << getSocialSecurityNumber();
} // end function print
===================================================================
HourlyEmployee.h
===================================================================
#ifndef HOURLY_H
#define HOURLY_H
#include "Employee.h" // Employee class definition
class HourlyEmployee : public Employee
{
public:
static const int hoursPerWeek = 168; // hours in one week
HourlyEmployee(const string &, const string &,
const string &, double = 0.0, double = 0.0);
void setWage(double); // set hourly wage
double getWage() const; // return hourly wage
void setHours(double); // set hours worked
double getHours() const; // return hours worked
// keyword virtual signals intent to override
virtual double earnings() const; // calculate earnings
virtual void print() const; // print HourlyEmployee object
private:
double wage; // wage per hour
double hours; // hours worked for week
}; // end class HourlyEmployee
#endif // HOURLY_H
===================================================================
HourlyEmployee.cpp
===================================================================
#include "HourlyEmployee.h" // HourlyEmployee class definition
#include<iostream>
using namespace std;
// constructor
HourlyEmployee::HourlyEmployee(const string &first, const string &last,
const string &ssn, double hourlyWage, double hoursWorked)
: Employee(first, last, ssn)
{
setWage(hourlyWage); // validate hourly wage
setHours(hoursWorked); // validate hours worked
} // end HourlyEmployee constructor
// set wage
void HourlyEmployee::setWage(double hourlyWage)
{
wage = (hourlyWage < 0.0 ? 0.0 : hourlyWage);
} // end function setWage
// return wage
double HourlyEmployee::getWage() const
{
return wage;
} // end function getWage
// set hours worked
void HourlyEmployee::setHours(double hoursWorked)
{
hours = (((hoursWorked >= 0.0) &&
(hoursWorked <= hoursPerWeek)) ? hoursWorked : 0.0);
} // end function setHours
// return hours worked
double HourlyEmployee::getHours() const
{
return hours;
} // end function getHours
// calculate earnings;
// override pure virtual function earnings in Employee
double HourlyEmployee::earnings() const
{
if (getHours() <= 40) // no overtime
return getWage() * getHours();
else
return 40 * getWage() + ((getHours() - 40) * getWage() * 1.5);
} // end function earnings
// print HourlyEmployee's information
void HourlyEmployee::print() const
{
cout << "hourly employee: ";
Employee::print(); // code reuse
cout << " hourly wage: " << getWage() <<
"; hours worked: " << getHours();
} // end function print
===================================================================
SalariedEmployee.h
===================================================================
#ifndef SALARIED_H
#define SALARIED_H
#include "Employee.h" // Employee class definition
class SalariedEmployee : public Employee
{
public:
SalariedEmployee( const string &, const string &,
const string &, double = 0.0 );
void setWeeklySalary( double ); // set weekly salary
double getWeeklySalary() const; // return weekly salary
// keyword virtual signals intent to override
virtual double earnings() const; // calculate earnings
virtual void print() const; // print SalariedEmployee object
private:
double weeklySalary; // salary per week
}; // end class SalariedEmployee
#endif
===================================================================
SalariedEmployee.cpp
===================================================================
#include "SalariedEmployee.h" // SalariedEmployee class definition
#include<iostream>
using namespace std;
// constructor
SalariedEmployee::SalariedEmployee(const string &first,
const string &last, const string &ssn, double salary)
: Employee(first, last, ssn)
{
setWeeklySalary(salary);
} // end SalariedEmployee constructor
// set salary
void SalariedEmployee::setWeeklySalary(double salary)
{
weeklySalary = (salary < 0.0) ? 0.0 : salary;
} // end function setWeeklySalary
// return salary
double SalariedEmployee::getWeeklySalary() const
{
return weeklySalary;
} // end function getWeeklySalary
// calculate earnings;
// override pure virtual function earnings in Employee
double SalariedEmployee::earnings() const
{
return getWeeklySalary();
} // end function earnings
// print SalariedEmployee's information
void SalariedEmployee::print() const
{
cout << "salaried employee: ";
Employee::print(); // reuse abstract base-class print function
cout << " weekly salary: " << getWeeklySalary();
} // end function print
===================================================================
main.cpp
===================================================================
#include<iostream>
using std::cout;
using std::endl;
using std::fixed;
#include<iomanip>
using std::setprecision;
#include<vector>
using std::vector;
#include<time.h>
using std::time_t;
using std::time;
using std::localtime;
using std::strftime;
#include<cstdlib>
using std::atoi;
#include "Employee.h"
#include "SalariedEmployee.h"
#include "HourlyEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
int determineMonth();
int main()
{
cout << fixed << setprecision(2);
vector<Employee*> employees(4);
employees[0] = new SalariedEmployee
("John", "Smith", "111 - 11 - 1111",800);
employees[1] = new HourlyEmployee
("Karen", "Price", "222-22-2222", 16.75, 40);
employees[2] = new CommissionEmployee
("Sue", "Jones", "333-33-3333",10000, .06);
employees[3] = new BasePlusCommissionEmployee
("Bob", "Lewis", "444-44-4444",5000, .04, 300);
int month = determineMonth();
cout << "Employees processed polymorphcally via dynamic binding : ";
for (size_t i = 0; i < employees.size(); i++)
{
employees[i]->print();
cout << endl;
BasePlusCommissionEmployee* derivedPtr = dynamic_cast<BasePlusCommissionEmployee*>(employees[i]);
if (derivedPtr != 0)
{
double oldBaseSalary =
derivedPtr->getBaseSalary();
cout << "old base salary: $" << oldBaseSalary << endl;
derivedPtr->setBaseSalary(1.10 * oldBaseSalary);
cout << "new base salary with 10% increase is: $" << derivedPtr->getBaseSalary() << endl;
}
cout << endl;
}
for (size_t j = 0; j < employees.size(); j++)
{
cout << "deleting object of " << typeid(*employees[j]).name() << endl
delete employees[j];
}
system("PAUSE");
return 0;
}
int determineMonth()
{
time_t currentTime;
char monthString[3];
time(¤tTime);
strftime(monthString, 3, "%m", localtime(¤tTime));
return atoi(monthString);
}
=============================================================================
What I have for 11.9-11.10 for class Date:
====================
Date.cpp:
===================
#include "stdafx.h"
#include <iostream>
#include "Date.h"
const int Date::days[] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Date::Date(int m, int d, int y)
{
setDate(m, d, y);
}
void Date::setDate(int mm, int dd, int yy)
{
month = (mm >= 1 && mm <= 12) ? mm : 1;
year = (yy >= 1900 && yy <= 2100) ? yy : 1900;
if (month == 2 && leapYear(year))
day = (dd >= 1 && dd <= 29) ? dd : 1;
else
day = (dd >= 1 && dd <= days[month]) ? dd : 1;
}
Date &Date::operator++()
{
helpIncrement();
return *this;
}
Date Date::operator++(int)
{
Date temp = *this;
helpIncrement();
return temp;
}
const Date &Date::operator+=(int additionalDays)
{
for (int i = 0; i < additionalDays; i++)
helpIncrement();
return *this;
}
bool Date::leapYear(int testYear) const
{
if (testYear % 400 == 0 ||
(testYear % 100 != 0 && testYear % 4 == 0))
return true;
else
return false;
}
int Date::getMonth() const
{
return month;
}
bool Date::endOfMonth(int testDay) const
{
if (month == 2 && leapYear(year))
return testDay == 29;
else
return testDay == days[month];
}
void Date::helpIncrement()
{
if (!endOfMonth(day))
day++;
else
if (month < 12)
{
month++;
day = 1;
}
else
{
year++;
month = 1;
day = 1;
}
}
ostream &operator<<(ostream &output, const Date &d)
{
static char *monthName[13] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
output << monthName[d.month] << ' ' << d.day << ", " << d.year;
return output;
}
==============================================================================
Date.h:
==============================================================================
#include "stdafx.h"
#ifndef DATE_H
#define DATE_H
#include <iostream>
using namespace std;
class Date
{
friend ostream &operator<<(ostream &, const Date &);
public:
Date(int m = 1, int d = 1, int y = 1900);
void setDate(int, int, int);
Date &operator++();
Date operator++(int);
const Date &operator+=(int);
bool leapYear(int) const;
bool endOfMonth(int) const;
int getMonth() const;
private:
int month;
int day;
int year;
static const int days[];
void helpIncrement();
};
#endif
====================================================================================
I'm using Visual Studio 2010.
Explanation / Answer
New code lines are marked in bold
Employee.h
===================================================================
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include<string> // C++ standard string class
#include <date.cpp> //include date.cpp for declaring employee birthDate
using namespace std;
class Employee
{
public:
Employee(const string &, const string &, const string &);
void setFirstName(const string &); // set first name
string getFirstName() const; // return first name
void setLastName(const string &); // set last name
string getLastName() const; // return last name
void setSocialSecurityNumber(const string &); // set SSN
string getSocialSecurityNumber() const; // return SSN
Date getBirthDate() const; //return birthdate
void setBirthDate(Date&); //set birthdate
// pure virtual function makes Employee abstract base class
virtual double earnings() const = 0; // pure virtual
virtual void print() const; // virtual
private:
string firstName;
string lastName;
string socialSecurityNumber;
Date birthDate; //declare Date type object called birthDate
}; // end class Employee
#endif // EMPLOYEE_H
===================================================================
Employee.cpp
===================================================================
#include "Employee.h" // Employee class definition
#include<iostream>
using namespace std;
// constructor
Employee::Employee(const string &first, const string &last,
const string &ssn)
: firstName(first), lastName(last), socialSecurityNumber(ssn)
{
// empty body
} // end Employee constructor
// set first name
void Employee::setFirstName(const string &first)
{
firstName = first;
} // end function setFirstName
// return first name
string Employee::getFirstName() const
{
return firstName;
} // end function getFirstName
// set last name
void Employee::setLastName(const string &last)
{
lastName = last;
} // end function setLastName
// return last name
string Employee::getLastName() const
{
return lastName;
} // end function getLastName
// set social security number
void Employee::setSocialSecurityNumber(const string &ssn)
{
socialSecurityNumber = ssn; // should validate
} // end function setSocialSecurityNumber
// return social security number
string Employee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
} // end function getSocialSecurityNumber
void Employee::setBirthDate(const Date &bDate)
{
birthDate= bDate; //set birthdate of employee
}
Date Employee::getBirthDate(){
return birthDate; //return employee birthdate
}
// print Employee's information (virtual, but not pure virtual)
void Employee::print() const
{
cout << getFirstName() << ' ' << getLastName()
<< " social security number: " << getSocialSecurityNumber();
} // end function print
=======================================================================
main.cpp
===================================================================
#include<iostream>
using std::cout;
using std::endl;
using std::fixed;
#include<iomanip>
using std::setprecision;
#include<vector>
using std::vector;
#include<time.h>
using std::time_t;
using std::time;
using std::localtime;
using std::strftime;
#include<cstdlib>
using std::atoi;
#include "Employee.h"
#include "SalariedEmployee.h"
#include "HourlyEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
int determineMonth();
int main()
{
cout << fixed << setprecision(2);
vector<Employee*> employees(4);
employees[0] = new SalariedEmployee
("John", "Smith", "111 - 11 - 1111",800);
employees[1] = new HourlyEmployee
("Karen", "Price", "222-22-2222", 16.75, 40);
employees[2] = new CommissionEmployee
("Sue", "Jones", "333-33-3333",10000, .06);
employees[3] = new BasePlusCommissionEmployee
("Bob", "Lewis", "444-44-4444",5000, .04, 300);
int month = determineMonth();
cout << "Employees processed polymorphcally via dynamic binding : ";
for (size_t i = 0; i < employees.size(); i++)
{
employees[i]->print();
cout << endl;
BasePlusCommissionEmployee* derivedPtr = dynamic_cast<BasePlusCommissionEmployee*>(employees[i]);
if (derivedPtr != 0)
{
double oldBaseSalary =
derivedPtr->getBaseSalary();
cout << "old base salary: $" << oldBaseSalary << endl;
if(derivedPtr->getMonth==month){ //if present month is birthday month of employee
derivedPtr->setBaseSalary((1.10*oldBaseSalary)+100);
cout << "new base salary due to bithday month: $" << derivedPtr->getBaseSalary() << endl;
}else{
derivedPtr->setBaseSalary(1.10 * oldBaseSalary);
cout << "new base salary with 10% increase is: $" << derivedPtr->getBaseSalary() << endl;
}
}
cout << endl;
}
for (size_t j = 0; j < employees.size(); j++)
{
cout << "deleting object of " << typeid(*employees[j]).name() << endl
delete employees[j];
}
system("PAUSE");
return 0;
}
int determineMonth()
{
time_t currentTime;
char monthString[3];
time(¤tTime);
strftime(monthString, 3, "%m", localtime(¤tTime));
return atoi(monthString);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.