搞不明白应该怎样用这同一个类函数让他们互斗造成伤害,盼高手指点。

如下所示,我设了小白和小黑两个人的攻防值以及一个攻击类函数

int WhiteStr = 10;
int BlackStr = 8;
int WhiteDef = 4;
int BlackDef = 5;
class Attack
{
public:
unsigned int Damage()
{
cout<<"造成"<<Str()-Def()<<"点伤害"<<endl;
}
private:
int Str(), Def();
protected:
};

眼眸繁星
浏览 95回答 3
3回答

心有法竹

将类的攻击攻击接口改为publicclass Attack{public:int Str(), Def();unsigned int Damage(){cout<<"造成"<<Str()-Def()<<"点伤害"<<endl;}unsigned int Damage( Attack role){cout<<"造成"<<Str()-role.Def()<<"点伤害"<<endl;}private:protected:};Attack role_black;attack role_white;role_black(role_white);//black 对white 的伤害&nbsp;

饮歌长啸

最简单的,可以写在一个诠释WhiteStr = 10;&nbsp;诠释BlackStr = 8;&nbsp;诠释WhiteDef = 4;&nbsp;诠释BlackDef = 5;&nbsp;类攻击{的攻击(STR,INT高清)m_str(STR),m_def的的(DEF){} / /构造函数,在进攻防守传入unsigned int类型伤害(){法院<<“”<< STR()的高清()<<“点的伤害”<< endl; 0;&nbsp;}&nbsp;私人诠释STR()/ /函数编写完整{返回m_str;&nbsp;} 诠释投篮不中()/ /函数写{的回报m_def;&nbsp;}&nbsp;诠释m_str; / /攻击诠释m_def / /国防保护:};&nbsp;廉政的main(){攻击白(WhiteStr,WhiteDef),黑色(BlackStr,BlackDef);&nbsp;white.Damage();&nbsp;black.Damage();&nbsp;返回0;&nbsp;}&nbsp;

慕运维8079593

最简单可以写成这样int WhiteStr = 10;int BlackStr = 8;int WhiteDef = 4;int BlackDef = 5;class Attack{public:Attack(int str, int def) : m_str(str), m_def(def){} // 构造函数,将攻防传入unsigned int Damage(){cout<<"造成"<<Str()-Def()<<"点伤害"<<endl;return 0;}private:int Str() // 将函数写全{return m_str;}int Def() // 将函数写全{return m_def;}int m_str; // 攻击int m_def; // 防御protected:};int main(){Attack white(WhiteStr, WhiteDef), black(BlackStr, BlackDef);white.Damage();black.Damage();return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP