Receives object by reference???

Hello !

I am having a problem understanding this program. Mainly the purpose of line 41" void storeValues(InventoryItem&); // Receives an object by reference" . I'm not sure what they mean by "receives an object by reference". What is the difference between this and the line below it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 // This program passes an object to a function. It passes it
 // to one function by reference and to another by value.
 #include <iostream>
 #include <iomanip>
 #include <string>
 using namespace std;

 class InventoryItem
 {
 private:
 int partNum; // Part number
 string description; // Item description
 int onHand; // Units on hand
 double price; // Unit price

 public:

 void storeInfo(int p, string d, int oH, double cost); // Prototype

 int getPartNum()
 { return partNum; }

 string getDescription()
 { return description; }

 int getOnHand()
 { return onHand; }

 double getPrice()
 { return price; }

 // Implementation code for InventoryItem class function storeInfo
 void InventoryItem::storeInfo(int p, string d, int oH, double cost)
 { partNum = p;
 description = d;
 onHand = oH;
 price = cost;
 }

 // Function prototypes for client program
 void storeValues(InventoryItem&); // Receives an object by reference
 void showValues (InventoryItem); // Receives an object by value


 int main()
 {
 InventoryItem part; // part is an InventoryItem object

 storeValues(part);
 showValues(part);
 system("pause");
 return 0;
 }


 void storeValues(InventoryItem &item)
 {
 int partNum; // Local variables to hold user input
 string description;
 int qty;
 double price;

 // Get the data from the user
 cout << "Enter data for the new part number \n";
 cout << "Part number: ";
 cin >> partNum;
 cout << "Description: ";
 cin.get(); // Move past the '\n' left in the
 // input buffer by the last input
 getline(cin, description);
 cout << "Quantity on hand: ";
 cin >> qty;
 cout << "Unit price: ";
 cin >> price;

 // Store the data in the InventoryItem object
 item.storeInfo(partNum, description, qty, price);
 }


 void showValues(InventoryItem item)
 {
 cout << fixed << showpoint << setprecision(2) << endl;;
 cout << "Part Number : " << item.getPartNum() << endl;
 cout << "Description : " << item.getDescription() << endl;
 cout << "Units On Hand: " << item.getOnHand() << endl;
 cout << "Price : $" << item.getPrice() << endl;
 }
Last edited on
By reference means that you pass the original object. By value means that you pass a copy of the original object.

What's the difference? Modifying a variable which was "passed by reference" will modify the variable in the calling function. It can be treated as a second output from a function. Another advantage is that it doesn't copy data when the function is called which can save a lot of time for big containers.

Hopefully, this demonstrates the difference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int plus1A(int i)
{
  i++;
  return i;
}
int plus1B(int& i)
{
  i++;
  return i;
}

int main()
{
  int a = 1;
  int b = plus1A(a);

  cout << "a = " << a << " b = " << b << endl;

  int c = 1;
  int d = plus1B(c);

  cout << "c = " << c << " d = " << d << endl;
}
a = 1 b = 2
c = 2 d = 2


Last edited on
Ohh, I see! So it...saves it in a way so it doesn't disappear when you go into main. Okay, I think I get it, thank you very much ! :)
Topic archived. No new replies allowed.