A Question About C++ Classes

Started by
6 comments, last by MattBrox 12 years, 1 month ago
Hi all,

I've recently finished reading my first book on C++, and have one question concerning classes and inheritence.

I have three classes: creature, monster and player. Bother monster and player inherit from creature. The creature class has the attack() method, which is meant to make either player or monster attack another class that inherits from creature. Right now, this is what I have:

class creature
{
public:
virtual void attack(creature target);
int health;
int damage;
string name;
void sethealth(int x) {health = x;};
};

void creature::attack(creature target)
{
target.sethealth(target.health-damage);
cout << target.name << " now has " << target.health << "health. " << endl;
}


However, both target.name and target.health display nothing but rubbish, and I assume the health isn't really changing either. I can't seem to find any specific way of dealing with this neither online nor in my book. Any help would be appreciated.

EDIT: I thought of a better way to phrase my question: How can I have objects interact with other objects, through inheritence or another way?

Thanks,
Matt.
Advertisement
Did you set the initial health after creating creature?
Sounds like uninitialized variable to me.

Also, as written, the object "target", is passed "by value" to the function, "creature::attack". That means the object inside the function is a new object with the same "value" as the object passed. In other words, it's a copy. So you are correct - changes made to that copy don't affect the object which was originally passed to the function.

One way to make the function work with the actual object passed, rather than making a copy to work with, is to make the function call pass the object "by reference", like this:


void creature::attack(creature & target) {...}

And the function declaration needs the same change.


class creature
{
  public:
    virtual void attack(creature & target);
    ...
}
This works fine for me:

#include <iostream>
using namespace std;

class creature
{
public:
virtual void attack(creature target);
int health;
int damage;
string name;
void sethealth(int x) {health = x;};
};
void creature::attack(creature target)
{
target.sethealth(target.health-damage);
cout << target.name << " now has " << target.health << " health. " << endl;
}

int main (){

creature player;
creature enemy;

player.name = "Nick";
enemy.name = "Bokoblin";


player.sethealth(100);
enemy.sethealth(100);

cin >> player.damage;

player.attack(enemy);


}

Did you set the initial health after creating creature?
Sounds like uninitialized variable to me.


I double checked; this is not the problem.


This works fine for me:

#include <iostream>
using namespace std;

class creature
{
public:
virtual void attack(creature target);
int health;
int damage;
string name;
void sethealth(int x) {health = x;};
};
void creature::attack(creature target)
{
target.sethealth(target.health-damage);
cout << target.name << " now has " << target.health << " health. " << endl;
}

int main (){

creature player;
creature enemy;

player.name = "Nick";
enemy.name = "Bokoblin";


player.sethealth(100);
enemy.sethealth(100);

cin >> player.damage;

player.attack(enemy);


}



This is where I'm stuck. I don't want to create 2 creatures named player and enemy, rather I want to create two classes named player and enemy that inherit from the base-class "creature".
Aha thats easy:

class Creature
{
public:
virtual void attack(creature &target)=0;
int health;
int damage;
string name;
void sethealth(int x) {health = x;}
};

class Enemy : public Creature
{
public:
void attack(creature &target){/*do stuff*/}
};
class Player: public Creature
{
public:
void attack(creature &target){/*do stuff*/}
};


Hope it helped

This is where I'm stuck. I don't want to create 2 creatures named player and enemy, rather I want to create two classes named player and enemy that inherit from the base-class "creature".


where's the problem?


class Player : public Creature
{
public:
};
class Enemy : public Creature
{
};
Enemy enemy;
Player player;
player.attack(enemy);

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

This should work.

Now Enemy and Player class both have everything that class Creature has (health, name etc).
Thanks for the help, everyone. I got it all working. :)

This topic is closed to new replies.

Advertisement