patterncppMinor
Payroll Program using 3 types of pass-by
Viewed 0 times
payrollpassprogramusingtypes
Problem
This program is a payroll using calculate two types of employee. Two overloaded functions for two type of employee (hourly and salaried). There must be one pass-by-value, one pass-by-reference, and two pass-by-reference using pointer. All the variable must be local. There are 3 files of this project.
Interface
Implementation
```
#include
#include "Payroll.h"
#include
#include
#include
using namespace std;
double grossPay;
PayRoll::PayRoll()
{
double fedRate;
double stateRate;
cout > fedRate;
while (fedRate > fedRate;
}
cout > stateRate;
while (stateRate > stateRate;
}
}
void PayRoll::choice()
{
cout > choice;
string lastName;
string firstName;
switch (choice)
{
case '1':
double hours;
double payrate;
cout > lastName;
cout > firstName;
if (firstName.length() > hours;
cout > payrate;
payrate = (payrate);
gross(hours, payrate);
cout > lastName;
cout > firstName;
if (firstName.length() > salary;
salary = (salary);
gross(salary);
cout 0 && hours 40 && hours 50)
grossPay = ((40 payrate) + ((10) (payrate 1.5)) + (hours - 50) (payrate * 2));
}
void PayRoll::gross(double&salary)
{
grossPay = salary / 52;
}
double PayRoll::calcFed()
{
double fedRate = 10;
double* nPtr(&fedRate);
double FedTax = grossPay (nPtr / 100);
return FedTax;
}
double PayRoll::calcState()
{
double stateRate = 5;
double* pPtr(&stateRate);
double StateTax = grossPay (pPtr / 100);
return StateTax;
}
double PayRoll::calcNetPay(double FedTax, doubl
Interface
class PayRoll
{
public:
PayRoll();
void choice();
void gross(double&);
void gross(double, double);
double calcFed();
double calcState();
double calcNetPay(double, double);
};Implementation
```
#include
#include "Payroll.h"
#include
#include
#include
using namespace std;
double grossPay;
PayRoll::PayRoll()
{
double fedRate;
double stateRate;
cout > fedRate;
while (fedRate > fedRate;
}
cout > stateRate;
while (stateRate > stateRate;
}
}
void PayRoll::choice()
{
cout > choice;
string lastName;
string firstName;
switch (choice)
{
case '1':
double hours;
double payrate;
cout > lastName;
cout > firstName;
if (firstName.length() > hours;
cout > payrate;
payrate = (payrate);
gross(hours, payrate);
cout > lastName;
cout > firstName;
if (firstName.length() > salary;
salary = (salary);
gross(salary);
cout 0 && hours 40 && hours 50)
grossPay = ((40 payrate) + ((10) (payrate 1.5)) + (hours - 50) (payrate * 2));
}
void PayRoll::gross(double&salary)
{
grossPay = salary / 52;
}
double PayRoll::calcFed()
{
double fedRate = 10;
double* nPtr(&fedRate);
double FedTax = grossPay (nPtr / 100);
return FedTax;
}
double PayRoll::calcState()
{
double stateRate = 5;
double* pPtr(&stateRate);
double StateTax = grossPay (pPtr / 100);
return StateTax;
}
double PayRoll::calcNetPay(double FedTax, doubl
Solution
using namespace std; - not a very good practice because namespaces can have functions and classes with the same names, which become ambiguous and can cause various problems: Why is using namespace std; considered bad practice.You can clean your code up by adding more functions. Why don't you create a function to output the employee data:
void outputEmployeeData()
{
std::cout << "Employee: " << lastName << ", " << firstName << std::endl;
std::cout << setprecision(2) << fixed;
std::cout << "Gross Pay: " << grossPay << std::endl
<< "Federal Tax: " << calcFed() << std::endl
<< "State Tax: " << calcState() << std::endl
<< "Total Tax: " << calcFed() + calcState() << std::endl
<< "Net Pay: " << grossPay - (calcFed() + calcState()) << std::endl;
}You will need to pass the data in to this method.
You can also clean your code up by adding
private variables and methods to the Payroll class:class PayRoll
{
public:
PayRoll();
void choice();
void gross(double&);
void gross(double, double);
double calcFed();
double calcState();
double calcNetPay(double, double);
private:
double grossPay;
};Now, you don't need to have a global variable in your implementation file.
I would probably create an
Employee class and maybe have a vector of employees in the payroll. You could then have functions to add and remove a given employee, and you could just pass the output method an Employee variable that it could pull all the data from.class Employee
{
public:
Employee(string fName, string lName, int wage);
string getFirstName();
string getLastName();
void setWage(int); // PLEASE don't use doubles for money, use ints
private:
string firstName;
string lastName;
int wage;
};Here is the updated Payroll class:
class PayRoll
{
public:
PayRoll();
void choice();
void gross(double&);
void gross(double, double);
double calcFed();
double calcState();
double calcNetPay(double, double);
void addEmployee(Employee);
void removeEmployee(Employee);
private:
std::vector employees;
};Warning:
I did not write this in an IDE, nor did I test it. It may not compile without modification.
Code Snippets
void outputEmployeeData()
{
std::cout << "Employee: " << lastName << ", " << firstName << std::endl;
std::cout << setprecision(2) << fixed;
std::cout << "Gross Pay: " << grossPay << std::endl
<< "Federal Tax: " << calcFed() << std::endl
<< "State Tax: " << calcState() << std::endl
<< "Total Tax: " << calcFed() + calcState() << std::endl
<< "Net Pay: " << grossPay - (calcFed() + calcState()) << std::endl;
}class PayRoll
{
public:
PayRoll();
void choice();
void gross(double&);
void gross(double, double);
double calcFed();
double calcState();
double calcNetPay(double, double);
private:
double grossPay;
};class Employee
{
public:
Employee(string fName, string lName, int wage);
string getFirstName();
string getLastName();
void setWage(int); // PLEASE don't use doubles for money, use ints
private:
string firstName;
string lastName;
int wage;
};class PayRoll
{
public:
PayRoll();
void choice();
void gross(double&);
void gross(double, double);
double calcFed();
double calcState();
double calcNetPay(double, double);
void addEmployee(Employee);
void removeEmployee(Employee);
private:
std::vector<Employee> employees;
};Context
StackExchange Code Review Q#85729, answer score: 3
Revisions (0)
No revisions yet.