要求:
声明一个英雄类,成员属性:英雄名字、血量,定义成员函数攻击水晶(纯虚函数)
声明一个坦克类,继承自英雄类,新增属性:攻击力,重写攻击水晶函数,水晶下降血量为基础伤害(自定义)+坦克英雄攻击力声明一个刺客类,继承自英雄类,新增属性:惩戒,重写攻击水晶函数,水晶下降血量为基础伤害(自定义)+惩戒量
定义全局函数fun,传入不同的英雄类对象,函数体内调用攻击水晶函数。
主测试文件中,完成相关函数的测试功能
代码实现过程:
#include <iostream>
#include<iomanip>
using namespace std;
class Hero{
protected:
string name;
double HP;
int basic = 50;
public:
Hero(){}
Hero(string n,double h):name(n),HP(h){}
virtual ~Hero(){}
virtual void crystal_attack(double & crystal_hp) = 0;
};
class Tank:public Hero{
protected:
double strength;
public:
Tank(){}
Tank(string n,double h,double s):Hero(n,h),strength(s){}
virtual ~Tank(){}
void crystal_attack(double & crystal_hp) override{
crystal_hp -= (strength+basic);
cout << left << setw(20) << name << "攻击了敌方水晶 , "<< "水晶血量 : " << crystal_hp << endl;;
}
};
class Assassin:public Hero{
protected:
double punish;
public:
Assassin(){}
Assassin(string n,double h,double p):Hero(n,h),punish(p){}
virtual ~Assassin(){}
void crystal_attack(double & crystal_hp) override{
crystal_hp -= (punish+basic);
cout << left << setw(20) << name << "攻击了敌方水晶 , "<< "水晶血量 : " << crystal_hp << endl;;
}
};
void fun(Hero & h){
static double crystal_hp = 15000;
h.crystal_attack(crystal_hp);
}
int main()
{
Tank t("聪明的墨菲特",4000,100);
Assassin a("超模",2000,400);
Hero & h1 = t;
Hero & h2 = a;
fun(h1);
fun(h2);
return 0;
}
实现结果:
