class dragon :public warrior { private: int weapon; double morale; int strength_re; public: dragon(int n,int i,string name_temp):warrior(i,name_temp,n)//n:编号 name_temp:名字 i:生命元数量 { strength_re = 0; weapon = get_num() % 3; morale = 0; } void input_strength_re(int s) { strength_re = s; morale = (double)strength_re / (double)get_strength(); } double get_morale() { return morale; } string get_weapon() { if (weapon == 0) { return "sword"; } else if (weapon == 1) { return "bomb"; } else { return "arrow"; } } };分别存放在头文件和源文件中
时间: 2024-01-21 12:04:20 浏览: 201
这段代码是关于游戏“红与黑”的人物类“龙”的实现。它继承了“战士”类,并且增加了三个私有成员变量:武器类型(0表示剑,1表示炸弹,2表示箭),士气值,以及武器耐久度。其中,构造函数初始化了武器类型和士气值,并且调用了“战士”类的构造函数。input_strength_re方法用于设置士气值,get_morale方法用于获取士气值,get_weapon方法用于获取武器类型。这段代码被分别存放在头文件和源文件中,是为了方便其他程序调用和管理。
相关问题
class dragon :public warrior { private: int weapon; double morale; int strength_re; public: dragon(int n,int i,string name_temp):warrior(i,name_temp,n)//n:编号 name_temp:名字 i:生命元数量 { strength_re = 0; weapon = get_num() % 3; morale = 0; } void input_strength_re(int s) { strength_re = s; morale = (double)strength_re / (double)get_strength(); } double get_morale() { return morale; } string get_weapon() { if (weapon == 0) { return "sword"; } else if (weapon == 1) { return "bomb"; } else { return "arrow"; } } };分别存放在头文件和源文件中
这段代码是关于游戏“红与黑”的人物类“龙”的实现。它继承了“战士”类,并且增加了三个私有成员变量:武器类型(0表示剑,1表示炸弹,2表示箭),士气值,以及武器耐久度。其中,构造函数初始化了武器类型和士气值,并且调用了“战士”类的构造函数。input_strength_re方法用于设置士气值,get_morale方法用于获取士气值,get_weapon方法用于获取武器类型。这段代码被分别存放在头文件和源文件中,是为了方便其他程序调用和管理。
解释下这段代码class dragon : public warrior { private: int Nofweapon; double morale; //士气 public: dragon(int w, double m) :Nofweapon(w), morale(m) {} void reset(int w, double m) { Nofweapon = w; morale = m; } void print() { printf("It has a %s,and it's morale is %.2f\n", weapon[Nofweapon].c_str(), morale); } };
这段代码定义了一个名为 dragon 的类,它是从 warrior 类继承而来的,表示游戏中的一种士兵——龙。
dragon 类中有两个私有成员变量:Nofweapon 和 morale,分别表示龙的武器编号和士气值。
dragon 类中有一个公有构造函数,它接受两个参数:w 和 m,分别表示龙的武器编号和士气值,用于创建一个 dragon 对象。
dragon 类中有一个公有成员函数 reset,它接受两个参数:w 和 m,分别表示龙的武器编号和士气值,用于重置龙的武器和士气。
dragon 类中有一个公有成员函数 print,它用于打印龙的信息,包括龙的武器和士气值。
总的来说,这段代码定义了一个具有武器和士气值的龙类,它是 warrior 类的一个子类,并且可以被用于游戏中的士兵生产。
阅读全文
相关推荐

















