u013988033 于 2015.07.14 10:13 提问

c++对象数组 构造函数

`````` #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;
}
``````

`````` #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;
}

``````

`````` #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   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   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;
``````

}

CSDNXIAOS   2015.07.16 15:27

----------------------Hi，地球人，我是问答机器人小S，上面的内容就是我狂拽酷炫叼炸天的答案，除了赞同，你还有别的选择吗？