c++对象数组 构造函数

题目是一个打印工资单(payrate*hours),数组里有七个对象,需要每个对象的输入时间(hour)乘payRate,最后返回到payTotal里,可是不知道怎么把键盘输入的存入对象数组里,而且总是报错,而且后面函数里的变量好像也有点问题,没用到数组,不知道怎么改啊,急求解答!!!!!非常感谢!!

这是main函数的cpp

 #include <iostream>
#include <iomanip>
#include "Payroll.h"

using namespace std;

int main()
{
    const int NUM_EMP = 7;
    double value;
    int num;
    Payroll employee[NUM_EMP];

    cout << "Enter the payRate for the employee: ";
    cin >> num;
    for(int i=0;i<NUM_EMP;i++)
    {

        cout << "Employee" 
             << i+1 << " : " 
             << "Enter the numbers of hours: ";
        cin >> employee[i]; 

    }

    Payroll item();
    cout << setprecision(2) << fixed;

    for(int k=0;k<NUM_EMP;k++)
    {
        cout << "The amount of gross pay for employee" 
             << k+1 << ": "
             << employee[k].getTotalPay << endl;
    }


    return 0;
}

下面是Payroll.cpp

 #include "Payroll.h"   // Needed for the Rectangle class
#include <iostream>      // Needed for cout
#include <cstdlib>       // Needed for the exit function
using namespace std;

Payroll::Payroll()
{
   hours = 0.0;
   payRate = 0.0;

}

void Payroll::setHours(double hou)
{
  if(hou <= 60.00)
      hours = hou;
  else
  {
      cout << "Please Enter the valid numbers: ";
      exit(EXIT_FAILURE);
  }
}

void Payroll::setPayRate(double pay)
{

    payRate = pay;
}

最后是payroll.h

 #ifndef PAYROLL_H
#define PAYROLL_H

class Payroll
{

private:
    double hours;
    double payRate;
public:
    Payroll();
    void setHours(double);
    void setPayRate(double);

    double getHours() const
    {

        return hours;
    }

    double getPayRate() const
    {

        return payRate;
    }

    double getTotalPay() const
    {

        return hours * payRate;
    }
};

#endif
查看全部
u013988033
行而知之
2015/07/14 02:13
  • c++
  • 对象
  • 点赞
  • 收藏
  • 回答
    私信

3个回复