php網(wǎng)站開發(fā)需要什么軟件友情鏈接獲取的途徑有哪些
- 整理思維導(dǎo)圖
- 復(fù)習(xí)課上代碼
- 全局變量,int monster = 10000;定義英雄類hero,受保護的屬性string name,int hp,int attck;公有的無參構(gòu)造,有參構(gòu)造,虛成員函數(shù) void Atk(){blood-=0;},法師類繼承自英雄類,私有屬性 int ap_atk=50;重寫虛成員函數(shù)void Atk(){blood-=(attck+ap_atk);};射手類繼承自英雄類,私有屬性 int ac_atk = 100;重寫虛成員函數(shù)void Atk(){blood-=(attck+ac_atk);}實例化類對象,判斷怪物何時被殺死。(能寫多少寫多少)
??
#include <iostream>
using namespace std;
// 全局變量,代表怪物的血量
int blood = 10000;class Hero
{
protected:// 英雄的名字string name;// 英雄的血量int hp;// 英雄的攻擊力int attck;public:// 無參構(gòu)造函數(shù)Hero() {}// 有參構(gòu)造函數(shù)Hero(string n, int h, int a) : name(n), hp(h), attck(a) {}// 虛函數(shù),用于扣減敵人的血量virtual void Atk() { blood -= 0; }
};class Mage : public Hero
{
private:// 法師的額外攻擊力int ap_atk = 50;public:Mage(string n, int h, int a):Hero(n,h,a) {}// 重寫父類的虛函數(shù)void Atk() override { blood -= (attck + ap_atk); }
};class Archer : public Hero
{
private:// 射手的額外攻擊力int ac_atk = 100;public:Archer(string n, int h, int a):Hero(n,h,a){}// 重寫父類的虛函數(shù)void Atk() override { blood -= (attck + ac_atk); }
};int main() {// 創(chuàng)建法師對象Mage mage1("Alice", 100, 100);// 創(chuàng)建射手對象Archer archer1("Bob", 100, 100);// 攻擊怪物,直到怪物的血量降為 0int s=0;while (blood > 0){mage1.Atk();if(blood>0){archer1.Atk();}s++;}// 輸出殺死怪物信息cout <<s<<"秒殺死怪物"<<endl;return 0;
}