基类:
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;
};
派生类:
class Ninja: public Entity{
public:
// Ninja constructor
Ninja ( int x, int y )
: Damage ( x ), HealthPoints ( y )
};
编辑:嘿,我一直在前面提到的代码上收到“类'Ninja'没有任何名为'Damage”的字段,还有HealthPoints(对不起,这句话在发布问题后被删除)
我尝试指向 Damage 和 HealthPoints( * ,this,&) 但我在 '*' 标记之前得到了“预期的标识符。它工作的唯一方法是正常初始化它们,但是如果我想要 const Damage 怎么办?