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.
};
/* 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?
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.
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_itempublic: //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 Itemint 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';
}