2 u013988033 u013988033 于 2015.07.14 10:13 提问

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

3个回答

yin3353623
yin3353623   2015.07.14 11:05

for(int i=0;i<NUM_EMP;i++)
{

    cout << "Employee" 
         << i+1 << " : " 
         << "Enter the numbers of hours: ";

    cin >> employee[i]; 

}
    改成:
     for(int i=0;i<NUM_EMP;i++)
{
   double _hours;
    cout << "Employee" 
         << i+1 << " : " 
         << "Enter the numbers of hours: ";
    cin >> _hours;
            employee[i].setHours(_hours); 

}
u010463663
u010463663   2015.07.15 16:55

#include
#include
#include "Payroll.h"

using namespace std;

int main()
{
const int NUM_EMP = 7;
double value,** 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 >> value;
    employee[i].setHours(value);
    employee[i].setPayRate(num);**

}

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;
}
system("pause");

return 0;

}

加粗的地方是修改的地方,num数据类型不对,输入num获取为PayRate,输入value获取为hours

CSDNXIAOS
CSDNXIAOS   2015.07.16 15:27

定义对象数组时,数组中的各个元素需要构造函数来初始化。数组能否定义成功,关键在于每个元素都有合适的构造函数,默认构造函数并不是必须的。  1.动态对象数组,对象所属类型必须有public默认构造函数. 2.静态对象数组(1)没有提供初始化式的元素将调用默认构造函数来初始化 (2)提供初始化式的构造函数将调用相应的构造函数  class Dog{pub......
答案就在这里:对象数组与构造函数
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

Csdn user default icon
上传中...
上传图片
插入图片