C++ Programming Project: An Employee Class Hierarchy I need help with my code. M
ID: 3918963 • Letter: C
Question
C++ Programming Project: An Employee Class Hierarchy
I need help with my code. My program does not compile; it returns with many errors. The following contains my code along with the project instructions.
--------------------------------------
Background
The code that you wrote for the second Employee project only works for hourly employees. Now that we have studied polymorphism, we can begin to make our payroll program more general. The most important thing we will do is enable polymorphism in our Employee class hierarchy, so that employee objects of different types automatically behave correctly.
Objectives
Continue gaining experience with object-oriented design
Learn to create a class hierarchy that is enabled for polymorphism
Learn to use polymorphism in a program.
The Program
We will rename Employee to HourlyEmployee, and then create two new classes, Employee and SalariedEmployee, as depicted in the class diagram below.
(Items preceded by dashes are private, those preceded by a # are protected, and public members start with a +. Static members appear underlined.)
Employee is an abstract class, meaning that it exists to hold what is common to all its derived classes, and is not to be instantiated directly by users. For this reason, it has no public constructors.
In addition to defining all things common to the derived employee classes, Employee defines a virtual destructor and declares four pure virtual functions, which are overridden in the derived classes (see the italicized functions in Employee above). All of these functions have bodies in Employee except calcPay, and are called explicitly from their derived counterparts. Note that readData functions as well as default constructors have protected access. This is because we don't want users to create instances of the concrete, derived employee objects directly without complete information.
Also, remember that base classes must always have a virtual destructor, even if the body of that destructor is empty (which is what = default accomplishes here). Finally, the parameterized constructors for the derived classes take all pertinent parameters for objects of their type (which includes data common to all employees).
The key functions work as follows:
read
This static member function creates an empty, derived employee object and then calls readData to initialize it. It returns a pointer to a new object, if data was read correctly, or nullptr if there was an input error. For example, HourlyEmployee::read creates a new, empty HourlyEmployee object on the heap, emp, say. and then calls emp->readData. HourlyEmployee::readData calls Employee::readData to read in the common employee fields from a file, and then reads in the hourly wage and hours worked. The pointer to the initialized object (or nullptr) is then returned.
readData
As explained above, this function first calls the base class implementation of readData, and then reads data from the file for its type of object (HourlyEmployee or SalariedEmployee).
write
This function works similarly to readData: it first calls Employee::write to write out the common data to the file, then writes out the specific derived class data.
printCheck
This function first calls Employee::printCheck to print the common check header, then prints the rest of the check according to the type of the object. See the sample output below to see how SalariedEmployee checks should look.
calcPay
Hourly Employees: An hourly employee's gross pay is calculated by multiplying the hours worked by their hourly wage. Be sure to give time-and-a-half for overtime (anything over 40 hours). To compute the net pay, deduct 20% of the gross for Federal income tax, and 7.5% of the gross for state income tax.
Salaried Employees: A salaried employee's gross pay is just their weekly salary value. To compute the net pay for a salaried employee, deduct 20% of the gross for Federal income tax, 7.5% of the gross for state income tax, and 5.24% for benefits.
The main() Function
Your driver will provide the same set of options as in the previous project. However, your driver code should be somewhat simpler because you will now create a vector of Employee pointers, and store the pointers to your objects in this vector. Add heap pointers to the following employees to your vector:
You can then use a simple for-loop to save each object's data to the file.
Here is a sample execution of Option 1:
The contents of the file employee.txt should be:
When you run option 2, your program should create a new vector of Employee pointers. Make four calls to either HourlyEmployee::read or SalariedEmployee::read, according to the same order that you wrote the objects in part 1. Add the pointers returned by read to your vector. Then loop through the vector printing out the checks. Here is an execution of Option 2:
From the end user's point of view, your program will work just as it did before.
--------------------------------------
//main.cpp
#include "employee.h"
#include
using namespace std;
void printCheck1(const Employee&);
void printCheck2(const Employee&);
int main() {
const int option1 = 1;
const int option2 = 2;
int userInput;
string fileName;
cout << "This program has two options:" << endl;
cout << "1 - Create a data file, or " << endl;
cout << "2 - Read data from a file and print paychecks." << endl;
cout << "Please enter (1) to create a file or (2) to print checks: "<< endl;
cin >> userInput;
HourlyEmployee Potter(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00);
SalariedEmployee Dumbledore(2, "A. Dumbledore", "Hogwarts", "803-1230", 1200);
HourlyEmployee Weasley(3, "R. Weasley", "The Burrow", "892-2000", 40, 10.00);
SalariedEmployee Hagrid(4, "R. Hagrid", "Hogwarts", "910-8765", 1000);
if (userInput == option1) {
cout << " Please enter a file name: ";
cin >> fileName;
ofstream dataFile(fileName);
if (dataFile.good()) {
Potter.write(dataFile);
Dumbledore.write(dataFile);
Weasley.write(dataFile);
Hagrid.write(dataFile);
dataFile.close();
cout << "Data file created ... you can now run option 2." << endl;
}
else {
throw std::runtime_error("Couldn't create file.");
}
}
else if (userInput == option2) {
cout << " Please enter a file name: ";
cin >> fileName;
ifstream dataFile(fileName);
if (dataFile.good()) {
if (Potter.read(dataFile)) {
printCheck1(Potter);
}
if (Dumbledore.read(dataFile)) {
printCheck2(Dumbledore);
}
if (Weasley.read(dataFile)) {
printCheck1(Weasley);
}
if (Hagrid.readdataFile)) {
printCheck2(Hagrid);
}
else
cout << " Couldn't open file for input";
}
else {
throw std::runtime_error(" Couldn't open file for input");
}
}
else {
cout << "Invalid input";
}
}
void printCheck1(const Employee& b) {
cout << " ....................UVU Computer Science Dept................................. ";
cout << " Pay to the order of " << b.getEmployeeName() << "....................................$" << setprecision(2) << fixed << b.getCalcPay() << endl;
cout << " United Community Credit Union " << endl;
cout << ".............................................................................. " << endl;
cout << "Hours worked: " << b.getHoursWorked() << fixed << setprecision(2) << endl;
cout << "Hourly Wage: " << b.getHourlyWage() << fixed << setprecision(2) << endl << endl << endl < }
void printCheck2(const Employee& b) {
cout << " ....................UVU Computer Science Dept................................. ";
cout << " Pay to the order of " << b.getEmployeeName() << "....................................$" << setprecision(2) << fixed << b.getCalcPay() << endl;
cout << " United Community Credit Union " << endl;
cout << ".............................................................................. " << endl;
cout << "Salary: " << b.Salary() << fixed << setprecision(2) << endl;
}
--------------------------------------
//employee.cpp
#include "employee.h"
#include
using namespace std;
const char tab = ' ';
const char space = ' ';
Employee::Employee() {
employeeNumber = 0;
employeeName = "none";
employeeAddress = "none";
phoneNumber = "none";
hourlyWage = 0.00;
hoursWorked = 0.00;
}
Employee::HourlyEmployee(int empNumber, string empName, string empAddress, string empPhone, double hWage, double hWorked) {
employeeNumber = empNumber;
employeeName = empName;
employeeAddress = empAddress;
phoneNumber = empPhone;
hourlyWage = hWage;
hoursWorked = hWorked;
}
int Employee::getEmployeeNumber( ) const {
return employeeNumber;
}
string Employee::getEmployeeName( ) const {
return employeeName;
}
string Employee::getEmployeeAddress( ) const {
return employeeAddress;
}
string Employee::getPhoneNumber( ) const {
return phoneNumber;
}
double Employee::getHourlyWage( ) const {
return hourlyWage;
}
double Employee::getHoursWorked( ) const {
return hoursWorked;
}
void Employee::setName(string empName) {
name = empName;
}
void Employee::setAddress(string empAddress) {
address = empAddress;
}
void Employee::setPhone(string empPhone) {
phone = empPhone;
}
void Employee::setHourlyWorked(double hours) {
hoursWorked = hours;
}
void Employee::setHourlyWaged(double wage) {
hourlyWage = wage;
}
double Employee::getCalcPay() {
double pay = hoursWorked * hourlyWage;
double fedTax = 0.20;
double stateTax = 0.075;
double overtime = 1.5;
int overtimeHours = hoursWorked - 40;
if (hoursWorked > 40) {
double grossPay = (40 * hourlyWage) + (overtimeHours * (hourlyWage *overtime));
pay = grossPay - ((grossPay * fedTax) + (grossPay * stateTax));
return pay;
}
else {
pay = pay - ((pay * 0.2) + (pay * 0.075));
}
return pay;
}
void Employee::read(ifstream& inputStream) {
if (inputStream >> employeeNumber && getline(inputStream, employeeName, tab) && getline(inputStream, employeeAddress, tab) && getline(inputStream, phoneNumber, tab) && inputStream >> hourlyWage && inputStream >> hoursWorked) {
return true;
}
else {
return false;
}
}
void Employee::write(ofstream& outputStream) {
outputStream << employeeNumber << space << employeeName << tab << employeeAddress << tab << phoneNumber << tab << hourlyWage << space << hoursWorked << endl;
}
--------------------------------------
//employee.h
#include
#include
#include
#include
#include
#include
using namespace std;
class Employee {
public:
virtual ~Employee();
int getEmployeeNumber() const;
string getEmployeeName() const;
string getEmployeeAddress() const;
string getPhoneNumber() const;
double getCalcPay() const;
void setEmployeeName(string);
void setEmployeeAddress(string);
void setPhoneNumber(string);
void write(ofstream&);
double calcPay();
void printCheck();
private:
int employeeNumber;
string employeeName;
string employeeAddress;
string phoneNumber;
protected:
Employee();
void read(ifstream&);
Employee(int, string, string, string, double, double);
};
class HourlyEmployee : public Employee {
private:
double hoursWorked;
double hourlyWage;
public:
HourlyEmployee(int, string, string, string, double, double);
double getHoursWorked;
double getHourlyWage;
void setHoursWorked(double);
void setHourlyWage(double);
void write(ofstream&);
HourlyEmployee static read(ifstream&);
double calcPay();
void printCheck();
protected:
HourlyEmployee();
//virtual read(ifstream&);
};
class SalariedEmployee : public Employee {
private:
double salary;
public:
SalariedEmployee(int, string, string, string, double);
double getSalary;
void setSalary(double);
void write(ofstream&);
double calcPay;
void printCheck();
//static read(ifstream&);
protected:
SalariedEmployee();
//virtual read(ifstream&);
};
Explanation / Answer
//main.cpp
#include "Employee.h"
#include "HourlyEmployee.h"
#include "SalariedEmployee.h"
#include <stdexcept>
using namespace std;
//function explanations are in their respective headers
int main() {
vector<Employee*> employees = {
new HourlyEmployee(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00),
new SalariedEmployee(2, "A. Dumbledore", "Hogwarts", "808-1230", 1200),
new HourlyEmployee(3, "R. Weasley", "The Burrow", "892-2000", 40, 10),
new SalariedEmployee(4, "R. Hagrid", "Hogwarts", "910-8765", 1000)
};
cout << "Select an option: "
<< endl << "1 - Create a data file" << endl << "or"
<< endl << "2 - Read data from file and print paychecks"
<< endl << "Enter desired option: ";
int option;
cin >> option;
if (option == 1) {
cout << "Enter file name to be created (e.g. file.txt): ";
string fileName;
cin >> fileName;
ofstream o(fileName);
for (auto e : employees) {
e->write(o);
delete e;
}
employees.clear();
cout << endl << "file "" << fileName << "" created.";
}
else if (option == 2) {
cout << "Enter file name to be read from (e.g. file.txt): ";
string fileName;
cin >> fileName;
ifstream is(fileName);
try {
if (!is) {
throw runtime_error("Invalid file name/file not found: "" + fileName + "", exiting program now");
}
}
catch (runtime_error e) {
cout << endl << e.what();
cout << endl << endl;
system("pause");
return 0;
}
vector<Employee*> readEmployees;
readEmployees.push_back(HourlyEmployee::read(is));
readEmployees.push_back(SalariedEmployee::read(is));
readEmployees.push_back(HourlyEmployee::read(is));
readEmployees.push_back(SalariedEmployee::read(is));
for (auto e : readEmployees) {
e->printCheck();
}
}
cout << endl << endl;
system("pause");
return 0;
}
------------------------------------------------------------------------------------
//Employee.cpp
#include "Employee.h"
Employee::Employee(int employeeNumber, string name, string streetAddress, string phoneNumber) {
this->employeeNumber = employeeNumber;
this->name = name;
this->streetAddress = streetAddress;
this->phoneNumber = phoneNumber;
}
string Employee::getPhoneNumber() const {
return this->phoneNumber;
}
void Employee::setPhoneNumber(string phoneNumber) {
this->phoneNumber = phoneNumber;
}
string Employee::getStreetAddress()const {
return this->streetAddress;
}
void Employee::setStreetAddress(string streetAddress) {
this->streetAddress = streetAddress;
}
string Employee::getName()const {
return this->name;
}
void Employee::setName(string name) {
this->name = name;
}
double Employee::calcPay() const {
return -1;
}
void Employee::setEmployeeNumber(int num) {
this->employeeNumber = num;
}
int Employee::getEmployeeNumber()const {
return this->employeeNumber;
}
void Employee::write(ofstream& o){
o << this->getEmployeeNumber() << endl << this->getName() << endl << this->getStreetAddress() << endl << this->getPhoneNumber() << endl;
}
void Employee::readData(ifstream& is) {
int index = 0;
string data[4];
for (int i = 0; i < 4; i++) {
getline(is, data[i]);
}
this->setEmployeeNumber(stoi(data[0]));
this->setName(data[1]);
this->setStreetAddress(data[2]);
this->setPhoneNumber(data[3]);
}
void Employee::printCheck() {
cout << fixed << setprecision(2);
cout << endl << endl;
drawChar('-', 20);
cout << "UVU Computer Science Dept";
drawChar('-', 20);
cout << endl << endl
<< "Pay to the order of " << this->getName();
drawChar('.', 10);
cout << "$" << this->calcPay()
<< endl << endl
<< "United Community Credit Union" << endl;
drawChar('-', 68);
cout << endl;
}
void Employee::drawChar(char c, int num) {
for (int i = 0; i < num; i++) {
cout << c;
}
}
-----------------------------------------------------------------------------
//Employee.h
#pragma once
#include <string>
#include <math.h>
#include <fstream>
#include <vector>
#include <iostream>
#include <iomanip>
using namespace std;
//this class represents an employee and that employee's data
class Employee {
private:
int employeeNumber;
string name;
string streetAddress;
string phoneNumber;
protected:
Employee() = default;
void virtual readData(ifstream& is);
Employee(int employeeNumber, string name, string streetAddress, string phoneNumber);
public:
string getPhoneNumber() const;
//sets the employee's phone number
void setPhoneNumber(string phoneNumber);
//Do you really want me to do this ;_;?
//returns the employee's street adress
string getStreetAddress() const;
//sets the employee's street address
void setStreetAddress(string streetAddress);
//returns the employee's name
string getName() const;
//sets the employee's name
void setName(string name);
//set the employee's number
void setEmployeeNumber(int num);
//calculates the pay of an employee
virtual double calcPay() const = 0;
//returns the employee's employee number
int getEmployeeNumber() const;
//the write function, will write a premade set of data to a file
virtual void write(ofstream& o);
//prints out the paycheck of an employee
void virtual printCheck();
//prints out a character iteravely
void drawChar(char c, int num);
};
-----------------------------------------------------------------------------------
//HourlyEmployee.cpp
#include "HourlyEmployee.h"
HourlyEmployee::HourlyEmployee(int employeeNumber, string name, string streetAddress, string phoneNumber, double hoursWorked, double hourlyWage) : Employee::Employee(employeeNumber, name, streetAddress, phoneNumber) {
this->hourlyWage = hourlyWage;
this->hoursWorked = hoursWorked;
}
double HourlyEmployee::getHoursWorked() const {
return this->hoursWorked;
}
void HourlyEmployee::setHoursWorked(double hoursWorked) {
this->hoursWorked = hoursWorked;
}
double HourlyEmployee::getHourlyWage() const {
return this->hourlyWage;
}
void HourlyEmployee::setHourlyWage(double hourlyWage) {
this->hourlyWage = hourlyWage;
}
double HourlyEmployee::calcPay() const {
double overTime = 0;
double overTimePay = 0;
double regularPay = 0;
const double FEDERAL_TAX = .2;
const double STATE_TAX = .075;
double grossPay = 0;
double netPay = 0;
double totalHours = hoursWorked;
if (totalHours > 40) {
overTime = hoursWorked - 40;
overTimePay = overTime * (hourlyWage * 1.5);
totalHours -= overTime;
}
regularPay = hourlyWage * totalHours;
grossPay = regularPay + overTimePay;
netPay = grossPay * (1 - FEDERAL_TAX);
netPay -= grossPay * STATE_TAX;
//round to the second decimal's place (100.345 = 100.35, 100.342 = 100.34)
netPay *= 100;
netPay = (abs(static_cast<int>(netPay) - netPay) >= .5) ? ceil(netPay) : floor(netPay);
netPay /= 100;
return netPay;
}
void HourlyEmployee::readData(ifstream & is)
{
Employee::readData(is);
string dat[2];
getline(is, dat[0]);
getline(is, dat[1]);
this->setHoursWorked(stod(dat[0]));
this->setHourlyWage(stod(dat[1]));
}
void HourlyEmployee::write(ofstream& o) {
Employee::write(o);
o << this->getHoursWorked() << endl << this->getHourlyWage() << endl;
}
HourlyEmployee* HourlyEmployee::read(ifstream& is) {
HourlyEmployee* e = new HourlyEmployee();
e->readData(is);
return e;
}
void HourlyEmployee::printCheck() {
Employee::printCheck();
cout << "Hours Worked: " << this->hoursWorked << endl << "Hourly Wage: " << this->hourlyWage
<< endl << endl;
}
-------------------------------------------------------------------------------------------------
//HourlyEmployee.h
#pragma once
#include <string>
#include <math.h>
#include <fstream>
#include <vector>
#include "Employee.h"
using namespace std;
//this class represents an employee and that employee's data
class HourlyEmployee : public Employee{
private:
double hourlyWage;
double hoursWorked;
protected:
void virtual readData(ifstream& is) override;
HourlyEmployee() = default;
public:
HourlyEmployee(int employeeNumber, string name, string streetAddress, string phoneNumber, double hourlyWage, double hoursWorked);
//returns the employee's hours worked
double getHoursWorked() const;
//sets the employee's hours worked
void setHoursWorked(double hoursWorked);
double getHourlyWage() const;
//sets the hourly wage of an hourly employee
void setHourlyWage(double hourlyWage);
//calculates the pay of an employee
double virtual calcPay() const;
//the write function, will write a premade set of data to a file
virtual void write(ofstream& o) override;
//the read function
static HourlyEmployee* read(ifstream& is);
//prints out the paycheck of an employee
void virtual printCheck() override;
};
----------------------------------------------------------------------------------------
//SalariedEmployee.cpp
#include "SalariedEmployee.h"
SalariedEmployee::SalariedEmployee(int employeeNumber, string name, string streetAddress, string phoneNumber, double salary) : Employee::Employee(employeeNumber, name, streetAddress, phoneNumber), salary{ salary }
{
}
void SalariedEmployee::readData(ifstream & is)
{
Employee::readData(is);
string dat[2];
getline(is, dat[0]);
this->salary = stod(dat[0]);
}
void SalariedEmployee::write(ofstream & o)
{
Employee::write(o);
o << this->salary << endl;
}
double SalariedEmployee::getSalary()
{
return this->salary;
}
void SalariedEmployee::setSalary(double salary)
{
this->salary = salary;
}
double SalariedEmployee::calcPay() const
{
const double FEDERAL_TAX = .2;
const double STATE_TAX = .075;
const double BENEFITS = .0524;
double grossPay = this->salary;
double netPay = 0;
netPay = grossPay * (1 - FEDERAL_TAX);
netPay -= grossPay * STATE_TAX;
netPay -= grossPay * BENEFITS;
//round to the second decimal's place (100.345 = 100.35, 100.342 = 100.34)
netPay *= 100;
netPay = (abs(static_cast<int>(netPay) - netPay) >= .5) ? ceil(netPay) : floor(netPay);
netPay /= 100;
return netPay;
}
void SalariedEmployee::printCheck()
{
Employee::printCheck();
cout << "Salary: " << this->salary << endl << endl;
}
SalariedEmployee * SalariedEmployee::read(ifstream & is)
{
SalariedEmployee* emp = new SalariedEmployee();
emp->readData(is);
return emp;
}
-----------------------------------------------------------------------------------------------------------------------
//SalariedEmployee.h
#include "Employee.h"
class SalariedEmployee :
public Employee
{
private:
double salary;
protected:
//the readData function, reads in data from a file
virtual void readData(ifstream& is) override;
public:
SalariedEmployee() = default;
SalariedEmployee(int employeeNumber, string name, string streetAddress, string phoneNumber, double salary);
//the write function, will write a premade set of data to a file
void virtual write(ofstream& o) override;
//gets the salary of a salaried employee
double getSalary();
//sets the salary of a salaried employee
void setSalary(double salary);
virtual double calcPay() const override;
//prints out the paycheck of an employee
virtual void printCheck() override;
static SalariedEmployee* read(ifstream& is);
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.