通过价值传递和通过参考传递 [英] Pass by Value and Pass by Reference

查看:75
本文介绍了通过价值传递和通过参考传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个项目,该项目通过使用超载为员工计算收入,并且还按值传递+按引用传递.我需要使用程序中的至少一个函数来演示传递值;以及至少一个用于演示带有引用参数的引用传递的功能.这是到目前为止我得到的:

I'm working on a project that calculate income for employees by using overloading and also pass by value + pass by reference. I need to use at least one of the functions in program to demonstrate pass-by-value; and at least one of the functions to demonstrate pass-by-reference with reference arguments. Here is what I got so far:

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

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: " << grosspay << endl;

    federaltax = grosspay * .10;
    cout << "Federal Tax is: " << federaltax << endl;

    statetax = grosspay * .05;
    cout << "State Tax is: " << statetax << endl;

    totaltax = federaltax + statetax;
    cout << "Total tax is: " << totaltax << endl;

    netpay = grosspay - totaltax;
    return (netpay);

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

    grosspay = year / 52;
    cout << "Grosspay weekly is: " << grosspay << endl;

    federaltax = grosspay * .10;
    cout << "Federal Tax is: " << federaltax << endl;

    statetax = grosspay * .05;
    cout << "State Tax is: " << statetax << endl;

    totaltax = federaltax + statetax;
    cout << "Total Tax is: " << totaltax << endl;

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

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: " << ID << endl;
            cout << setprecision(2) << fixed;
            cout << "The net pay for hourly employee: " << income(hours, payrate) << endl;
            break;
        case 2:
            cout << "Enter employee ID: " << endl;
            cin >> ID;
            cout << "Enter salary: " << endl;
            cin >> year;

            cout << "Employee ID: " << ID << endl;
            cout << setprecision(2) << fixed;
            cout << "The net pay for salaried employee: " << income(year) << endl;
            break;
        case 3:
            cout << "Exited program" << endl;
            break;
        default:
            cout << "Please try again!" << endl;
            break;
        }
    }
}

这里的一位天才告诉我,我需要输入双倍收入(const double& year)作为通过引用.但是我不太确定到底有什么区别!我仍然有相同的输出.谁能帮我吗?

One of the genius here told me that I need to put double income(const double &year) for the pass-by-reference. But I'm not really sure what makes the difference! I still have the same output. Can anyone please help me?

推荐答案

以下是一些准则:

  • 如果该值适合处理器的寄存器,则按值传递该参数将不会被修改.
  • 如果函数将修改参数,则按引用传递.
  • 如果对象大于对象,则通过常量引用传递处理器的寄存器和参数将不会被修改.

通过常量引用传递诸如 double,float,int,char, bool 之类的类型没有意义,因为它们通常适合处理器的字长.编译器将尝试将这些值传递到寄存器中.这些POD类型没有额外的复制成本.因此,按值传递(需要较少的键入).

Passing by constant reference for types like double, float, int, char, and bool doesn't make sense because these usually fit into the processor's word size. The compiler will try to pass these values in a register. These POD types have no additional copying costs. Thus, pass by value (less typing required).

这篇关于通过价值传递和通过参考传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆