我已经阅读了有关此主题的几个先前提出的问题,但似乎无法找到我正在寻找的答案。当我运行程序时,我没有收到任何错误,但我收到了大量的垃圾数据。我知道这是因为我没有正确传递参数,但我是c ++的新手,特别是如何正确使用指针。为简单起见:我通过PrintCheck()
传递一个员工对象,该对象调用CalcSalary()
函数,该函数必须使用GetHours()
和GetWage()
来访问成员数据以进行计算和返回正确的salary
。非常感谢我为什么要生成垃圾数据!
我有一个班级:
class Employee
{
private:
int employeeNumber;
string name;
string streetAddress;
string phoneNumber;
double hourlyWage;
double hoursWorkedperWeek;
public:
Employee(void);
Employee(int, string, string, string, double, double);
int GetEmployeeNum() const;
void SetEmployeeNum(int);
string GetName() const;
void SetName(string);
string GetAddress() const;
void SetAddress(string);
string GetPhone() const;
void SetPhone(string);
double GetWage() const;
void SetWage(double);
double GetHours() const;
void SetHours(double);
double CalcPay(double, double);
};
我还有一个需要与班级互动的功能:
void PrintCheck(Employee&);
我的主要功能如下:
void main()
{
Employee joe(1, "Joe Blo", "125 Cool St.", "555 555 5555", 10.00, 45); //create employee 1
Employee jane(2, "Jane Doe", "521 Dumb St.", "1 800 555 5555", 12.50, 30); //create employee 2
PrintCheck(joe); //print check
}
printcheck函数如下所示:
void PrintCheck(Employee& a)
{
cout << "Pay to the order of " << a.GetName() << "...................................";
cout << a.CalcPay(a.GetWage(), a.GetHours()) << endl;
cout << "Hours worked: " << a.GetHours() << endl;
cout << "Hourly wage: " << a.GetWage() << endl;
}
Calcpay功能是:
double Employee::CalcPay(double h, double w)
{
double salary = 0;
int OT = 40;
double timeandahalf = 1.5;
double STATE = .075;
double FED = .20;
if (h > OT) // overtime
{
salary = h * (w * timeandahalf); // calc time and a half
salary = salary * STATE; // calc state deductions
salary = salary * FED; // calc federal deductions
}
else
{
salary = h * w; // calc salary
salary = salary * STATE; // calc state deductions
salary = salary * FED; // calc federal deductions
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(PRECISION);
return salary;
}
我的“获取”功能都遵循这种模式:
int Employee::GetEmployeeNum() const
{
return employeeNumber;
}
我的预期输出是:
Pay to the order of: Joe Blo............ $salary.
Hours worked: $hours.
Hourly wage: $wage.
我得到了什么:
Pay to the order of: ........................ 128509280503000000000000000000000000000.00 (this number is long, but I didn't feel like typing them all)
Hours worked: -9723636237 (same, just tons of bs numbers)
Hourly wage: (the exact same number as above)
我的类构造函数:
Employee::Employee(int en, string n, string a, string p, double w, double h)
{
int employeeNumber = en;
string name = n;
string streetAddress = a;
string phoneNumber = p;
double hourlyWage = w;
double hoursWorkedperWeek = h;
}
Employee::Employee()
{
}
答案 0 :(得分:4)
您的Employee::Employee(int en, string n, string a, string p, double w, double h)
声明了其中的新局部变量,因此会影响您班级中的成员变量。所以实际上,你在施工期间从未正确地初始化任何成员。
以下内容应该可以解决这个问题:
Employee::Employee(int en, string n, string a, string p, double w, double h)
: employeeNumber ( en ),
name ( n ),
streetAddress ( a ),
phoneNumber ( p ),
hourlyWage ( w ),
hoursWorkedperWeek ( h )
{
}
答案 1 :(得分:2)
这是错误
Employee::Employee(int en, string n, string a, string p, double w, double h)
{
int employeeNumber = en;
string name = n;
string streetAddress = a;
string phoneNumber = p;
double hourlyWage = w;
double hoursWorkedperWeek = h;
}
应该是
Employee::Employee(int en, string n, string a, string p, double w, double h)
{
employeeNumber = en;
name = n;
streetAddress = a;
phoneNumber = p;
hourlyWage = w;
hoursWorkedperWeek = h;
}
甚至更好,它应该像大狼的回答一样。
您的版本中的错误是您在构造函数中声明了与您的类成员具有完全相同名称的变量。这些变量隐藏您的类成员,因此您的构造函数不会初始化您的类。所以你得到了垃圾。