c++ Could somebody break down this code I\'m about to post and explain it to me
ID: 3850170 • Letter: C
Question
c++
Could somebody break down this code I'm about to post and explain it to me how it works. I understand parts but if their is at all an ELI5(Explain like im 5) here I would really appreciate.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Employee_Info{
public:
int employeeid;
string firstname, lastname;
char taxstatus, typeemp;
double hoursworked, hourlyrate, overtimehours, overtimepay, grosspay, grosssalary, taxrate, taxamount, netpay;
};//end class
class Employee :public Employee_Info{
public:
ifstream input;
Employee_Info empi[100];
int counter;
void headers();
virtual void grosspaycalc() = 0;
virtual void taxratecalc() = 0;
virtual void netpaycalc() = 0;
virtual void employee_report() = 0;
void netpaysort();
virtual void displayinfo() = 0;
};//end class
void Employee::netpaysort(){
for (int pass = 1; pass <= counter; pass = pass + 1) {
for (int i = 0; i<counter - 1; i = i + 1) {
if (empi[i].netpay<empi[i + 1].netpay) {
swap(empi[i].netpay, empi[i + 1].netpay);
}//end if
}//end for
}//end for
}//end netpaysort
class Hourly_Employee : public Employee{
public:
Hourly_Employee();//constructor
~Hourly_Employee();//destructor
void grosspaycalc(){
for (int i = 0; i<counter; i++) {
if(empi[i].typeemp == 'Y')
{
// For salary based employees, to find the regular (gross) pay for a week, divide the salary by 52.
// To compute the overtime pay for a salary based employee,
// first find the hourly rate by dividing the gross pay by 40, and then compute overtime pay.
double gross_pay = empi[i].grosspay/52;
empi[i].hourlyrate = gross_pay/40;
if (empi[i].hoursworked>40) {
empi[i].overtimehours = empi[i].hoursworked - 40;
empi[i].overtimepay = 1.5*empi[i].overtimehours*empi[i].hourlyrate;
}
else
{
empi[i].overtimehours = 0;
empi[i].overtimepay = 0;
}
}
else if (empi[i].hoursworked>40){
empi[i].overtimehours = empi[i].hoursworked - 40;
empi[i].overtimepay = 1.5*empi[i].overtimehours*empi[i].hourlyrate;
empi[i].grosspay = empi[i].overtimepay + (empi[i].hoursworked*empi[i].hourlyrate);
}//end if
else
{
empi[i].overtimehours = 0;
empi[i].overtimepay = 0;
empi[i].grosspay = empi[i].hoursworked*empi[i].hourlyrate;
}
}//end for
}//end grosspaycalc
void taxratecalc(){
for (int i = 0; i<counter; i++) {
if (empi[i].grosspay>1000) {
empi[i].taxrate = .30;
}//end if
if (empi[i].grosspay>800 && empi[i].grosspay <= 1000) {
empi[i].taxrate = .20;
}//end if
if (empi[i].grosspay>500 && empi[i].grosspay <= 800) {
empi[i].taxrate = .10;
}//end if
if (empi[i].grosspay >= 0 && empi[i].grosspay <= 500) {
empi[i].taxrate = 0;
}//end if
if ((empi[i].taxstatus == 'H' || empi[i].taxstatus == 'h') && (empi[i].grosspay>500)){
empi[i].taxrate = empi[i].taxrate - .05;
}//end if
if (empi[i].taxstatus == 'S' || empi[i].taxstatus == 's'){
empi[i].taxrate = empi[i].taxrate + .05;
}//end if
if (empi[i].taxstatus == 'M' || empi[i].taxstatus == 'm'){
empi[i].taxrate = empi[i].taxrate;
}//end if
}//end for
}//end Hourly_Employee taxratecalc
void netpaycalc(){
for (int i = 0; i<counter; i++) {
empi[i].taxamount = empi[i].grosspay*empi[i].taxrate;
empi[i].netpay = empi[i].grosspay - empi[i].taxamount;
}//end for
}//end netpaycalc
void displayinfo(){
//if (empi[i].typeemp == 'N' || empi[i].typeemp == 'n') {
cout << setiosflags(ios::fixed | ios::showpoint | ios::left) << setprecision(2);
for(int i=0; i<counter; i++)
cout << setw(15) << empi[i].employeeid << setw(15) << empi[i].firstname << setw(15) << empi[i].lastname << "XXX-XX-" << setw(15) << empi[i].taxstatus << setw(15) << empi[i].typeemp << setw(15) << empi[i].hoursworked << "$ " << setw(15) << empi[i].hourlyrate << setw(15) << empi[i].grosspay << setw(15) << empi[i].overtimehours << "$ " << setw(17) << empi[i].overtimepay << "% " << setw(15) << empi[i].taxrate * 100 << "$ " << setw(15) << empi[i].taxamount << "$ " << setw(15) << empi[i].netpay << endl;
//}//end if
}//end employeeinfo
void employee_report(){
grosspaycalc();
taxratecalc();
netpaycalc();
netpaysort();
}//end readindata
};//end class
Hourly_Employee::Hourly_Employee():Employee(){
input.open("data.txt");
counter=0;
while (input >> empi[counter].firstname >> empi[counter].lastname >> empi[counter].employeeid >> empi[counter].taxstatus >> empi[counter].typeemp
>> empi[counter].hoursworked >> empi[counter].hourlyrate >> empi[counter].grosspay) {
counter++;
}//end while
}//end constructor
Hourly_Employee::~Hourly_Employee(){
input.close();
}// end Destructor
//end function
void headers();
int main(int argc, char * argv[]){
const int MAXSIZE = 70;
Hourly_Employee hourly_report;
cout << setw(100) << "NOREEN Payroll Program" << endl;
headers();
//for (int i = 0; i<MAXSIZE; i++) {
hourly_report.employee_report();
hourly_report.displayinfo();
//}//end for
return 0;
}// End Main
void headers(){
cout << setiosflags(ios::fixed | ios::showpoint | ios::left);
cout << setw(15) << "EMPLOYEE ID" << setw(15) << "FIRST NAME" << setw(15) << "LAST NAME" << setw(22) << "TAX STATUS" << setw(15) << "SALARIED" << setw(15) << "HOURS WORKED" << setw(17) << "HOURLY RATE" << setw(15) << "GROSS PAY" << setw(15) << "OVERTIME HOURS" << setw(18) << "OVERTIME PAY" << setw(18) << "TAX RATE" << setw(17) << "TAX AMOUNT" << setw(15) << "NET PAY" << endl;
}//end
Explanation / Answer
//header files
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//class Employee_Info in which variables are declared public
class Employee_Info{
public:
int employeeid;
string firstname, lastname;
char taxstatus, typeemp;
double hoursworked, hourlyrate, overtimehours, overtimepay, grosspay, grosssalary, taxrate, taxamount, netpay;
};//end class
// class Employee which is derived from Employee_Info
class Employee :public Employee_Info{
public:
ifstream input;
Employee_Info empi[100]; //array of 100 objects of Employee_Info class ie 100 Employees
int counter;
void headers();
// all the following functions are declared pure virtual functions which need to be override
virtual void grosspaycalc() = 0;
virtual void taxratecalc() = 0;
virtual void netpaycalc() = 0;
virtual void employee_report() = 0;
void netpaysort();
virtual void displayinfo() = 0;
};//end class
// sort the netPay of Employees in decreasing order using bubble sort method
void Employee::netpaysort(){
for (int pass = 1; pass <= counter; pass = pass + 1) {
for (int i = 0; i<counter - 1; i = i + 1) {
if (empi[i].netpay<empi[i + 1].netpay) { //if netpay are not in decreasing order swap
swap(empi[i].netpay, empi[i + 1].netpay);
}//end if
}//end for
}//end for
}//end netpaysort
// Hourly_Employee class is derived from Employee class
class Hourly_Employee : public Employee{
public:
Hourly_Employee();//constructor
~Hourly_Employee();//destructor
// override the pure virtual functions of base Employee class here
void grosspaycalc(){
for (int i = 0; i<counter; i++) {
if(empi[i].typeemp == 'Y')
{
// For salary based employees, to find the regular (gross) pay for a week, divide the salary by 52.
// To compute the overtime pay for a salary based employee,
// first find the hourly rate by dividing the gross pay by 40, and then compute overtime pay.
double gross_pay = empi[i].grosspay/52;
empi[i].hourlyrate = gross_pay/40;
if (empi[i].hoursworked>40) {
empi[i].overtimehours = empi[i].hoursworked - 40;
empi[i].overtimepay = 1.5*empi[i].overtimehours*empi[i].hourlyrate;
}
else
{
empi[i].overtimehours = 0;
empi[i].overtimepay = 0;
}
}
else if (empi[i].hoursworked>40){
empi[i].overtimehours = empi[i].hoursworked - 40;
empi[i].overtimepay = 1.5*empi[i].overtimehours*empi[i].hourlyrate;
empi[i].grosspay = empi[i].overtimepay + (empi[i].hoursworked*empi[i].hourlyrate);
}//end if
else
{
empi[i].overtimehours = 0;
empi[i].overtimepay = 0;
empi[i].grosspay = empi[i].hoursworked*empi[i].hourlyrate;
}
}//end for
}//end grosspaycalc
void taxratecalc(){
for (int i = 0; i<counter; i++) {
if (empi[i].grosspay>1000) {
empi[i].taxrate = .30;
}//end if
if (empi[i].grosspay>800 && empi[i].grosspay <= 1000) {
empi[i].taxrate = .20;
}//end if
if (empi[i].grosspay>500 && empi[i].grosspay <= 800) {
empi[i].taxrate = .10;
}//end if
if (empi[i].grosspay >= 0 && empi[i].grosspay <= 500) {
empi[i].taxrate = 0;
}//end if
if ((empi[i].taxstatus == 'H' || empi[i].taxstatus == 'h') && (empi[i].grosspay>500)){
empi[i].taxrate = empi[i].taxrate - .05;
}//end if
if (empi[i].taxstatus == 'S' || empi[i].taxstatus == 's'){
empi[i].taxrate = empi[i].taxrate + .05;
}//end if
if (empi[i].taxstatus == 'M' || empi[i].taxstatus == 'm'){
empi[i].taxrate = empi[i].taxrate;
}//end if
}//end for
}//end Hourly_Employee taxratecalc
void netpaycalc(){
for (int i = 0; i<counter; i++) {
empi[i].taxamount = empi[i].grosspay*empi[i].taxrate;
empi[i].netpay = empi[i].grosspay - empi[i].taxamount;
}//end for
}//end netpaycalc
void displayinfo(){
//if (empi[i].typeemp == 'N' || empi[i].typeemp == 'n') {
cout << setiosflags(ios::fixed | ios::showpoint | ios::left) << setprecision(2);
for(int i=0; i<counter; i++)
cout << setw(15) << empi[i].employeeid << setw(15) << empi[i].firstname << setw(15) << empi[i].lastname << "XXX-XX-" << setw(15) << empi[i].taxstatus << setw(15) << empi[i].typeemp << setw(15) << empi[i].hoursworked << "$ " << setw(15) << empi[i].hourlyrate << setw(15) << empi[i].grosspay << setw(15) << empi[i].overtimehours << "$ " << setw(17) << empi[i].overtimepay << "% " << setw(15) << empi[i].taxrate * 100 << "$ " << setw(15) << empi[i].taxamount << "$ " << setw(15) << empi[i].netpay << endl;
//}//end if
}//end employeeinfo
void employee_report(){
grosspaycalc();
taxratecalc();
netpaycalc();
netpaysort();
}//end readindata
};//end class
Hourly_Employee::Hourly_Employee():Employee(){
input.open("data.txt"); //open the file
counter=0;
while (input >> empi[counter].firstname >> empi[counter].lastname >> empi[counter].employeeid >> empi[counter].taxstatus >> empi[counter].typeemp
>> empi[counter].hoursworked >> empi[counter].hourlyrate >> empi[counter].grosspay) {
counter++;
}//end while
}//end constructor
Hourly_Employee::~Hourly_Employee(){
input.close(); //close the file
}// end Destructor
//end function
void headers(); //prototype of function
int main(int argc, char * argv[]){
const int MAXSIZE = 70;
Hourly_Employee hourly_report;
cout << setw(100) << "NOREEN Payroll Program" << endl;
headers();
//for (int i = 0; i<MAXSIZE; i++) {
hourly_report.employee_report(); //report of 70 employees
hourly_report.displayinfo(); // display Info for 70 employees
//}//end for
return 0;
}// End Main
//definition of function headers()
void headers(){
cout << setiosflags(ios::fixed | ios::showpoint | ios::left);
cout << setw(15) << "EMPLOYEE ID" << setw(15) << "FIRST NAME" << setw(15) << "LAST NAME" << setw(22) << "TAX STATUS" << setw(15) << "SALARIED" << setw(15) << "HOURS WORKED" << setw(17) << "HOURLY RATE" << setw(15) << "GROSS PAY" << setw(15) << "OVERTIME HOURS" << setw(18) << "OVERTIME PAY" << setw(18) << "TAX RATE" << setw(17) << "TAX AMOUNT" << setw(15) << "NET PAY" << endl;
}//end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.