C++ Program that asks you to write a class, and an vector of objects. Please fol
ID: 645268 • Letter: C
Question
C++ Program that asks you to write a class, and an vector of objects. Please follow the instructions and use Visual Studio to write in. I will be awarding best answer and points to answer that is correct.
C++ Program that asks you to write a class, and an vector of objects. Please follow the instructions and use Visual Studio to write in. Define a class country as follows: Class: country Define a class called country which has the following private data fields: 1. Name of the country (use the string data type). 2. Population of the country (use the long data type). 3. Area of the country in square kilometers (use the double data type) This class should have the following constructors: 1. A programmer-defined default constructor 2. A programmer-defined constructor that accepts one incoming parameter value for each private field. These incoming parameter values are for initializing a newly created country object. This class should have the following public methods: 1. A get method for each private data field of country. 2. A set method for each private data field of country. 3. A method for deriving the population density of country with the following formula: population density per square kilometer (as a double value) = population / area Program: main () Write a program main () that asks a user for full information of an indefinite number of actual countries until the user chooses to quit. You can assume that the user will enter each piece of information in the correct data type. For example:Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
class country{
private:
string name;
long population;
double area;
public:
country(){
};
country(string x,long y,double z){
name = x;
population = y;
area = z;
};
string getname(){
return name;
};
long getpop(){
return population;
};
double getarea(){
return area;
};
void setname(string x){
name = x;
};
void setpop(long x){
population = x;
};
void setarea(double x){
area = x;
};
double density(){
return (double)population/area;
}
};
main(){
string name;
long pop;
double area;
country c;
vector <country> countries;
while(1){
cout << "Please enter the name of the country(-99 = no more)" << endl;
cin >> name;
if(name==string("-99"))break;
cout << "Please enter the population of this country in a whole figure" << endl;
cin >> pop;
cout << "Please enter the area of this country in square kilometers" << endl;
cin >> area;
c = country(name,pop,area);
countries.push_back(c);
}
cout << endl << "Countries entered are :"<<endl<<endl;
for(int i=0;i<countries.size();i++){
cout << "Country : "<<countries[i].getname()<<endl;
cout << "Population : "<<countries[i].getpop()<<endl;
cout << "Area : "<<countries[i].getarea()<<" sq.kilometers "<<endl;
cout << "Population density : "<<countries[i].density()<<" people per square kilometer"<<endl;
cout << endl;
}
cout << "Total number of countries entered : "<<countries.size()<<endl<<endl;
long maxpop=0;
double maxarea=0;
double maxdensity=0;
int p,a,d;
for(int i=0;i<countries.size();i++){
if(countries[i].getpop()>=maxpop){
p=i;
maxpop = countries[i].getpop();
}
if(countries[i].getarea()>=maxarea){
a=i;
maxarea = countries[i].getarea();
}
if(countries[i].density()>=maxdensity){
d=i;
maxdensity = countries[i].density();
}
}
cout << "Country with largest population : "<<countries[p].getname() << " - Population : " << countries[p].getpop()<<endl;
cout << "Country with largest area : "<<countries[a].getname() << " - Area : " << countries[a].getarea()<<endl;
cout << "Country with highest population density : "<<countries[d].getname() << " - Density : " << countries[d].density()<<endl;
cout << endl << "Bye!" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.