0

Base class:

class Entity {

public:

    // friend
    // e1 attacker, e2 target
    friend void Attack( Entity &e1, Entity &e2 );

    // methods set attributes
    virtual void setDamage ( int x ) { this -> Damage = x; }
    virtual void setHealthPoints ( int x) { this -> HealthPoints = x; }

    // methods return attributes
    virtual int getDamage ( ) { return Damage ; }
    virtual int getHealthPoints ( ) { return HealthPoints; }

protected:

    // Attributes
    int Damage;
    int HealthPoints;

};

Derived class:

class Ninja: public Entity{

public:

    // Ninja constructor
    Ninja ( int x, int y )
    : Damage ( x ), HealthPoints ( y )

};

Edit: Hey, i keep getting "class 'Ninja' does not have any field named 'Damage" on the code mentioned before, also the HealthPoints(sorry, this sentence got removed after posting the question)

i tryied pointing Damage and HealthPoints( * ,this,&) but i'm getting " expected identifier before '*' token. The only way it works is initializing them normally, but what if i want to have const Damage?

  • 2
    `Damage` is a member of the `Entity` class so the only place you can initialise it is in an `Entity` class constructor. Add a constructor for your `Entity` class and invoke that from your `Ninja` class constructor. – john May 22 '20 at 19:55
  • You can initialize `const` members just fine in a member initializer list. But it has to be done by the base class constructor. – cigien May 22 '20 at 19:57
  • Entity needs a constructor, and constructors need curly braces {} – QuentinUK May 22 '20 at 19:59

0 Answers0