class iceman :public warrior { private: int weapon; public: ichttps://csdnimg.cn/release/searchv2-fe/img/icon-send.3681be21.svgeman(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-02-06 19:04:13 浏览: 161
头文件(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);
string get_weapon();
};
#endif // ICEMAN_H
```
源文件(iceman.cpp):
```c++
#include "iceman.h"
iceman::iceman(int n, int i, string name_temp) : warrior(i, name_temp, n) {
weapon = get_num() % 3;
}
string iceman::get_weapon() {
if (weapon == 0) {
return "sword";
}
else if (weapon == 1) {
return "bomb";
}
else {
return "arrow";
}
}
```
在需要使用 iceman 类的源文件中,需要同时包含 iceman.h 和 warrior.h 文件,并且编译器需要链接 iceman.cpp 和 warrior.cpp 两个源文件,例如:
```c++
#include "iceman.h"
#include "warrior.h"
int main() {
iceman* p = new iceman(1, 2, "iceman");
string weapon = p->get_weapon();
delete p;
return 0;
}
```
阅读全文
相关推荐



















