Passing Objects to Functions

Passing Objects to Functions
Passing Objects to Functions
CONCEPT

 Class objects may be passed as arguments to functions.

EXPLANATION

As we know how to use variables as function arguments. Class objects can also be passed as arguments to functions. For example, the following function has a parameter that receives a Rectangle object.
void displayRectangle(Rectangle r)
{
cout << "Length = " << r.getLength() << endl;
cout << "Width = " << r.getWidth() << endl;
cout << "Area = " << r.getArea() << endl;
}

The following lines of code create a Rectangle object with length 15 and width 10, and
then pass it to the displayRectangle function.

Rectangle box(15, 10);
displayRectangle(box);


Assuming the Rectangle class includes the member functions used in this example, the
displayRectangle function will output the following information:
Length = 15
Width = 10
Area = 150
As with regular variables, objects can be passed to functions by value or by reference. In
the Rectangle example, box is passed to the displayRectangle function by value. This means that displayRectangle receives a copy of box. If displayRectangle called any Rectangle class mutator functions, they would only change the copy of box, not the original. If a function needs to store or change data in an object’s member variables, the object must be passed to it by reference.

PROGRAM

1 // This program passes an object to a function. It passes it
2 // to one function by reference and to another by value.
3 #include <iostream>
4 #include <iomanip>
5 #include <string>
6 using namespace std;
7
8 class InventoryItem
9 {
10 private:
11 int partNum; // Part number
12 string description; // Item description
13 int onHand; // Units on hand
14 double price; // Unit price
15
16 public:
17
18 void storeInfo(int p, string d, int oH, double cost); // Prototype
19
20 int getPartNum()
21 {
22  return partNum; }
23 string getDescription()
24 { return description; }
25
26 int getOnHand()
27 { return onHand; }
28
29 double getPrice()
30 { return price; }
31 };
32
33 // Implementation code for InventoryItem class function storeInfo
34 void InventoryItem::storeInfo(int p, string d, int oH, double cost)
35 { partNum = p;
36 description = d;
37 onHand = oH;
38 price = cost;
39 }
40
41 // Function prototypes for client program
42 void storeValues(InventoryItem&); // Receives an object by reference
43 void showValues (InventoryItem); // Receives an object by value
44
45 //**************** main ******************
46 int main()
47 {
48 InventoryItem part; // part is an InventoryItem object
49
50 storeValues(part);
51 showValues(part);
52 return 0;
53 }
54
55 /**********************************************************
56 * storeValues *
57 * This function stores user input data in the members of *
58 * an InventoryItem object passed to it by reference. *
59 * ********************************************************/
60 void storeValues(InventoryItem &item)
61 {
62 int partNum; // Local variables to hold user input
63 string description;
64 int qty;
65 double price;
66
67 // Get the data from the user
68 cout << "Enter data for the new part number \n";
69 cout << "Part number: ";
70 cin >> partNum;
71 cout << "Description: ";
72 cin.get(); // Move past the '\n' left in the
73 // input buffer by the last input
74 getline(cin, description);
75 cout << "Quantity on hand: ";
76 cin >> qty;
77 cout << "Unit price: ";
78 cin >> price;
79
80 // Store the data in the InventoryItem object
81 item.storeInfo(partNum, description, qty, price);
82 }
83
84 /********************************************************
85 * showValues *
86 * This function displays the member data stored in the *
87 * InventoryItem object passed to it by value. *
88 ********************************************************/
89 void showValues(InventoryItem item)
90 {
91 cout << fixed << showpoint << setprecision(2) << endl;;
92 cout << "Part Number : " << item.getPartNum() << endl;
93 cout << "Description : " << item.getDescription() << endl;
94 cout << "Units On Hand: " << item.getOnHand() << endl;
95 cout << "Price : $" << item.getPrice() << endl;
96 }
Program Output
Output with Example Input Shown in Bold
Enter data for the new part number
Part number: 175[Enter]
Description: Hammer[Enter]
Quantity on hand: 12[Enter]
Unit price: 7.49[Enter]


Part Number : 175
Description : Hammer
Units On Hand: 12
Price : $7.49
Previous
Next Post »