c++ class problem

I'm working on a program using classes and seem to have hit a brick wall.
Here is an example program based on my problem.
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class CItem
{
public:
	string Name;
	int Price;
};

CItem Nothing = { " ", -1 };	//Used when the person has dropped an item

class CPerson
{
public:
	void PrintItems()
	{
		cout << "-------Items-------" << endl;
		for(int i=0;i<ItemCount;++i)
			cout << Items[i].Name << endl;
		cout << "-----End Items-----" << endl;
	}
	void Pay(int amount)	//send a negative amount to pay
				//or a positive amount to get paid
	{
		Money += amount;
	}
	void AddItem(CItem item)	//Puts an item at the end of your list
	{
		Items[ItemCount] = item;
		++ItemCount;
	}
	void DropItem(int item)		//drop the item at index item
					//then push each item under that up
					//and finally add a Nothing at the end
	{
		do
		{
			Items[item] = Items[item+1];
			item++;
		} while(item < 8);
		Items[8] = Nothing;
		--ItemCount;
	}
	string Name;
	int Money;
	int ItemCount;
	CItem Items[9];
};

class CStore
{
public:
	bool MainMenu(CPerson& customer)
	{
		cout << "We deal in datatypes and bytes." << endl
			<< "Dost thou wish to buy anything today (y/n)?" << endl;
		char c;
		cin >> c;
		if(c == 'y')
		{
			while(ItemMenu(customer));
		}
		cout << "Please, come again." << endl;
		return false;
	}
	bool ItemMenu(CPerson& customer)
	{
		cout << "What dost thou wish to buy?" << endl
			<< "$" << customer.Money << endl;
		for(int i=0;i<ItemCount;++i)
		{
			cout << i << ") $" << Items[i].Price
				<< "\t\t\t" << Items[i].Name << endl;
		}
		cout << "i) Items" << endl
			<< "q) Quit" << endl;
		char c;
		cin >> c;
		if('0' <= c && c <= (char)'0'+ItemCount-1 && c != 'q')
		{
				int i = (int)(c - '0');	//ugly workaround

			if(customer.Money >= Items[i].Price)
			{
				if(customer.ItemCount < 9)
				{
				   cout << "The " << Items[i].Name << "?" << endl
						<< "Is that okay (y/n)?" << endl;
					char d;
					cin >> d;
					if(d == 'y')
					{
						customer.Pay(-Items[i].Price);
						cout << "You pay $" << Items[i].Price << endl;
						customer.AddItem(Items[i]);
						cout << "You bought the " << Items[i].Name << endl;
						
					}
				}
				else cout << "You cannot hold anymore." << endl;
			}
			else cout << "Sorry. Thou hast not enough money." << endl;
			return true;
		}
		else if(c == 'i') customer.PrintItems();
		else return false;
	}
	CPerson Cashier;
	int Money;
	int ItemCount;		//How many unique items. store doesn't run out.
	CItem Items[4];
};

int main()
{
	CPerson myPerson = { "Michael Toy", 300, 0, {} };
	
	CStore myStore = {
		{ "Dennis Ritchie", 0, 0, {} },	//doesn't do anything
		9001,
		4,				//only 4 items sold
		{
			{ "int", 32 },		//store items & prices
			{ "float", 32},
			{ "char", 8},
			{ "bool", 8}
		}
	};
	
	myStore.MainMenu(myPerson);
}

The code initializes a person and a store and automatically has
the menu displayed.
After the person has bought something and
returned to the main menu all their data
(money, itemcount, items[]) is reset...
I'm guessing I don't fully understand how classes work.

edit: changed member functions to reference arguments (CPerson& customer)
Last edited on
You're passing the object by value, not by reference, which makes a copy of the object, so the changes made to it are not reflected in the original.
...omg i feel dumb, I actually saw this just now and came to close my topic.
Thanks for the reply though.
You're using your classes a lot like structs:
* You don't have constructors.
* You've made all of the data members in all classes public.
That misses two of the main purposes of classes: automatic initialization and data hiding / encapsulation.

Also, you should move the implementation code out of the class declarations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Obj {
public:
  Obj (int, int, int);
  void func ();
private:
  int a;
  int b;
  int c;
};

Obj::Obj (int a_, int b_, int c_) : a(a_), b(b_), c(c_) { }

void Obj::func () { }

int main () {
  Obj obj (1, 2, 3);
}

Topic archived. No new replies allowed.