This program reads the number of towns (townCount) and then iterates through each pair, creating a Town object for each pair and adding it to the townList.
If the population of a town falls within the specified range, it calls the Print() method for that town. Note that you may need to adjust the code based on the specific requirements or format of your input data.
Here's a simple C++ program that achieves this:
#include <iostream>
#include <vector>
#include <string>
class Town {
public:
Town(const std::string& name, int population) : name(name), population(population) {}
void Print() const {
std::cout << "Town: " << name << ", Population: " << population << std::endl;
}
int getPopulation() const {
return population;
}
private:
std::string name;
int population;
};
int main() {
int townCount;
std::cin >> townCount;
std::vector<Town> townList;
for (int i = 0; i < townCount; ++i) {
std::string name;
int population;
std::cin >> name >> population;
Town town(name, population);
townList.push_back(town);
if (population >= 2394 && population <= 3803) {
town.Print();
}
}
return 0;
}