Jump to content
  • Advertisement
Sign in to follow this  
MattBrox

A Question About C++ Classes

This topic is 2484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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);
    ...
}
Edited by vreality

Share this post


Link to post
Share on other sites
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);


}

Share this post


Link to post
Share on other sites

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".

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!