Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Employee Read / Write Program Modify Code from a previous project in order to have it fit the new guidelines HERE IS THE PREVIOUS

C++ Employee Read / Write Program

Modify Code from a previous project in order to have it fit the new guidelines

HERE IS THE PREVIOUS .CPP FILE

#include

#include

#include

#include "Employee.h"

#include

using namespace std;

Employee::Employee()

{

employeeNumber = 0; // Employee's employee number

employeeName = ""; //Employee's name

streetAddress = ""; //Employee's street address

phoneNumber = ""; //Employee's phone number

hourlyWage = 0; //Employee's hourly wage

hoursWorked = 0;

grossPay = 0;

netPay = 0;

}

Employee::Employee(int empNum, string empName, string streetAddress, string phoneNumber, double hourlyWage, double hoursWorked)

{

employeeNumber = empNum;

employeeName = empName;

this->streetAddress = streetAddress;

this->phoneNumber = phoneNumber;

this->hourlyWage = hourlyWage;

this->hoursWorked = hoursWorked;

grossPay = 0;

netPay = 0;

}

int Employee::getEmployeeNumber()

{

return employeeNumber;

}

void Employee::setEmployeeNumber(int empNum)

{

employeeNumber = empNum;

}

string Employee::getEmployeeName()

{

return employeeName;

}

void Employee::setEmployeeName(string empName)

{

employeeName = empName;

}

string Employee::getStreetAddress()

{

return streetAddress;

}

void Employee::setStreetAddress(string strtAddrs)

{

streetAddress = strtAddrs;

}

string Employee::getPhoneNumber()

{

return phoneNumber;

}

void Employee::setPhoneNumber(string phnNum)

{

phoneNumber = phnNum;

}

double Employee::getHourlyWage()

{

return hourlyWage;

}

void Employee::setHourlyWage(double hrWage)

{

hourlyWage = hrWage;

}

double Employee::getHoursWorked()

{

return hoursWorked;

}

void Employee::setHoursWorked(double hrWorked)

{

hoursWorked = hrWorked;

}

void printCheck(Employee ee)

{

cout

cout

cout

cout

cout

cout

cout

}//End of function

void read(ifstream &in)

{

Employee employees[10];

int counter = 0;

while (in.read((char *)&employees[counter++], sizeof(Employee)))

for (int i = 0; i

{

printCheck(employees[i]);

}

in.close();

}

void write(ofstream &out)

{

// Instantiate your employees here first, then call their functions.

Employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 10.00, 45.00);

printCheck(joe);

Employee sam(21, "Sam Jones", "45 East State", "661-9000", 12.00, 30.00);

printCheck(sam);

Employee mary(15, "Mary Smith", "12 High Street", "401-8900", 15.00, 40.00);

printCheck(mary);

out.write((char *)(&joe), sizeof(Employee));

out.write((char *)(&sam), sizeof(Employee));

out.write((char *)(&mary), sizeof(Employee));

out.close();

}

//Main function

int main()

{

int choice;

string filename;

while (true)

{

cout

cout

cout

cout

cout to create a file or to print checks: ";

cin >> choice;

if (choice == 1)

{

cout

cin >> filename;

ofstream out(filename);

out.open(filename.c_str(), ios::binary);

write(out);

}

else if (choice == 2)

{

cout

cin >> filename;

ifstream in(filename);

in.open(filename.c_str(), ios::binary);

read(in);

}

else break;

//Calls function to displays information

}

}//End of main

HERE IS THE PREVIOUS .H FILE

#pragma once

#include

using namespace std;

class Employee {

private:

int employeeNumber; // Employee's employee number

string employeeName; //Employee's name

string streetAddress; //Employee's street address

string phoneNumber; //Employee's phone number

double hourlyWage; //Employee's hourly wage

double hoursWorked; //Employee's hours worked

double netPay; //Net pay

double grossPay; //Gross pay

public:

Employee();

Employee(int, string, string, string, double, double);

int getEmployeeNumber();

void setEmployeeNumber(int);

string getEmployeeName();

void setEmployeeName(string);

string getStreetAddress();

void setStreetAddress(string);

string getPhoneNumber();

void setPhoneNumber(string);

double getHourlyWage();

void setHourlyWage(double);

double getHoursWorked();

void setHoursWorked(double);

double calcPay()

{

const int OVER = 40;

double federal = 0.20;

double state = 0.075;

double timeHalf = 1.5;

double grossPay;

double netPay;

if (getHoursWorked()

{

grossPay = getHoursWorked() * getHourlyWage();

netPay = grossPay - (grossPay * federal) - (grossPay * state);

}

if (getHoursWorked() >= OVER)

{

grossPay = getHoursWorked() * ((getHourlyWage() * timeHalf));

netPay = grossPay - (grossPay * federal) - (grossPay * state);

}

return netPay;

}

};

image text in transcribed

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, it defines 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 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 is empty (which is what = defaultaccomplishes 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.

Objectives:

1. Continue gaining experience with object-oriented design

2. Learn to create a class hierarchy that is enabled for polymorphism

3. Learn to use polymorphism in a program.

The Driver

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::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:

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.txt 
--------------------FluffShuffle Electronics------------------------------- 
Pay to the order of H. Potter....................................$348.00 
United Bank of Eastern Orem 
Hours worked: 40.00 
HourlyWage: 12.00 
--------------------FluffShuffle Electronics------------------------------- 
Pay to the order of A. Dumbledore....................................$807.12 
United Bank of Eastern Orem 
Salary: 1200.00 
--------------------FluffShuffle Electronics------------------------------- 
Employee getEmployeeNumber getName getAddress getPhone Employee read(ifstream&) virtual void write(ofstream&) virtual double calcPay 0 virtual printCheck 0 virtual void readData -0 virtual Employee() Hourly Salaried Employee Employee getHours Worked getWeekly Salary getHourly Wage override write override write override calcPay override calcPay override printCheck override printCheck override readData override readData Employee getEmployeeNumber getName getAddress getPhone Employee read(ifstream&) virtual void write(ofstream&) virtual double calcPay 0 virtual printCheck 0 virtual void readData -0 virtual Employee() Hourly Salaried Employee Employee getHours Worked getWeekly Salary getHourly Wage override write override write override calcPay override calcPay override printCheck override printCheck override readData override readData

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. What is input VAT?

Answered: 1 week ago

Question

What were your most important educational experiences?

Answered: 1 week ago

Question

Which personal relationships influenced you the most?

Answered: 1 week ago