class iceman :public warrior { private: int weapon; public: iceman(int n, int i, string name_temp) :warrior(i, name_temp, n) { weapon = get_num() % 3; } string get_weapon() { if (weapon == 0) { return "sword"; } else if (weapon == 1) { return "bomb"; } else { return "arrow"; } } };请将这段代码分别存放到头文件和源文件中
时间: 2024-01-15 16:03:59 浏览: 103
C04_KEY_INT.rar_codewarrior hc08_keyboard codewarrior_飞思卡尔
头文件(iceman.h):
```c++
#ifndef ICEMAN_H
#define ICEMAN_H
#include "warrior.h"
class iceman : public warrior {
private:
int weapon;
public:
iceman(int n, int i, string name_temp) : warrior(i, name_temp, n) {
weapon = get_num() % 3;
}
string get_weapon() {
if (weapon == 0) {
return "sword";
}
else if (weapon == 1) {
return "bomb";
}
else {
return "arrow";
}
}
};
#endif // ICEMAN_H
```
源文件(iceman.cpp):
```c++
#include "iceman.h"
// 空文件,无需实现 iceman 类的方法
```
在需要使用 iceman 类的源文件中,只需要包含 iceman.h 文件即可,例如:
```c++
#include "iceman.h"
int main() {
iceman* p = new iceman(1, 2, "iceman");
string weapon = p->get_weapon();
delete p;
return 0;
}
```
阅读全文