Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We need a Country class to keep track of the countries and a few details about them. We will also need a class called CountryCatalogue

We need a Countryclass to keep track of the countries and a few details about them. We will also need a class called CountryCatalogueto keep track of the collection of Countries. Both these classes will have relevant attributes and behaviours (described below).

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

This description is part of a single program that I have to create and was having trouble with.

Class attributes will be the country name name), population Cpop), area _area), and continent Ccontinent). As for the methods, we will need 2 constructors and a deconstructor. One constructor will be a default one (provided), and another will be the not default one where the attributes are assigned to things we care about. We'll also have a deconstructor, but this will effectively be an empty function as we don't need to delete anything. To keep things simple, don't worry about making a copy constructor. This won't actually matter for the object we're making here as we'll get a sufficient one for free, but it's definitely good practice to always include one. Despite this, we'll skip it to streamline the assignment. Write getter methods for the country name, pop, area, and continent. Write another one to return the county's population density. This is not an attribute, so you'll have to calculate it on the fly (it's just population/area) Write a single setter method to set the population. Write a to _string() method to convert the country object into a string and return it. For the countries, we'll simply just return"name in continent". So, for example, "China in Asia". That's literally it. Test your class to make sure all the code you wrote is correct. To do this, just make a few instances in a main and try manipulating the object and see if it worked right. Be sure it's working perfectly before moving on. This class will be the tricky one. This class will keep track of all the countries we have. We will then use this class to manipulate our collection of countries, get information from them, etc The class attributes will be: . .catalogue - an army of countries or more accurately a pointer to a dynamicarmy of pointers to countries. maxSize - how big our array called_catalogue is currently . curSize - how many things are currently in our array called_catalogue countryContinent - a dictionary/map (same thing) to hold pairs of countries and continents The methods this class will have are described below A default constructor that will just set maxSize to 10, curSize to 0 (nothing in our catalogue yet), and make a dynamic array of size maxSize. I provided this for you. .A constructor that will setup our catalogue of countries based on some text files. This constructor will be fairly complex and you may find it to be one of the more challenging parts of this assignment. I have included some comments to help guide what to do This constructor will receive two strings as parameters that correspond to the names of files that contain the information the class needs to setup the catalogue. Your program must work in general with any possible files, but the ones we'll be using to test your code are provide and are named country cpp.txt and continent cpp.txt. Based on the contents of the continent file, the constructor will populate the countryContinent map with country (key) continent value) pairs. o o Map? If your memory is that bad, read these carefully again: lus.com/re https:/thispointer.com/stdmap-tutorial examples //stackoverflow.com/questions/26281979/c Based on the contents of the country file, the constructor will populate the catalogue. You will need to use the countryContinent here to get each country's continent. I also recommend making use of the addCountry method described below that you will implement to easily add the countries to the catalogue. The format of the files is this (slight change here from A4 in 161): o o Continent: Line 1 contains the country name, then that county's continent on line 2. Then line 3 is a new country name, and line 4 is it's continent, etc. Country: Line 1 contains a country name, followed by its population, followed by the area, then line 4 is a new country name, then population, then area, etc. o File I/O? ://www.cpl Get reading! One thing to take special note of is the .eof depending on how you want to code it. Also, be sure to close your files! function. You might find that handy " .We'll not making a copy constructor here. This is probably a bad move long term, but to simplify things a little, we'll skip it for Al .A deconstructor that will delete the contents of the catalogue, and then delete the catalogue itself. Remember, catalogue is a pointer to a group of pointers. We have to delete the inside pointers, then delete the array that held the pointers that is being pointed to by_catalogue. .A private method called expandCapacity that will just make a new array that's bigger if we run out of space in our catalogue. As a rule, we will always double the size every time. Watch out for memory leaks! addCountry is a method that will take all the information needed to make a country (name, pop, area, continent) as a parameter, make the object, and add it to the catalogue. If we run out of room in our array we might have to expand the capacity (see above). Whenever we add it, we must be sure to increment the counter keeping track of the number of things in our countryCatalogue. .RemoveCountry will take a name of a country as a parameter and delete the country with the matching name from the catalogue. If it does not exist, then the method will ultimately have no impact. Watch out for memory leaks and be sure to update the catalogue correctly findCountry will take a country name and return a pointer to the country with the matching name in the catalogue (assuming it exists). If it does not exist, return nullptr numCountriesInContinent will take a continent name, and then return how many countries from that continent. filterCountriesByContinent will take a continent name and the number of countries that exist on that continent and return a pointer to an array containing pointers to the countries on the continent. This one might be a little tricky, but it shouldn't be too bad. Just make a pointer to an array of pointers to countries of the proper size, then add the countries on the continent to the array. Return the pointer to the array in the end. If none exist, return nullptr. .printCountryCatalogue will just print out the contents of the catalogue. This one should be simple. Print out" name in_continent" for each country on a new line. To do this, just have a loop go over the _catalogue and call each country's to string) method and print it out. . setPopulationOfSelectedCountry will take a country name and a new population and thern update the corresponding country in the catalogue's population (assuming it exists) findCountryWithLargestPop will return a pointer to the country with the largest population within our catalogue. If nothing is in the catalogue, return nullptr findCountry WithSmallestAreawill return a pointer to the country with the smallest population within out catalogue. If nothing is in the catalogue, return nullptr findMostPopulousContinent will return the name of the continent with the largest population based on the countries within the catalogue. This one will be a little tricky saveCountryCatalogue will take the name of a file to create and write to which will save some details about the contents of our catalogue. The function will return the number of lines written to the file. Format the output as follows. o NAMEICONTINENTPOPULATIONIPOPULATION DENSITY o ChinalAsia 1339190000 139.5431469965489 Do not be surprised if your output formats the numbers funny. This is fine. See my included example output file named saved al example.txt. o File LO? Class attributes will be the country name name), population Cpop), area _area), and continent Ccontinent). As for the methods, we will need 2 constructors and a deconstructor. One constructor will be a default one (provided), and another will be the not default one where the attributes are assigned to things we care about. We'll also have a deconstructor, but this will effectively be an empty function as we don't need to delete anything. To keep things simple, don't worry about making a copy constructor. This won't actually matter for the object we're making here as we'll get a sufficient one for free, but it's definitely good practice to always include one. Despite this, we'll skip it to streamline the assignment. Write getter methods for the country name, pop, area, and continent. Write another one to return the county's population density. This is not an attribute, so you'll have to calculate it on the fly (it's just population/area) Write a single setter method to set the population. Write a to _string() method to convert the country object into a string and return it. For the countries, we'll simply just return"name in continent". So, for example, "China in Asia". That's literally it. Test your class to make sure all the code you wrote is correct. To do this, just make a few instances in a main and try manipulating the object and see if it worked right. Be sure it's working perfectly before moving on. This class will be the tricky one. This class will keep track of all the countries we have. We will then use this class to manipulate our collection of countries, get information from them, etc The class attributes will be: . .catalogue - an army of countries or more accurately a pointer to a dynamicarmy of pointers to countries. maxSize - how big our array called_catalogue is currently . curSize - how many things are currently in our array called_catalogue countryContinent - a dictionary/map (same thing) to hold pairs of countries and continents The methods this class will have are described below A default constructor that will just set maxSize to 10, curSize to 0 (nothing in our catalogue yet), and make a dynamic array of size maxSize. I provided this for you. .A constructor that will setup our catalogue of countries based on some text files. This constructor will be fairly complex and you may find it to be one of the more challenging parts of this assignment. I have included some comments to help guide what to do This constructor will receive two strings as parameters that correspond to the names of files that contain the information the class needs to setup the catalogue. Your program must work in general with any possible files, but the ones we'll be using to test your code are provide and are named country cpp.txt and continent cpp.txt. Based on the contents of the continent file, the constructor will populate the countryContinent map with country (key) continent value) pairs. o o Map? If your memory is that bad, read these carefully again: lus.com/re https:/thispointer.com/stdmap-tutorial examples //stackoverflow.com/questions/26281979/c Based on the contents of the country file, the constructor will populate the catalogue. You will need to use the countryContinent here to get each country's continent. I also recommend making use of the addCountry method described below that you will implement to easily add the countries to the catalogue. The format of the files is this (slight change here from A4 in 161): o o Continent: Line 1 contains the country name, then that county's continent on line 2. Then line 3 is a new country name, and line 4 is it's continent, etc. Country: Line 1 contains a country name, followed by its population, followed by the area, then line 4 is a new country name, then population, then area, etc. o File I/O? ://www.cpl Get reading! One thing to take special note of is the .eof depending on how you want to code it. Also, be sure to close your files! function. You might find that handy " .We'll not making a copy constructor here. This is probably a bad move long term, but to simplify things a little, we'll skip it for Al .A deconstructor that will delete the contents of the catalogue, and then delete the catalogue itself. Remember, catalogue is a pointer to a group of pointers. We have to delete the inside pointers, then delete the array that held the pointers that is being pointed to by_catalogue. .A private method called expandCapacity that will just make a new array that's bigger if we run out of space in our catalogue. As a rule, we will always double the size every time. Watch out for memory leaks! addCountry is a method that will take all the information needed to make a country (name, pop, area, continent) as a parameter, make the object, and add it to the catalogue. If we run out of room in our array we might have to expand the capacity (see above). Whenever we add it, we must be sure to increment the counter keeping track of the number of things in our countryCatalogue. .RemoveCountry will take a name of a country as a parameter and delete the country with the matching name from the catalogue. If it does not exist, then the method will ultimately have no impact. Watch out for memory leaks and be sure to update the catalogue correctly findCountry will take a country name and return a pointer to the country with the matching name in the catalogue (assuming it exists). If it does not exist, return nullptr numCountriesInContinent will take a continent name, and then return how many countries from that continent. filterCountriesByContinent will take a continent name and the number of countries that exist on that continent and return a pointer to an array containing pointers to the countries on the continent. This one might be a little tricky, but it shouldn't be too bad. Just make a pointer to an array of pointers to countries of the proper size, then add the countries on the continent to the array. Return the pointer to the array in the end. If none exist, return nullptr. .printCountryCatalogue will just print out the contents of the catalogue. This one should be simple. Print out" name in_continent" for each country on a new line. To do this, just have a loop go over the _catalogue and call each country's to string) method and print it out. . setPopulationOfSelectedCountry will take a country name and a new population and thern update the corresponding country in the catalogue's population (assuming it exists) findCountryWithLargestPop will return a pointer to the country with the largest population within our catalogue. If nothing is in the catalogue, return nullptr findCountry WithSmallestAreawill return a pointer to the country with the smallest population within out catalogue. If nothing is in the catalogue, return nullptr findMostPopulousContinent will return the name of the continent with the largest population based on the countries within the catalogue. This one will be a little tricky saveCountryCatalogue will take the name of a file to create and write to which will save some details about the contents of our catalogue. The function will return the number of lines written to the file. Format the output as follows. o NAMEICONTINENTPOPULATIONIPOPULATION DENSITY o ChinalAsia 1339190000 139.5431469965489 Do not be surprised if your output formats the numbers funny. This is fine. See my included example output file named saved al example.txt. o File LO

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions

Question

13. You always should try to make a good first impression.

Answered: 1 week ago

Question

Identify three ways to manage an intergenerational workforce.

Answered: 1 week ago

Question

Prepare a Porters Five Forces analysis.

Answered: 1 week ago

Question

Analyze the impact of mergers and acquisitions on employees.

Answered: 1 week ago