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.