Help with classes

could someone please direct me to a good place for me to work on classes? i am working on a very simple text based game and i put in an inventory system. using if statements. i know its a bad way to do it. but thats how i knew how to so i did it and it works. but i want to work on doing this the "Proper" way. now i know it will probably still be far from good but just an improvement from what i have.

i know some basics on classes but dont know how to apply to what i want to use them for.

thanks for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void PrimaryHand()
{
     if(PrimaryHandItem == 0)
     {
          PrimaryHandName = "No Item";
          PrimaryHandHP = 0;

          PrimaryHandATT = 0;
          PrimaryHandDEF = 0;
     }
     else if(PrimaryHandItem == 1)
     {
          PrimaryHandName = "Sword";
          
          PrimaryHandHP = 10;
          PrimaryHandATT = 2;
          PrimaryHandDEF = 1;
     }
}
This is a
simple idea


Eyenrique-MacBook-Pro:Desktop Eyenrique$ ./Items 
Sword
HP: 10
att: 2
def: 1

Bow
HP: 9
att: 2
def: 0

Ender pearl
HP: 5
att: 0
def: 1


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
//Items.cpp
//##

#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;



class Item{
	public:
		int id;
		string item_name;
		int hp;
		int att;
		int def;
};//end class Item


int main(){

	const int SIZE=3;
	Item inventory[SIZE]; 

	inventory[0].id=0; 
	inventory[1].id=1;
	inventory[2].id=2;

	for(int i=0;i<SIZE;i++){
		switch(inventory[i].id){
			case 0:
				inventory[i].item_name="Sword";
				inventory[i].hp=10;
				inventory[i].att=2;
				inventory[i].def=1;	
			break;
			case 1:
				inventory[i].item_name="Bow";
                                inventory[i].hp=9;
                                inventory[i].att=2;
                                inventory[i].def=0;

			break;
			case 2:
				inventory[i].item_name="Ender pearl";
                                inventory[i].hp=5;
                                inventory[i].att=0;
                                inventory[i].def=1;

			break;

		}//end switch
	}//end for

	//print your inventory
	for(int i=0;i<SIZE;i++){
		cout<<inventory[i].item_name<<'\n'
			<<"HP: "<<inventory[i].hp<<'\n'
			<<"att: "<<inventory[i].att<<'\n'
			<<"def: "<<inventory[i].def<<'\n'<<endl;
	}//end for

return 0; //indicates success
}//end main 
ok. thanks but a lot of that is more advanced then what i am capable of. what it looks like to me is pretty much replace the: "if(PrimaryHandItem == 0)" with the "case 0;"

would that be a true statement or not?

and the other part i am not familiar with is the
for(int i=0;i<SIZE;i++)
i guess its fairly straight forward to me it looks like an if but it only happens if parameters are true. im not sure to be honest though.
well first off,
switch and for loop are
basic statements i
think that you should
learn more about
control structures;
(conditional and
repetition)

switch its a multiple-selection
statement that is helpful in
some cases instead of
if or if-else;

for -for loop- is
a repetition statement that
specifies the number of
iterations in a single
line of code.


Best regards.

To be honest, I don't think that's the greatest example of using classes to find a data-driven solution, as opposed to a hardcoded one.
As OP has already noticed, it's not really any different from his original code (
what it looks like to me is pretty much replace the: "if(PrimaryHandItem == 0)" with the "case 0;"
).

If you want, I can write a quick example that would make more sense in my eyes, however, you may not understand it.
i would appreciate it if you could. the more different ways i can see it done the better. right?
Topic archived. No new replies allowed.