Thread: Incrementing/Decrementing Object's hitpoints using classes

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    25

    Incrementing/Decrementing Object's hitpoints using classes

    I can't seem to increment/decrement an object's hitpoints in a game. For example...

    Code:
    #include <iostream>
    
    class Player
    {
    public:
         int getHitPoints() const { return HitPoints; }
         void setHitPoints(int hit) { HitPoints = hit; }
    private:
         int HitPoints;
    };
    
    int main()
    {
         using namespace std;
         
         Player Bob;
         Bob.setHitPoints(10);
    
         cout << "Bob gets hit!" << Bob.getHitPoints() - 2 << endl;
    
         return 0;
    }
    Any help is appreciated.
    My work:
    http://phil.webula.net

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What is the problem?

    Kuphryn

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    This
    Code:
    Bob.getHitPoints() - 2
    never actually assigns to anything the value of Bob.getHitPoints() - 2. SOmething along the lines of
    Code:
    Bob.setHitPoints(Bob.getHitPoints() - 2);
    would, however, work. On the other hand, you may wish to simply have a function that will decrement the hitpoints for you.

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    25
    Can you post an example?

    I tried this, but when I checked his new health status, it went back up to 10.

    Code:
    cout << "Bob gets hit!" << (Bob.getHitPoints() - 2) << endl; // 8 
    cout << "Bob checks his health!" << Bob.getHitPoints() <<   endl;
                  // 10
    Last edited by Darkman; 01-10-2004 at 07:08 PM.
    My work:
    http://phil.webula.net

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Code:
    (Bob.getHitPoints() - 2)
    Isn't assigning anything to your class. It's calling the method getHitPoints and getting the value 10 or whatever and then decrementing 2 from that and displaying it. To actually change it you need to call the method for setting the hitpoints in your class. As it has already been stated, something like

    Code:
    Bob.setHitPoints(Bob.getHitPoints() - 2);
    You could also make setHitPoints return an int that is the new health value if you wanted to.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    195
    You have to make a seperate class function like void decrementhp(int amt) to subtract or add hitpoints. You can't actually subtract from the function itself.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by KneeGrow
    You have to make a seperate class function like void decrementhp(int amt) to subtract or add hitpoints. You can't actually subtract from the function itself.
    But you can... See the examples that Mr Wizard and I posted.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    195
    Originally posted by Zach L.
    But you can... See the examples that Mr Wizard and I posted.
    lol my bad

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Having things like GetHP() and SetHP() is not really a very good OOP way of doing things. Your way offers almost no benefit over a struct; you aren't hiding the data or improving access to it, you're only obfuscating access to it.

    A good way for your class is to do this:

    1) Have a constructor which sets the character's max HP, and sets the current HP to max.

    2) Have two functions -- Heal() and Harm(). Heal() increments the HP, Harm() decrements. Both of them do bounds checking (i.e. you can never get above max HP or below 0)

    3) Have an isDead() function that tells if the current HP is positive.

    4) Have a HP() function to get the current HP, but do NOT allow a direct set of HP.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  2. static classes vs objects
    By earnshaw in forum C# Programming
    Replies: 5
    Last Post: 02-08-2006, 03:19 PM
  3. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 11:01 PM
  4. Mixing objects from different classes?
    By teedee46 in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2002, 10:28 AM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM