C ++中的重载错误[通过值传递并通过引用传递]

时间:2015-03-18 19:47:01

标签: c++ overloading pass-by-reference pass-by-value

我正在研究一个项目,该项目使用重载计算两种类型员工(薪水和小时)的收入。这个项目有3个文件,这里是主要来源:

#include <iostream>
#include "Grosspay.h"
using namespace std;

void Grosspay::determineGrosspay()
{
    cout << "Enter 1 - Calculate payroll for hourly employee" << endl;
    cout << "Enter 2 - Calculate payroll for salary employee" << endl;
    cout << "Enter 3 - Exit" << endl;

    cout << "Federal Tax is 10% of Grosspay" << endl;
    cout << "State Tax is 5% of Grosspay" << endl;
    while (choice != 3)
    {
        cout << "\nEnter your choice: " << endl;
        cin >> choice;
        switch (choice)
        {
        case 1:
            cout << "Enter employee ID: " << endl;
            cin >> ID;
            cout << "Enter hours: " << endl;
            cin >> hours;
            cout << "Enter payrate: " << endl;
            cin >> payrate;

            cout << "Employee ID: %d" << ID << endl;
            cout << "The net pay for hourly employee: %.2f\n" << income(hours, payrate) << endl;
            break;
        case 2:
            cout << "Enter employee ID: " << endl;
            cin >> ID;
            cout << "Enter salary: " << endl;
            cin >> year;

            cout << "The net pay for salaried employee: %.2f\n" << income(year) << endl;
            break;
        case 3:
            cout << "Exited program" << endl;
            break;
        default:
            cout << "Please try again!" << endl;
            break;
        }
    }
}
double income(double hours, double payrate)
{
    double grosspay = 0;
    double federaltax = .10;
    double statetax = .05;
    double totaltax;
    double netpay;
    if (hours <= 40)
    {
        grosspay = payrate * hours;
    }if (hours > 40 && hours <= 50)
    {
        grosspay = (payrate * 40) + ((hours - 40) * payrate * 1.5);
    }
    if (hours > 50)
    {
        grosspay = (payrate * 40) + (10 * payrate * 1.5) + ((hours - 50) * payrate * 2);
    }
    cout << "Grosspay weekly is %.2f\n" << grosspay << endl;

    federaltax = grosspay * .10;
    cout << "Federal Tax is: %.2f\n" << federaltax << endl;

    statetax = grosspay * .05;
    cout << "State Tax is: %.2f\n" << statetax << endl;

    totaltax = federaltax + statetax;
    cout << "Total tax is: %.2f\n" << totaltax << endl;

    netpay = grosspay - totaltax;
    return (netpay);

}
double income(double year)
{
    double grosspay;
    double federaltax = .10;
    double statetax = .05;
    double totaltax;
    double netpay;

    grosspay = year / 52;
    cout << "Grosspay weekly is %.2f\n" << grosspay << endl;

    federaltax = grosspay * .10;
    cout << "Federal Tax is: %.2f\n" << federaltax << endl;

    statetax = grosspay * .05;
    cout << "State Tax is: %.2f\n" << statetax << endl;

    totaltax = federaltax + statetax;
    cout << "Total Tax is: %.2f\n" << totaltax << endl;

    netpay = grosspay - totaltax;
    return (netpay);
}

我不知道为什么该计划无法找到收入 来自

cout << "The net pay for hourly employee: %.2f\n" << income(hours, payrate) << endl;
cout << "The net pay for salaried employee: %.2f\n" << income(year) << endl;

另外,我需要使用程序中的至少一个函数来演示按值传递;至少是用参考参数演示pass-by-reference的函数之一。我不知道如何把它放在源头。有人请帮忙吗?

1 个答案:

答案 0 :(得分:0)

由于income类中未定义Grosspay函数,因此应在调用之前定义它们。尝试在income方法之前移动determineGrosspay的定义。

另一种解决方案是使用function prototype通过在determineGrosspay函数之前移动原型并在之后实现它。


由于您在评论中询问了按引用传递和传递值,因此我将其包含在此答案中。您为income函数编写的内容是按值传递。这意味着将一个或多个变量的副本传递给该函数。

通过引用传递意味着您将变量的引用传递给函数而不是它的副本。通过引用传递假定您的应用程序具有以下函数签名:income(const double &year)如果您不想修改函数中的年份变量,income(double &year)如果您想修改year的值


double income(const double &year)用于您的应用程序。

请研究这意味着什么,因为它是该语言的重要组成部分。