CLASS CityData Create the .h and.cpp files required to realize a class called cityData. The class will identify A vector
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
CLASS CityData Create the .h and.cpp files required to realize a class called cityData. The class will identify A vector
This is City.h
#define __CITY_H
#ifndef __CITY_H 123
#include<iostream>
#include<iomanip>
using namespace std;
class City
{
private :
string name,country;
long population;
public :
string getName();
long getPopulation();
string getCountry();
City(string name,string country,long p);
bool operator>(City& arg);
};
#endif
This is City.cpp
#include<iostream>
#include"City.h"
#include<iomanip>
using namespace std;
bool City :: operator>(City& arg){
if(this->population>arg.getPopulation()) return true;
else return false;
}
ostream & operator<<(ostream& arg1,City&
arg2)
{
arg1<<setw(20)<<arg2.getName()<<setw(15)<<arg2.getCountry()<<"
"<<arg2.getPopulation()<<endl;
return arg1;
}
string City :: getName()
{
return name;
}
long City ::getPopulation()
{return this->population;}
string City ::getCountry()
{return this->country;}
City :: City(string name,string country,long p)
{
this->name=name;
this->country=country;
this->population=p;
}
CLASS CityData Create the .h and.cpp files required to realize a class called cityData. The class will identify A vector of city objects called cities Declare (in the.h) and define (in the .cpp) get and set functions. Your constructor will use one argument, a string. The string will be the filename of the file that will contain the city data. Your constructor will contain code to open the file and read in the data. Each line of the file contains city name followed by a tab followed by the country name followed by another tab followed by the city's population. The capture below shows you a section of the file (and as you can see the cities are NOT sorted according to population) shaoguan China 2997600 Kuwait City Kuwait 2989000 Shanwei China 2993600 Minneapolis United States 2977172 Kyiv Ukraine 2963199 Take advantage of those tab characters and use std::string function like find() and substr() (as we have done before) to break up each line in the three parts. Tip: to convert a string to a long you can use a function called stol with prototype: long std::stol(string) You must also create the following: Create a function called citiesInCountry which will be used to output a listing of all the cities that are in a country. The function must accept a string argument. The string will be the name of the country to look for. Create a function called fiveLargest Cities() which will be used to output the five largest (in population) cities in the vector of cities. Create a function called fiveSmallest Cities() which will be used to output the five smallest (in population) cities in the vector of cities.