Adjust an objects value within a class?

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?

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
#include <iostream>
#include <string>
using namespace std;


class Player{
    private:
        string name;
        int hp;
    public:
        void GetHp(int hp){
            this-> hp = hp;
        }
        string GetName(string name){
            this-> name = name;
            return name;
        }
        void ShowHp(){
            cout << this-> name << "'s Hp is " << this -> hp << endl;
        }
        void GetHurt(){
            cout << this-> name << " got hurt!\n";
            this -> hp = this -> hp - 5;
        }
};


int main(){
    Player Josh;
    Player Curie;
    Josh.GetName("Josh");
    Josh.GetHp(20);
    Josh.ShowHp();
    Josh.GetHurt();
    Josh.ShowHp();
    Curie.GetName("Curie");
    Curie.GetHp(30);
    Curie.ShowHp();
    Curie.GetHurt();
    Curie.ShowHp();
    return 0;
}
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
#include <iostream>
#include <string>

struct player {

    explicit player( const std::string& name, int hp ) : name_(name), hp_( hp > 0 ? hp : 0 ) {}

    std::string name() const { return name_ ; }

    int hp() const { return hp_ ; }
    int hp( int v ) { return hp_ = v > 0 ? v : 0 ; }

    void print() const {

        std::cout << "player{ " << name() << ", hp: " << hp() << " }\n" ;
    }

    void hurt() {

        std::cout << "player " << name() << " had " << hp() << " hp left\n" ;
        hp( hp() - 5 ) ;
        std::cout << "\tgot hurt and now has only " << hp() << " hp left\n\n" ;


    }


    private:
        std::string name_ ;
        int hp_ ;
};

int main() {

    player josh( "Josh", 35 ) ;
    josh.print() ;

    player curie( "Curie", 60 ) ;
    curie.print() ;

    josh.hurt() ;
    for( int i = 0 ; i < 3 ; ++i ) curie.hurt() ;

    josh.print() ;
    curie.print() ;
}
closed account (E0p9LyTq)
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.

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
#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!


Last edited on
closed account (E0p9LyTq)
JLBorges, thank you for making me go look for why you used explicit with the ctor. I learned something very useful. :)

https://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-mean
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!
closed account (E0p9LyTq)
JLBorges is one of the real experts here for writing good code. My knowledge of what C++ can do has rather large gaps.
Last edited on
Topic archived. No new replies allowed.