2

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
  • If you separate the calculations from the output, reference parameters start to make more sense. Commented Mar 19, 2015 at 23:45

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

I'm a beginner, so not quite understand what you saying. What should I need to do in my code to fix it?
You don't need to fix it. An alternative is to use double income(double year).
I'm totally lost now. So, this double income(double hours, double payrate) is pass-by-value and double income(const double &year) is pass-by-reference. But there will be also no changes if I do double income(double year). These are what I think I understood from my knowledge. Then how do const and double &year do to make its become a reference parameter?
The syntax double & declares the parameter as being passed by reference (direct access). The const double & declares that the parameter will be directly accessed but not modified. The key here is "not modified". Since the value is not modified, a copy can be passed. Since the double data type can fit into a processor register, the compiler can pass the copy in a register instead of passing a pointer to the value and dereferencing the pointer for direct access.
@ThomasMatthews Since he said he's a beginner I don't think you should continue the explanation with registers. How about: Pass by value if it's a bult-in type, pass by reference if it will be modified, and pass by const ref if it's a user-defined class and doesn't need to be modified.
1

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.

2 Comments

I'm understand the first paragraph that you mentioned. So what make double income(double hours, double payrate) become a value parameter then?
The fact that you do not lead your variable name with an ampersand. That's a very high-level way of thinking of it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.