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 に答える