我已经阅读了有关此主题的几个先前提出的问题,但似乎无法找到我正在寻找的答案。当我运行程序时,我没有收到任何错误,但我得到了很多垃圾数据。我知道这是因为我没有正确传递参数,但我是 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;
}
我的“get”函数都遵循这种模式:
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()
{
}