64,282
社区成员
发帖
与我相关
我的任务
分享
#include<string>
#include<iostream>
#include<vector>
using namespace std;
//将领
class CGeneral {
private:
string name;
public:
CGeneral( string name ){
this->name = name;
}
string getName() {
return this->name;
}
};
//城池
class CTown {
public:
CTown( string name ){
this->name = name;
}
string getName(){
return this->name;
}
int getGeneralCount() {
return this->generals.size();
}
bool append_general( CGeneral general ) {
this->generals.push_back( general );
return true;
}
private:
string name;
//城池中居住的将领
vector<CGeneral> generals;
};
//场景
class CScene {
private:
//场景中的城池
vector<CTown> towns;
public:
bool append_town( CTown town ){
this->towns.push_back( town );
return true;
}
int count_town() {
return this->towns.size();
}
CTown at(int index) {
return this->towns.at(index);
}
};
//创建场景
int main() {
CGeneral m_guanyu = CGeneral("关羽");
CScene scene;
CTown *town_xinye = new CTown("新野");
town_xinye->append_general( m_guanyu );
scene.append_town( *town_xinye );
CTown town_xuchang = CTown("许昌");
scene.append_town( town_xuchang );
CTown town_jianye = CTown("建业");
scene.append_town( town_jianye );
cout<<"场景中目前有城池数:"<<scene.count_town()<<"\n"<<endl;
string msg;
for(int i=0; i<scene.count_town();i++) {
CTown t = scene.at(i);
cout<<"第"<<i+1<<"城池名称是:"<<t.getName()<<"\t"<<"居住的将领人数:"<<t.getGeneralCount()<<endl;
}
return 0;
}