用c++帮我写一个三国武将类类型
时间: 2023-08-28 12:51:22 浏览: 56
当然!以下是一个用C++编写的简化版三国武将类类型的示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Hero {
private:
string name;
string kingdom;
int level;
string weapon;
public:
Hero(string n, string k, int l, string w) {
name = n;
kingdom = k;
level = l;
weapon = w;
}
string getName() {
return name;
}
string getKingdom() {
return kingdom;
}
int getLevel() {
return level;
}
string getWeapon() {
return weapon;
}
void displayInfo() {
cout << "姓名:" << name << endl;
cout << "国家:" << kingdom << endl;
cout << "等级:" << level << endl;
cout << "武器:" << weapon << endl;
}
};
int main() {
Hero hero1("刘备", "蜀国", 5, "双股剑");
Hero hero2("曹操", "魏国", 6, "青釭剑");
Hero hero3("孙权", "吴国", 4, "长枪");
hero1.displayInfo();
cout << endl;
hero2.displayInfo();
cout << endl;
hero3.displayInfo();
return 0;
}
```
这个示例代码定义了一个`Hero`类,包含了武将的姓名、国家、等级和武器。类中有构造函数用于初始化对象的属性,以及一些成员函数用于获取属性值和显示武将信息。在`main`函数中创建了三个武将对象,并调用`displayInfo`函数显示每个武将的信息。
请注意,这只是一个简单的示例,您可以根据需要扩展和修改类的属性和方法。