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 22:04:20 浏览: 78
C04_KEY_INT.rar_codewarrior hc08_keyboard codewarrior_飞思卡尔
头文件 dragon.h:
``` c++
#ifndef DRAGON_H
#define DRAGON_H
#include <string>
#include "warrior.h"
class dragon : public warrior {
private:
int weapon;
double morale;
int strength_re;
public:
dragon(int n, int i, std::string name_temp);
void input_strength_re(int s);
double get_morale();
std::string get_weapon();
};
#endif // DRAGON_H
```
源文件 dragon.cpp:
``` c++
#include "dragon.h"
dragon::dragon(int n, int i, std::string name_temp) : warrior(i, name_temp, n) {
strength_re = 0;
weapon = get_num() % 3;
morale = 0;
}
void dragon::input_strength_re(int s) {
strength_re = s;
morale = (double)strength_re / (double)get_strength();
}
double dragon::get_morale() {
return morale;
}
std::string dragon::get_weapon() {
if (weapon == 0) {
return "sword";
} else if (weapon == 1) {
return "bomb";
} else {
return "arrow";
}
}
```
其中,头文件中使用了宏定义和预处理指令来避免头文件被多次包含,源文件中包含了头文件,并且实现了dragon类的各个成员函数。
阅读全文