C ++中的薪资计划

时间:2017-06-16 03:19:43

标签: c++

我一直在为这个课程编写课程,但是我的输出无法正确。

我的作业指示是:<​​/ p>

  

扩展员工薪资计划,以包括基于小时和薪水的员工。此阶段使用一组员工对象,不同类别员工的继承以及薪水计算的多态性。给出了52周的年薪以及以薪水为基础的员工的加班时数。

     

对于基于工资的员工,要找到一周的常规(总)工资,将工资除以52.要计算基于工资的员工的加班工资,首先通过将总工资除以40来找到每小时工资,然后计算加班费。对于每位员工,还必须计算加班费,税额和净工资。

     

此外,该计划应该找到所有员工的最低和最高净工资,并根据他们的净工资(升序)对员工进行分类

这就是我到目前为止所做的:

#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
int totalEmployeeCount,hrs,employeeCounter,stat,empID;
double rate,regPay;
string fName;
string lName;

class employee{
public:
double salary,hourlyRate,taxRate,taxAmount,grossPay,netPay,otPay;
int hours,otHours;

public: void setVariables(int empID,string fName,string lName, int stat, 
double rate, double hrs){
    int employeeID = empID;
    string firstName = fName;
    string lastName = lName;
    int payStat = stat;
    if(payStat ==1){hourlyRate = rate;}
    else{salary = rate;}
    hours = hrs;
}
//DECLARE FUNCTION TO CALCULATE GROSS PAY
public: virtual double calculateGrossPay()=0;

double calculateTaxAmount(){ taxRate = .30; // tax rate is a flat rate of 30%
taxAmount = grossPay*taxRate;  // CALCULATE TAX AMOUNT
return taxAmount;
}       // END CALCULATE TAXAMOUNT() FUNCTION.

double calculateNetPay(){
    netPay = grossPay - taxAmount;
    return netPay;
}       // END CALCULATENETPAY() FUNCTION.
void printData(){
    cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint);
    cout<<fName<<setw(6)<<lName<<setw(6)<<empID<<setw(10)<<hrs<<setw(3)
    <<otHours<<setw(8)<<grossPay<<setw(8)<<netPay<<setw(8)<<otPay<<endl;}  
// END PRINTDATA() FUNCTION
}; // END EMPLOYEE CLASS

class employeeSalary : public employee{
    public: double calculateGrossPay() { 
    double regPay = hours*hourlyRate;
    double hourlyRate = ((salary/52)/40);
    if (hours > 40) {otHours = (hours - 40); //calculate OT hours
     otPay = (otHours * hourlyRate); //calculate OT pay
     grossPay = (regPay + otPay); }
    else if (hours <= 40) {otHours = 0; otPay = 0; grossPay = regPay;}
    return grossPay;    } 
}; //end EmployeeSalary class 

class employeeHourly : public employee{
  public: double calculateGrossPay(){ 
   regPay = (40 * hourlyRate); //calculate regular hours
  if (hours > 40){   otHours = (hours - 40); //calculate OT hours
     otPay = (otHours * hourlyRate * 1.5); //calculate OT pay
     grossPay = (regPay + otPay); //calculate gross pay
  } //enf if clause for gross pay with overtime
  else { otHours = 0;   otPay = 0; grossPay = regPay;
  } //end else clause for four hours
  return grossPay;  } //end calculateGrossPay() function
}; //end EmployeeHourly class

int main(){
int employeeCounter;
cout<< "enter # of employees you want to process:  ";
cin>>totalEmployeeCount;
employee*employee[100];
while(employeeCounter < totalEmployeeCount){
    cout<<"Is employee "<<employeeCounter+1<< " hourly or salary? (enter 1 
for hourly / 2 for salary):";
    cin>>stat;
    if (stat == 1){cout<< "Instantiating and HOURLY employee object 
inherited from base class employee"
    <<endl<<endl;
        cout<<"Enter employee's ID: ";
        cin>>empID;
        cout<<"Enter employee's first name: "; 
         cin>>fName;
        cout<<"Enter employee's last name: ";
         cin>>lName;
        cout<<"Enter employee's hourly wage: "; 
         cin>>rate;
        cout<<"Enter employee's hours for this week: "; 
         cin>>hrs;
        employee[employeeCounter] = new employeeHourly();         
        employee[employeeCounter]->setVariables(empID, fName, lName, stat, 
rate, hrs);
        employee[employeeCounter]->calculateGrossPay(); 
        employee[employeeCounter]->calculateTaxAmount(); 
        employee[employeeCounter]->calculateNetPay();
        cout<<endl<<endl;
            employeeCounter++; } //end if

    else{cout<<"instantialting a SALARY employee object in herited from base class employee"
    <<endl<<endl;
    cout<<"Enter employee's ID: ";
        cin>>empID;
        cout<<"Enter employee's first name: "; 
         cin>>fName;
        cout<<"Enter employee's last name: ";
         cin>>lName;
        cout<<"Enter employee's hourly wage: "; 
         cin>>rate;
        cout<<"Enter employee's hours for this week: "; 
         cin>>hrs;
        employee[employeeCounter] = new employeeHourly();         
        employee[employeeCounter]->setVariables(empID, fName, lName, stat, 
    rate, hrs);
        employee[employeeCounter]->calculateGrossPay(); 
        employee[employeeCounter]->calculateTaxAmount(); 
        employee[employeeCounter]->calculateNetPay();
        cout<<endl<<endl;
        employeeCounter++;}
}
    /*employeeCounter = 0;
    while (employeeCounter < totalEmployeeCount){
        employee[employeeCounter]->printData();
        employeeCounter++;
    }*/
system("pause");    
}

当我在CMD中运行我的程序并为2名员工输入时,第二名员工将覆盖第一名员工。我不确定有什么问题,任何帮助或指导都会非常感激(我之前找不到这个问题,但我认为它已经存在,我是一名新生并且不熟悉Stack Overflow)。

1 个答案:

答案 0 :(得分:0)

初始化employeeCounter并且它将起作用:

int employeeCounter = 0;

坦率地说,这段代码根本就不存在。你需要至少一致地缩进它。你需要注意很多事情。

您应该浏览C++ Core Guidelines

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md

已修改 - 已更新代码

这是您的清理和功能代码:

#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;

class employee
{
public:
    double salary, hourlyRate, taxRate, taxAmount, grossPay, netPay, otPay;
    int hours, otHours;

    int payStat;
    int employeeID;
    string firstName;
    string lastName;

public:
    void setVariables( int empID, string fName, string lName, int stat, int rate, int hrs )
    {
        employeeID = empID;
        firstName = fName;
        lastName = lName;
        payStat = stat;

        if( payStat == 1 )
        {
            hourlyRate = rate;
        }
        else
        {
            salary = rate;
        }

        hours = hrs;
    }

public:
    virtual double calculateGrossPay() = 0;

    double calculateTaxAmount()
    {
        taxRate = .30;
        taxAmount = grossPay*taxRate;
        return taxAmount;
    }

    double calculateNetPay()
    {
        netPay = grossPay - taxAmount;
        return netPay;
    }

    void printData()
    {
        cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
        cout << firstName << setw(6) << lastName << setw(6) << employeeID << setw(10) << hours << setw(3)
             << otHours << setw(8) << grossPay << setw(8) << netPay << setw(8) << otPay << endl;
    }
};

class employeeSalary : public employee
{
public:
    double calculateGrossPay()
    {
        double regPay = hours*hourlyRate;
        double hourlyRate = ((salary/52)/40);
        if (hours > 40)
        {
            otHours = (hours - 40);
            otPay = (otHours * hourlyRate);
            grossPay = (regPay + otPay);
        }
        else if (hours <= 40)
        {
            otHours = 0; otPay = 0; grossPay = regPay;
        }

        return grossPay;
    }
};

class employeeHourly : public employee
{
public:
    double calculateGrossPay()
    {
        const double regPay = (40 * hourlyRate);

        if ( hours > 40 )
        {
            otHours = (hours - 40);
            otPay = (otHours * hourlyRate * 1.5);
            grossPay = (regPay + otPay);
        }
        else
        {
            otHours = 0;
            otPay = 0;
            grossPay = regPay;
        }

        return grossPay;
    }
};

int main()
{
    int employeeCounter = 0;
    int totalEmployeeCount = 0;

    string fName, lName;
    int empID = 0, stat = 0, rate = 0, hrs = 0;

    cout << "enter # of employees you want to process:  ";
    cin >> totalEmployeeCount;

    employee* employee[100];

    while( employeeCounter < totalEmployeeCount )
    {
        cout<<"Is employee "<< employeeCounter+1 << " hourly or salary? (enter 1 for hourly / 2 for salary):";
        cin>>stat;

        if (stat == 1)
        {
            cout << "Instantiating and HOURLY employee object inherited from base class employee" << endl << endl;

            cout<<"Enter employee's ID: ";
            cin>>empID;
            cout<<"Enter employee's first name: ";
            cin>>fName;
            cout<<"Enter employee's last name: ";
            cin>>lName;
            cout<<"Enter employee's hourly wage: ";
            cin>>rate;
            cout<<"Enter employee's hours for this week: ";
            cin>>hrs;

            employee[employeeCounter] = new employeeHourly();
            employee[employeeCounter]->setVariables( empID, fName, lName, stat, rate, hrs );
            employee[employeeCounter]->calculateGrossPay();
            employee[employeeCounter]->calculateTaxAmount();
            employee[employeeCounter]->calculateNetPay();
            cout<<endl<<endl;
            employeeCounter++;
        }
        else
        {
            cout<<"instantialting a SALARY employee object in herited from base class employee"<<endl<<endl;
            cout<<"Enter employee's ID: ";
            cin>>empID;
            cout<<"Enter employee's first name: ";
            cin>>fName;
            cout<<"Enter employee's last name: ";
            cin>>lName;
            cout<<"Enter employee's hourly wage: ";
            cin>>rate;
            cout<<"Enter employee's hours for this week: ";
            cin>>hrs;
            employee[employeeCounter] = new employeeHourly();
            employee[employeeCounter]->setVariables(empID, fName, lName, stat,
                                                    rate, hrs);
            employee[employeeCounter]->calculateGrossPay();
            employee[employeeCounter]->calculateTaxAmount();
            employee[employeeCounter]->calculateNetPay();
            cout<<endl<<endl;
            employeeCounter++;
        }
    }

    cout << "-----------------------------------------\n";

    for ( int i = 0; i < employeeCounter; ++i )
    {
        employee[ i ]->printData();
    }

    cout << "-----------------------------------------\n";

    return 0;
}

您有全局变量来存储数据,而在打印函数中,您正在打印这些全局变量。这意味着您只能获得最新的存储值。

真的需要通过OOP的基础知识来正确处理这些事情。

始终缩进代码。 初始化变量。 并且,不要评论每行代码。