Question: Can someone simplify this? I only have 3 files for this project. Employee.h , Employee.cpp, and Main We will rename Employee to HourlyEmployee, and then
Can someone simplify this? I only have 3 files for this project. Employee.h , Employee.cpp, and Main
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:
HourlyEmployee(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00); SalariedEmployee(2, "A. Dumbledore", "Hogwarts", "803-1230", 1200); HourlyEmployee(3, "R. Weasley", "The Burrow", "892-2000", 40, 10.00); SalariedEmployee(4, "R. Hagrid", "Hogwarts", "910-8765", 1000);
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:
This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter (1) to create a file or (2) to print checks:1 Please enter a file name: employee.txt Data file created ... you can now run option 2.
The contents of the file employee.txt should be:
1 H. Potter Privet Drive 201-9090 40 12 2 A. Dumbledore Hogwarts 803-1230 1200 3 R. Weasley The Burrow 892-2000 40 10 4 R. Hagrid Hogwarts 910-8765 1000
When you run option 2, your program should create a new vector of Employee pointers. Make four calls to either HourlyEmployee::reador 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:
This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter (1) to create a file or (2) to print checks:2 Please enter a file name: employee.dat ....................UVU Computer Science Dept................................. Pay to the order of H. Potter....................................$348.00 United Community Credit Union .............................................................................. Hours worked: 40.00 HourlyWage: 12.00 ....................UVU Computer Science Dept................................. Pay to the order of A. Dumbledore....................................$807.12 United Community Credit Union .............................................................................. Salary: 1200.00 ....................UVU Computer Science Dept................................. Pay to the order of R. Weasley....................................$290.00 United Community Credit Union .............................................................................. Hours worked: 40.00 HourlyWage: 10.00 ....................UVU Computer Science Dept................................. Pay to the order of R. Hagrid....................................$672.60 United Community Credit Union .............................................................................. //chegg answer with multiply files //could be used as a reference Salary: 1000.00
Driver.cpp --------------------------- #include
using namespace std;
const int ONE = 1, TWO = 2, SIZE = 4, HOURLY = 6, SALARIED = 5;
int main() { int objectCount = 0; int userOption = 0; string userFileName = "0"; // Presents the user with a menu of choices : // create a data file, or read data from a file and print checks. cout > userOption; if (userOption != ONE && userOption != TWO) { cout ::max(), ' '); }
} while (userOption != ONE && userOption != TWO);
MyVector
cout > userFileName;
// Create an ofstream object and open a file.
ofstream writeToFile(userFileName.c_str()); // Pass just the file name as the parameter(no path) so that your program assumes // the file to be in the same folder as your executable file. // Use a for loop to save each object's data to the file. MyVector payroll.clear(); // clear payroll but save for later use writeToFile.close(); // Close the file. // Print a message that creation of the file is complete. cout userFileName = "0"; cout > userFileName; ifstream readFile; // Open the file that the user iputs. do { readFile.open(userFileName.c_str()); if (readFile.fail()) { readFile.clear(); cout > userFileName; if (userFileName == "e" || userFileName == "E") { exit(1); } } } while (readFile.fail()); // Array of pointers for objects to be read in string dataType; Employee* ePtr = NULL; while (readFile >> dataType) // read in first line, quit when end of file is encountered { // see what kind of data we have if (dataType == "hourly") ePtr = new Hourly; else if (dataType == "salaried") ePtr = new Salaried; else cout // read in data for the object try { ePtr->readData(readFile); } catch (EofException badRead) { cout system("CLS"); // print checks for (int i = 0; i printCheck() // readFile.close(); // Can close the reading file at this point for (int i = 0; i // Skip a few lines of output and do a system("PAUSE") to wait for the user to hit Enter to continue. cout ------------------------------------------------ Employee.cpp --------------------------- #include "Employee.h" #include "Exception.h" #include Employee::Employee() { empNum = 0; name = ""; address = ""; phoneNum = ""; } Employee::Employee(int tempNum, string tempName, string tempAddress, string tempPhNum) { empNum = tempNum; name = tempName; address = tempAddress; phoneNum = tempPhNum; } void Employee::setEmpNumber(int tempNum) { empNum = tempNum; } void Employee::setName(string tempName) { name = tempName; } void Employee::setAddress(string tempAddress) { address = tempAddress; } void Employee::setPhoneNum(string tempPhNum) { phoneNum = tempPhNum; } int Employee::getEmpNumber() const { return empNum; } string Employee::getName() const { return name; } string Employee::getAddress() const { return address; } string Employee::getPhoneNum() const { return phoneNum; } // calcPay() will have no implementation in Employee class void Employee::readData(ifstream& in) { string tempString; in >> empNum; if (in.eof()) { throw EofException(); } else if (in.fail()) { throw invalid_argument(tempString); } else if (in.bad()) { throw out_of_range(tempString); } in.ignore(); getline(in, name); if (in.eof()) { throw EofException(); } getline(in, address); if (in.eof()) { throw EofException(); } getline(in, phoneNum); if (in.eof()) { throw EofException(); } } void Employee::writeData(ofstream& out) { out string Employee::printCheck() const { // in the base class printcheck function we display the check heading and the person's name // The printCheck function does not really print the check, it generates a string that will be // printed later ostringstream out; out getEmpNumber() getName() getAddress() getPhoneNum() getName(); return out.str(); } ------------------------------------------------ Employee.h --------------------------- #include class Employee { protected: int empNum; string name, address, phoneNum; public: Employee(); Employee(int, string, string, string); void setEmpNumber(int); void setName(string); void setAddress(string); void setPhoneNum(string); int getEmpNumber() const; string getName() const; string getAddress() const; string getPhoneNum() const; virtual double calcPay() const = 0; virtual void readData(ifstream&); virtual void writeData(ofstream&); virtual string printCheck() const; }; ------------------------------------------------ hourly.cpp --------------------------- #include "Employee.h" #include "Hourly.h" #include "Exception.h" #include Hourly::Hourly() { hrlyWage = 0; hrsWorked = 0; } Hourly::Hourly(int _empNum, string _name, string _address, string _phoneNum, double _hrsWorked, double _hrlyWage) : Employee(_empNum, _name, _address, _phoneNum) { //empNum = _empNum; /ame = _name; //address = _address; //phoneNum = _phoneNum; hrsWorked = _hrsWorked; hrlyWage = _hrlyWage; } void Hourly::setHrlyWage(double tempWage) { hrlyWage = tempWage; } void Hourly::setHrsWorked(double tempHours) { hrsWorked = tempHours; } double Hourly::getHrlyWage() const { return hrlyWage; } double Hourly::getHrsWorked() const { return hrsWorked; } // To compute the net pay, deduct 20% of the gross for Federal income tax, and 7.5% of the gross for state income tax. double Hourly::calcPay() const { double netPay, grossPay, overTimeHrs, regHours = 40, federalTax = 0.20, stateTax = 0.075, timeAndHalf = 1.5; if (hrsWorked > regHours) { overTimeHrs = hrsWorked - regHours; grossPay = ((regHours * hrlyWage) + ((overTimeHrs * hrlyWage) * timeAndHalf)); } else { grossPay = hrsWorked * hrlyWage; } netPay = grossPay - ((grossPay * federalTax) + (grossPay * stateTax)); return netPay; } void Hourly::readData(ifstream& in) { string tempString; in >> hrlyWage; if (in.eof()) { throw EofException(); } else if (in.fail()) { throw invalid_argument(tempString); } else if (in.bad()) { throw out_of_range(tempString); } in >> hrsWorked; if (in.eof()) { throw EofException(); } else if (in.fail()) { throw invalid_argument(tempString); } else if (in.bad()) { throw out_of_range(tempString); } Employee::readData(in); } void Hourly::writeData(ofstream& out) { out out string Hourly::printCheck() const { ostringstream out; out.setf(ios::fixed); out.setf(ios::showpoint); out.precision(2); out out calcPay() getHrsWorked() getHrlyWage(); return out.str(); } ------------------------------------------------ Hourly.h --------------------------- #include "Employee.h" #include class Hourly : public Employee { private: double hrlyWage, hrsWorked; public: Hourly(); Hourly(int, string, string, string, double, double); // setHrlyWage Function void setHrlyWage(double); // setHrsWorked Function void setHrsWorked(double); // getHrlyWage Function double getHrlyWage() const; // getHrsWorked Function double getHrsWorked() const; // calcPay Function double calcPay() const; // readData Function void readData(ifstream&); // writeData Function void writeData(ofstream&); // printCheck function string printCheck() const; }; ------------------------------------------------ Salaried.cpp --------------------------- #include "Employee.h" #include "Salaried.h" #include "Exception.h" #include Salaried::Salaried() { salary = 0; } Salaried::Salaried(int _empNum, string _name, string _address, string _phoneNum, double _salary) : Employee(_empNum, _name, _address, _phoneNum) { //empNum = _empNum; /ame = _name; //address = _address; //phoneNum = _phoneNum; salary = _salary; } void Salaried::setSalary(double _salary) { salary = _salary; } double Salaried::getSalary() const { return salary; } // 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% of the gross for benefits. double Salaried::calcPay() const { double netPay, federalTax = 0.20, stateTax = 0.075, benefits = 0.0524; netPay = salary - ((salary * federalTax) + (salary * stateTax) + (salary * benefits)); return netPay; } void Salaried::readData(ifstream& in) { string tempString; in >> salary; if (in.eof()) { throw EofException(); } else if (in.fail()) { throw invalid_argument(tempString); } else if (in.bad()) { throw out_of_range(tempString); } Employee::readData(in); } void Salaried::writeData(ofstream& out) { out } // create a string for the paycheck - calls the base class printCheck function string Salaried::printCheck() const { ostringstream out; out.setf(ios::fixed); out.setf(ios::showpoint); out.precision(2); out out calcPay() getSalary(); return out.str(); } ------------------------------------------------ Salaried.h --------------------------- #include "Employee.h" #include class Salaried : public Employee { private: double salary; public: Salaried(); Salaried(int, string, string, string, double); // setSalary Function void setSalary(double); // getSalary Function double getSalary() const; // calcPay Function double calcPay() const; // readData Function void readData(ifstream&); // writeData Function void writeData(ofstream&); // printCheck function string printCheck() const; }; ------------------------------------------------ Exception.cpp --------------------------- #include "Exception.h" #include WriteErrorException::WriteErrorException() { messageBadWrite = "Error writing the data to file! Program cannot continue!"; } string WriteErrorException::getBadWriteMessage() const { return messageBadWrite; } EofException::EofException() { messageEof = "End of File (eof) encountered before data was read! Program cannot continue!"; } string EofException::getEofMessage() const { return messageEof; } ------------------------------------------------ Exception.h --------------------------- #include class WriteErrorException { private: string messageBadWrite; public: WriteErrorException(); string getBadWriteMessage() const; }; class EofException { private: string messageEof; public: EofException(); string getEofMessage() const; }; ------------------------------------------------ MyVector.h --------------------------- #include #pragma once template MyVector(); MyVector (int); MyVector(const MyVector ~MyVector(); // function size int size() const; // function capacity int capacity() const; //function clear void clear(); //function push_back void push_back(const T&); // function at T at(unsigned int)const; //operator= as a member function MyVector // operator friend ostream& operator&); }; //**************************************************************************************** template template template myVectorArray = new T[vectorCapacity]; // initialize myVectorArray as a new int array (dynamically allocated) for (unsigned int i = 0; i template if (this == &rho) // Test for self assignment with if statement { return *this; } delete[] this->myVectorArray; // Free up the storage of the left hand object using delete[] function this->vectorSize = rho.vectorSize; // Make size of this equal to size of right hand object using MyVector size() function this->vectorCapacity = rho.vectorCapacity; // Make capacity of this equal to capacity of right hand object using MyVector capacity() function this->myVectorArray = new T[vectorCapacity]; for (unsigned int i = 0; i myVectorArray[i] = rho.myVectorArray[i]; } return *this; // Return this object } // Destructor should clean up allocated storage from the heap here. template template template template template if (vectorSize + 1 > vectorCapacity) // if the array capacity won't hold another value { vectorCapacity *= TWO; // double the size of the array capacity tempArray = new T[vectorCapacity]; // Dynamically allocate a new array of integers // doubled capacity to be the capacity of the new array. for (unsigned int i = 0; i myVectorArray; // Delete the original array. myVectorArray = tempArray; } myVectorArray[vectorSize] = n; // Add the new element at the next open slot in the new array. vectorSize++; // Increment the size; } template template return out; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
