Bear with me here, i have been struggling to grasp the concept of classes and pointers, I understand what they do but writing it out has been tough for me... (I apologize if i am not phrasing this question right, correct me if im wrong please.)
SO, I have been trying to figure out a way to say "Hey! you have 20 HP left, you got hurt and now you have 15 HP left!"
This is what i came up with, did i approach this problem in the correct way?
By convention a GetName() method should only retrieve the value of your name member. SetName() would be a possible name for changing the value of name.
You've combined what is recommended to be done as separate methods into one.
#include <iostream>
#include <string>
class Player
{
public:
// you should create at least one constructor
Player() : myName(""), myHP(0) {}
// another ctor could specify name and HP on object construction
Player(std::string aName, int hp)
{
myName = aName;
myHP = hp;
}
std::string GetName() const
{
return myName;
}
// you don't need a destructor, recommended to create one anyway
// you might need it later as the class evolves and expands
~Player() {}
void SetName(std::string aName)
{
myName = aName;
}
int GetHP() const
{
return myHP;
}
void SetHP(int hp)
{
myHP = hp;
}
void GetsHurt()
{
myHP -= 5;
}
private:
std::string myName;
int myHP;
};
int main()
{
Player Peter("Peter", 25);
std::cout << "This player is named " << Peter.GetName() << '\n';
std::cout << '\n' << Peter.GetName() << " has " << Peter.GetHP()
<< " hitpoints.\n" << "He got hurt";
Peter.GetsHurt();
std::cout << " and has " << Peter.GetHP() << " left!\n";
}
This player is named Peter
Peter has 25 hitpoints.
He got hurt and has 20 left!
Thank you guys for the replies, It helps for me to compare what i came up with to someone who is experienced. Even if i dont understand what im reading haha, makes me learn more things!