I need help with how objects interact.

So I'm pretty new to C++ and I'd love some help with this concept. I'm trying to create a small program. I have two classes: a Player_Character and Item. The Player_Character class has a integer (HP). I'd like the Player_Character to use an Item and that Item will affect the Player_Character's HP. The affect should change based on the Item. Let's say I have an instance of an Item: a potion which increases a Player_Character's HP by 3. Here's an example of what I have:

class Player_Character{
int m_hp;

Player_Character(int hp = 10);
void heal(int amount = 0);
void use_item(object thing); // dunno, if this is correct.
};

void Player_Character::heal(int amount){
m_hp += amount;
}

/* This is what I don't get */
void use_item(object thing){
// Here I need the thing to tell the Player_Character to use it's heal function with a parameter of 3.
}
/* The above is what I don't understand*/
}

class Item{
int m_strength;

Item(int strength = 0);
// The Item should be able to tell the class that uses it what to do with the item's m_strength.
};

int main(){
Player_Character hero(15);
Item potion(3);

hero.use_item(potion);
//hero.m_hp should increase by 3.
}


How to make the item tell the player_character to use it's heal function?
sir, use [code] tag
button <>
can't read your code (it's kinda notepad text and not C++ code)
In this case, it's player_character's responsibility to know how to use the item, right? In reality, a potion itself shouldn't need to know how it's used - instead, a potion would know things like its effectiveness.

Also, about your classes - if you don't put a public: on top of the sections you want to be able to access from the outside of the class. A class defaults to private, so you won't be able to access any of your classes' properties from main.
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
class Item; //you need to make Player_Character aware of Item's existence in the program

class Player_Character{
  int m_hp;
  void heal(int amount = 0); //heal can be private here, as outside the class we call use_item

 public: //you need to make your methods public
  Player_Character(int hp = 10);
  // void use_item(object thing); // dunno, if this is correct.
  void use_item(Item thing); //now it takes an Item
  int getHP(); //need a way to access the player's hp
};

//define Player_Character's constructor
Player::Character(int hp) {
  m_hp = hp;
}

void Player_Character::heal(int amount){
  m_hp += amount;
}

/* This is what I don't get */
//forgot to add the prefix "Player_Chatacter::"
void Player_Character::use_item(Item thing){
// Here I need the thing to tell the Player_Character to use it's heal function with a parameter of 3.
  heal( thing.getStrength() ); //call heal, and pass it the item's strength
}
/* The above is what I don't understand*/
//dont need this }

//define getHP method
int Player_Character::getHP() {
  return m_hp;
}


class Item{
  int m_strength;

 public: //make methods public
  Item(int strength = 0);
  int getStrength(); //need a way to access the item's strength
// The Item should be able to tell the class that uses it what to do with the item's m_strength.
};

//define Item's constructor
Item::Item(int strength) {
  m_strength = strength;
}

//define Item::getStrength method
int getStrength() {
  return m_strength;
}

#include <iostream> //added for testing

int main(){
Player_Character hero(15);
Item potion(3);

hero.use_item(potion);
//hero.m_hp should increase by 3.
//check!
std::cout << "The hero's HP is " << hero.getHP() << '\n';
}
Topic archived. No new replies allowed.