Sign in to follow this  
Just Chris

Member functions not returing the right value

Recommended Posts

I'm trying to strengthen my OOP skills and the Java class I'm taking has really helped me with thinking in terms of objects. It was so easy in Java, though. The syntax in C++ is a bit different and this probably has caused the following error in my part. I'm doing a simple RPG battle simulation, and what I'm trying to do is return the HP of both opponents before and after the battle. The following code I'm using:
#include <iostream>
//#include <cstdlib>
using namespace std;

class Being {
  protected:
    int HP, MP, Strength, Defense, Magic, Resistance, Speed, Agility, Luck;
  public:
    int getHP();
    int getSpeed();
    int getDefense();
    int getStrength();
    void attack(Being&);
    void defend(int);
};

int Being::getHP(){
    //cout << "One's HP is" << endl;
    return HP;
}

int Being::getSpeed(){
    return Speed;
}

int Being::getStrength(){
    return Strength;
}

int Being::getDefense(){
    return Defense;
}

void Being::attack(Being &enemy){
    enemy.defend(Strength-enemy.getDefense());
}

void Being::defend(int damage){
    HP=HP-damage; //calculating the damage without newHP
}

class goodGuy: public Being{    //Inheritance to change the constructor of the goodguy
  public:                       //without also changing the badguy's
      goodGuy();
};

goodGuy::goodGuy(){  
  int HP = 40;
  int MP = 0;
  int Strength = 6;
  int Defense = 4;
  int Magic = 0;
  int Resistance = 3;
  int Speed = 1;
  int Agility = 2;
  int Luck = 3;
}

class badGuy: public Being{ 
  public:
      badGuy();
};

badGuy::badGuy(){        
  int HP = 10;
  int MP = 0;
  int Strength = 5;
  int Defense = 2;
  int Magic = 0;
  int Resistance = 1;
  int Speed = 2;
  int Agility = 1;
  int Luck = 0;
}

class Battle{
int being1Health, being2Health, being1Strength;
public:
    void startBattle(Being, Being);
};

void Battle::startBattle(Being One, Being Two){
  being1Health=One.getHP();
  being2Health=Two.getHP();
  being1Strength=One.getStrength(); // debug function, just to see if I have problems returning other values
     cout << "Jacob's strength: " << being1Strength << endl;
     cout << "Starting HP for Jacob: " << being1Health << endl;
     cout << "Starting HP for Blob: " << being2Health << endl;
  while(being1Health>0 && being2Health>0){
     if(One.getSpeed()>=Two.getSpeed()){
        One.attack(Two);
        Two.attack(One);
     }
     else{
        Two.attack(One);
        One.attack(Two);
     }
     being1Health=One.getHP();
     being2Health=Two.getHP();
   }
     cout << "Final HP for Jacob: " << being1Health << endl;
     cout << "Final HP for Blob: " << being2Health << endl;
}

int main(int argc, char *argv[]){
    Battle fight;
    goodGuy Jacob;
    badGuy Blob;
    fight.startBattle(Jacob, Blob);
    system("PAUSE");
    return EXIT_SUCCESS;
}
The output I get:
Jacob's strength: 8
Starting HP for Jacob: 2009252814
Starting HP for Blob: -1
Final HP for Jacob: 2009252814
Final HP for Blob: -1

Now, it looks like Jacob's strength has went up by 2, probably because it was added to the defense value of Blob. But why are the HP values totally off?

Share this post


Link to post
Share on other sites
In the constructors of badGuy and goodGuy, you're assigning the hitpoints etc. to local variables, not the member variables. You should remove the types in there, e.g.:

goodGuy::goodGuy(){
HP = 40;
MP = 0;
Strength = 6;
Defense = 4;
Magic = 0;
Resistance = 3;
Speed = 1;
Agility = 2;
Luck = 3;
}


Even better would be to use so-called initializer lists, which would make it look like:

goodGuy::goodGuy()
: HP(40)
, MP(0)
, Strength(6)
, Defense(4)
, Magic(0)
, Resistance(3)
, Speed(1)
, Agility(2)
, Luck(3)
{
}



Share this post


Link to post
Share on other sites
That's it, it seems to work well now!

Guess I should've seen that sooner...I was declaring variable types that were already declared under the Being class.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this