Question
Your task for this lab is to create a program that provides a class called Country . This class can be initiated by the country
Your task for this lab is to create a program that provides a class called Country. This class can be initiated by the country name and number of cities. The cities are added to the country by city name and number of population. Every city can hold a specific number for the population. If more people try to migrate, a City will discard the overflow population. A Country can add a new city and allow people to migrate there.
The Country then can be displayed with the name of the cities and their population.
Implementation
Implement this program in two modules (i.e., classes): Country and City
The City Class
Develop this class module in two files named City.h and City.cpp.
First, define the following constant in the City header:
const int POPULATION = 20000; //max population of a city
The City class encapsulates a City using its name and its population using the following member variables (attributes) and member functions (methods):
Private member variables(attributes)
The class has the following private member variables:
char m_cityName[31];// holds the city name up to 30 chars int m_population;//number of people. A city can have zero population.
Public member functions(methods)
A City can be created in two different ways:
No argument constructor (default)
City();
By default a City is instantiated by setting all the member variables to default values. You can do this by setting the first character of m_cityName to an empty Cstring and m_population to a value like -1.
Two argument constructor
City(const char* name,int population);
A City can also be created by the city name and the number of population.
If name is not nullptr and not empty also if population is greater than or equal to zero (a city can have 0 population) then it will set the received arguments to the corresponding member variables. Otherwise, set the values to a safe empty state.
void setEmpty();
Sets a City to a safe empty state by setting m_cityName to an empty string and m_population to -1.
void setCityName(const char* name);
If name is not nullptr then it will copy the Cstring pointed by the name argument into m_cityName attribute up to 30 characters. Otherwise, set it to an empty string.
void setPopulation(int population);
If the population value is less than the constant POPULATION then it will set m_population attribute to the value. Otherwise, it will set it to -1
void addPopulation(int population);
This function will add the newly migrated population to a city up to the constant POPULATION limit. If the received value is less than POPULATION then it will add the value to that city. If after adding, m_population exceeds value of the constant POPULATION then it will set the m_population to POPULATION instead, discarding the population overflow.
bool isEmpty()const;
Returns true if m_cityName is empty or the m_population is less than 0.
int getPeople()const;
Returns m_population
void display()const;
- If City object is valid a) prints "|" b) prints m_cityName in 30 spaces ,left justified. c) prints " | " d) prints m_population in 10 spaces, right justified. e) prints " | " f) new line
- otherwise prints, cout << "Invalid city" << endl;
The Country Class
Develop this module in two files named Country.h and Country.cpp. Place your class definition in Country.h and your function definitions in Country.cpp. Create a class named Country and the class should have the following member variables (attributes) and member functions (method): First, define the following constant in the Country header:
const int MAX_CITY = 20; //maximum number of cities
Private member variables(attributes)
The class should have the following private member variables:
char* m_name;//points to a dynamically allocated Cstring holding the Country name int m_noOfCity;//size of the dynamically allocated array of cities City* m_city=nullptr;//pointer to the dynamically allocated array of cities
Public member functions(methods)
A Country can be created in two different ways:
No argument constructor (default)
Country();
- By default, a Country is initiated by setting all the member variable values to default values. You can do this by setting m_name, m_city to nullptr and m_noOfCity to a recognizable value like 0.
Three argument constructor
Country(const char* name, int noOfCity,const City* cities);
A Country can also be created by a name, the number of cities and adding a list of cities:
- First, it will set the attributes to default values (So if the argument validation fails, the object will be in a safe empty state)
- Next, if all of the arguments are valid, it will set m_name and m_noOfCity to the corresponding argument values. The number of cities cannot be more than MAX_CITY and less than zero.
- Then dynamically allocate an array of cities pointed by m_city member variable. The length of this array will be m_noOfCity.
- Lastly, add all the cities to the dynamically allocated array of m_city.
void setName(const char* name);
Sets the name of the Country. First, it will delete the old resources of the m_name. Then if the incoming name is not nullptr and not empty it will allocate memory to the length of the name argument (+1 for null character) and copy the name into the newly allocated memory. Otherwise, it will deallocate the m_city array and set the object to the safe empty state.
Country& addCity(const City& c);
If the incoming City reference is not empty, this method will add the city to the m_city array of cities, otherwise, the function is exited and no action will be taken.
To accommodate the new city you have to resize the memory of the m_city.
- create a temporary pointer of type City and dynamically allocate an array of cities with the length of m_noOfCity + 1.
- then copy the existing values into temp.
- delete the resources of m_city
- add the new received city to the temp array and increase the value of m_noOfCity.
- assign the temp to m_city
In all cases when the function ends, return the current object.
Country& migrate(int people);
This function will receive the number of people who want to migrate to the list of cities m_city. It will loop through all the cities one by one. If a city`s population is less than the POPULATION constant value then this function will add the received people to that city. Otherwise, it will do nothing. lastly, return the current object.
void setEmpty();
Sets the Country object to an Empty State. Do this by setting the m_name and m_city to nullptr and m_noOfCity to 0.
bool isEmpty()const;
Returns true if m_name is nullptr;
~Country()
Deallocate the memory allocated by m_name and m_city.
void display()const;
If the Country object is not empty this function will print the output in the following format: (For more details see the sample output)
- prints "Country Name: "
- prints the name and then goes to new line
- prints "No of city: "
- printf the number of cities and then goes to new line
- prints "City name" with width 32, left justified.
- prints "Population"
- goes to new line
- will loop through the m_city elements, printing each of them.
otherwise prints, cout<< "Invalid country object" << endl;
Tester program:
// Workshop #4: // Version: 0.9 // Date: 2021/05/01 // Author: Nargis Khan // Description: // This file tests the lab section of your workshop // Revision: Fardad Soleimanloo // Date: 2021/10/01 /////////////////////////////////////////////////// #include Execution sample: LAB Submission (part 1)Files to submit:Displaying invalid Country objects 1. Invalid country object 2. Invalid country object 3. Invalid country object ------------------------------------------------------ Displaying valid Country object with valid cities Country Name: Flower No of city: 3 City name Population |Lily | 15000 | |Tulip | 8000 | |Daisy | 500 | ------------------------------------------------------- Displaying invalid City objects 1. invalid city 2. invalid city 3. invalid city ------------------------------------------------------ Migrating 10K people to the Flower country cities Country Name: Flower No of city: 3 City name Population |Lily | 20000 | |Tulip | 18000 | |Daisy | 10500 | ------------------------------------------------------- Adding a new city to the country and migrating new people there Country Name: Flower No of city: 4 City name Population |Lily | 20000 | |Tulip | 20000 | |Daisy | 18500 | |Rose | 8000 |
Country.cpp Country.h City.cpp City.h CountryTester.cpp
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started