将变量从一个函数传递到另一个函数(C ++)

时间:2014-05-02 15:47:20

标签: c++ function class

我似乎在将变量从一个函数传递到另一个函数时遇到了麻烦。我的主要课程如下:

class EmployeeClass {
public:
    void ImplementCalculations(string EmployeeName, double hours, double wage);
    void DisplayEmployeeInformation();
    void Addsomethingup (EmployeeClass, EmployeeClass, EmployeeClass);
    string EmployeeName;
    double hours;
    double wage;
    double basepay;
    double overtime_hours;
    double overtime_pay ;
    double overtime_extra ;
    double iTotal_salaries ;
    double iIndividualSalary;
    double iTotal_hours ;
    double iTotal_OvertimeHours;
};

我正在传递一个字符串和两个双精度函数" ImplementCalculations"如下所示:

Employee1.ImplementCalculations (Employee1.EmployeeName, Employee1.hours, Employee1.wage);
    Employee2.ImplementCalculations (Employee2.EmployeeName, Employee2.hours, Employee2.wage);
    Employee3.ImplementCalculations (Employee3.EmployeeName, Employee3.hours, Employee3.wage);

这些传递的变量对它们进行了一些数学运算:

void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage) {
    //Initialize overtime variables
    double overtime_hours=0;
    double overtime_pay=0;
    double overtime_extra=0;
    double basepay = 0;
    double iIndividualSalary = 0;


    if (hours > 40) 
    {       
        basepay = 40 * wage;
        overtime_hours = hours - 40;
        overtime_pay = wage * 1.5;
        overtime_extra = overtime_hours * overtime_pay;
        iIndividualSalary = overtime_extra + basepay;

        /*
        Implement function call to output the employee information.  Function is defined below.
        */
        DisplayEmployeeInformation ();


    }   // if (hours <= 40)
    else
    {   
        basepay = hours * wage;
        overtime_hours=0;
        overtime_extra=0;
        iIndividualSalary = basepay;
        /*
        Implement function call to output the employee information.  Function is defined below.
        */
        DisplayEmployeeInformation();

    }; // End of the else

然而,这是事情变得糟糕的地方。我认为,因为我没有传递任何它不想工作的DisplayEmployeeInformation,这也使得AddSomethingUp也不起作用。

void EmployeeClass::DisplayEmployeeInformation () {
    // This function displays all the employee output information.

    cout << "Employee Name ............. = " << EmployeeName << endl;
    cout << "Base Pay .................. = " << setprecision(5)<< basepay << endl;
    cout << "Hours in Overtime ......... = " << setprecision(4)<< overtime_hours << endl;
    cout << "Overtime Pay Amount........ = " << setprecision(5)<< overtime_pay << endl;
    cout << "Total Pay ................. = " << setprecision(6)<< iIndividualSalary << endl;


} // END OF Display Employee Information


void EmployeeClass::Addsomethingup (EmployeeClass Employee1, EmployeeClass Employee2, EmployeeClass Employee3){
    /* 
    Adds the total hours for objects 1, 2, and 3.
    Adds the salaries for each object.
    Adds the total overtime hours.
    */
    iTotal_hours = Employee1.hours + Employee2.hours + Employee3.hours;
    iTotal_salaries = Employee1.iIndividualSalary + Employee2.iIndividualSalary + Employee3.iIndividualSalary;
    iTotal_OvertimeHours = Employee1.overtime_hours + Employee2.overtime_hours + Employee3.overtime_hours;



    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%% Total Employee Salaries ..... = " << setprecision(6)<<iTotal_salaries <<endl;
    cout << "%%%% Total Employee Hours ........ = " << setprecision(5)<<iTotal_hours << endl;
    cout << "%%%% Total Overtime Hours......... = " << setprecision(5)<<iTotal_OvertimeHours << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;

} // End of function

AddSomethingUp中的一行是iTotal_hours。不知道为什么会这样,但它是唯一得到的。

所以我的问题是我必须传递DisplayEmployeeInformation某种变量吗?那应该是什么样的?更像是将信息传递给ImplementCalculations?

2 个答案:

答案 0 :(得分:1)

EmployeeClass有许多成员变量,例如:hours,overtime_hours等。

ImplementCalculations函数在开头用完全相同的名称声明了一堆局部变量。它还具有完全相同名称的参数。

我假设在ImplementCalculations开始时你不希望声明 new 变量,而是设置类中的变量的值:

void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage) {
    //Initialize overtime variables
    overtime_hours=0;
    overtime_pay=0;
    overtime_extra=0;
    basepay = 0;
    iIndividualSalary = 0;
    ... (other code here) ...

还要考虑将参数变量重命名为p_EmployeeName和p_hours(等),这样它们就不会与你班级中的成员变量名冲突。

答案 1 :(得分:1)

虽然不是100%清楚你的问题是什么,但是很可能可能,原因是你没有使用引用

例如,使用此功能:

void Addsomethingup (EmployeeClass Employee1);

这会创建Employee1副本,当该函数离开时会被丢弃。传递给函数的原始对象不会被触及并保持不变。

你可以试试这个:

void Addsomethingup (EmployeeClass &Employee1);

如果您不打算修改对象,也可以传递const引用作为无用复制的替代方法:

void Addsomethingup (EmployeeClass const &Employee1);

事实上,你应该为你的std::string论点这样做(尽管C ++ 11可能会在这里改变游戏规则,但你最好向谷歌咨询,否则它就变得太偏离主题了这个问题)。你可能需要阅读一本好的C ++书。引用是一种非常基本的语言功能。

如果您来自Java背景,请不要对“引用”一词感到困惑。 Java中的“引用”更像是C ++中的指针,而C ++引用没有Java对应项。