#include "stdafx.h"
#include
#include
using namespace std;
class Champion
{public:
Champion(int id, string nm, int hp,int mn, int dmg) {
ID = id;
name = nm;
HP = hp;
mana = mn;
damage = dmg;
}
void sttack(Champion &chmp) {
chmp.takeDamage(this->damage);
}
void takeDamage(int incomingDmg)
{
HP -= incomingDmg;
}
int getHP() {
return HP;
}
private:
int ID;
string name;
int HP;
int mana;
int damage;
};
int main()
{
Champion galen(1, "Galen", 800, 100, 10);
Champion ash (2, "Ash", 700.150, 7);
cout << "Ash的初始血量:" << ash.getHP() << endl;
galen.sttack(ash);
cout << "Ash受到Galen攻击后的血量:" << ash.getHP() << endl;
return 0;
}
为什么我给构造函数传递的第一个实掺会出现错误?
还有this->damage这个语句是什么意思!教材上没有介绍它是从哪里提取的damage?
700.150,应该是700,150啊
this->damage this就表示当前实例,所以damage就是你这个对象的成员变量damage
你的构造函数是5个参数,你的700.150应该是700,150把,把点改成逗号