Pass by Value and Pass by Reference

萝らか妹 提交于 2019-12-20 03:12:33

问题


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;
        }
    }
}

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?


回答1:


Here are some guidelines:

  • Pass by value if the value can fit into the processor's registers and the parameter will not be modified.
  • Pass by reference if the parameter will be modified by the function.
  • Pass by constant reference if the object is larger than the processor's register and the parameter will not be modified.

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).




回答2:


When you write the ampersand in the function signature, you are specifying that you are accepting a reference to the argument (in the call statement). In your function double income(const double &year) you are passing by reference because year is a reference to year.

When you pass by value, a copy of the argument is created. When you pass by reference, a copy is not made, and your variable now has the address of the argument. So if you make a change to year in the function (assuming that it is not const for demonstrative purposes), your value in determineGross will also be changed, since the value at that address was changed.



来源:https://stackoverflow.com/questions/29156958/pass-by-value-and-pass-by-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!