Classes

Hi !

I have a question about a couple of lines in this code.

I was just wondering what exactly is the purpose of lines 39-46? Why do we "redefine" (sorry if that's not what it's actually called, I can't for the life of me remember :S ) the terms?

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
89
90
91
92
93
94
95
96
97
98
#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
line 16 is the declaraion of that method, lines 40 - 46 is the definition.

http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration
Thank you very much!
Topic archived. No new replies allowed.