Question: c + + Write the InputTowns ( ) function in the SmallTowns class. Within InputTowns ( ) , use cin to read pairs of input,

c++
Write the InputTowns() function in the SmallTowns class. Within InputTowns(), use cin to read pairs of input, string currName and
integer currPopulation, until "done" is read from input. Create each Town object with currName and currPopulation as arguments
and append each object to vector townList.
Ex: If the input is Sundance 1200 Goshen 3600 Opal 4500 Bow 1500 done, then the output is:
Town: Sundance, Population: 1200
Town: Goshen, Population: 3600
Town: Opal, Population: 4500
Town: Bow, Population: 1500Write the InputTowns() function in the SmallTowns class. Within InputTowns(), use cin to read pairs of input, string currName and integer currPopulation, until "done" is read from input. Create each Town object with currName and currPopulation as arguments and append each object to vector townList.
Ex: If the input is Sundance 1200 Goshen 3600 Opal 4500 Bow 1500 done, then the output is:
Town: Sundance, Population: 1200
Town: Goshen, Population: 3600
Town: Opal, Population: 4500
Town: Bow, Population: 1500 #include
#include
using namespace std;
class Town {
public:
void SetNameAndPopulation(string newName, int newPopulation);
void Print() const;
private:
string name;
int population;
};
void Town::SetNameAndPopulation(string newName, int newPopulation){
name = newName;
population = newPopulation;
}
void Town::Print() const {
cout "Town: " name ", Population: " population endl;
}
class SmallTowns {
public:
void InputTowns();
void PrintTowns();
private:
vector townList;
};
/* Your code goes here */
void SmallTowns::PrintTowns(){
Town currTown;
unsigned int i;
for (i =0; i townList.size(); ++i){
currTown = townList.at(i);
currTown.Print();
}
}
int main(){
SmallTowns smallTowns;
smallTowns.InputTowns();
smallTowns.PrintTowns();
return 0;
}
c + + Write the InputTowns ( ) function in the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!