我有这个命名Enemy
的类,它Ninja
继承了它的属性。在Ninja
的攻击函数中,我正在尝试调用Ninja
的getAttackPower
函数,但是我该如何获取它。我试过打电话this.getAttackPower
,但没有用。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Enemy {
int attackPower;
public:
virtual void attack() { cout << "I am attacking you!"; }
protected:
int getAttackPower() {
return attackPower;
}
void setAttackPower( int a ) {
attackPower = a;
}
};
class Ninja : public Enemy {
void attack() {
cout << "(minus " << getAttackPower() << " points).";
// .................. right here ......
}
};
int main() {
Ninja ninjaObj;
ninjaObj.setAttackPower(23);
ninjaObj.getAttackPower();
}
以下是我得到的错误:
void Enemy::setAttackPower(int) is protected
error: within this context
error: 'int Enemy::getAttackPower()' is protected
error: within this context