2

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

2 回答 2

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 )
{
}
于 2013-09-16T21:33:42.220 回答
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;
}

甚至更好,它应该像greatwolf的回答一样。

您的版本中的错误是您在构造函数中声明的变量与您的类成员的名称完全相同。这些变量隐藏了您的类成员,因此您的构造函数不会初始化您的类。所以你会得到垃圾。

于 2013-09-16T21:49:21.077 回答