代码:
#include <iostream>
using namespace std;
int blood=30000;
class Stu1
{
protected:
string name; //英雄
int hp; //血量
public:
Stu1(){} //无参构造
Stu1(string n ,int h): name(n) , hp(h){} //有参构造
virtual ~Stu1(){} //析构函数
virtual void show()=0;
};
class Stu2:public Stu1
{
protected:
int atk;
public:
Stu2(){}
Stu2(string n,int h,int a):Stu1(n,h),atk(a){}
virtual ~Stu2(){}
void show()
{
blood=blood-50-atk;
cout<<"英雄名字:"<<name<<endl;
cout<<"血量:"<<hp<<endl;
cout<<"攻击力:"<<atk<<endl;
cout<<"对水晶造成上海:"<<50+atk<<endl;
cout<<"水晶剩余血量为:"<<blood<<endl;
}
};
class Stu3 : public Stu1
{
protected:
int pun; //惩戒伤害
public:
Stu3() {}
Stu3(string n,int h,int p):Stu1(n,h),pun(p) {}
virtual ~Stu3() {}
void show()
{
blood = blood - 220 - pun;
cout<<"登场英雄:"<<name<<endl;
cout<<"基础伤害:220"<<endl;
cout<<"惩戒伤害:"<<pun<<endl;
cout<<"对水晶伤害为:"<<220+pun<<endl;
cout<<"水晶剩余血量:"<<blood<<endl;
}
};
void fun(Stu1 &h)
{
h.show();
}
int main()
{
Stu2 t("疾风剑豪",8760,1100);
Stu3 a("无极剑圣易",4620,900);
fun(t);
cout<<"***************************************"<<endl;
fun(a);
return 0;
}