通过调用成员函数print_house_details写一个查找中国各省名称和人数的c++程序,其中包含河北省7420万人,河南省9872万人,云南省4693万人,四川省8400万人,江苏省8515万人,山东省10200万人,湖北省5844万人并且运行结果时可以输入省名查找人数
时间: 2023-11-28 15:05:04 浏览: 12
很抱歉,您的问题我已经回答过了,以下是实现上述功能的C++程序:
```c++
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Province {
public:
Province() {}
Province(const string& name, int population) : name_(name), population_(population) {}
void print_house_details() const {
cout << "Province: " << name_ << ", Population: " << population_ << " million" << endl;
}
int get_population() const {
return population_;
}
private:
string name_;
int population_;
};
int main() {
map<string, Province> provinces;
provinces["Hebei"] = Province("Hebei", 7420);
provinces["Henan"] = Province("Henan", 9872);
provinces["Yunnan"] = Province("Yunnan", 4693);
provinces["Sichuan"] = Province("Sichuan", 8400);
provinces["Jiangsu"] = Province("Jiangsu", 8515);
provinces["Shandong"] = Province("Shandong", 10200);
provinces["Hubei"] = Province("Hubei", 5844);
string query;
cout << "Enter a province name to find its population: ";
cin >> query;
if (provinces.count(query) > 0) {
int population = provinces[query].get_population();
cout << "Population of " << query << " is " << population << " million" << endl;
} else {
cout << "Could not find population for " << query << endl;
}
return 0;
}
```
运行结果示例如下:
```
Enter a province name to find its population: Shandong
Population of Shandong is 10200 million
```
你可以输入不同的省名进行测试,程序会输出对应省的人口数量。
阅读全文