Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called \"country\" with the following features: -integer attribut

ID: 3555644 • Letter: C

Question

Create a class called "country" with the following features:

-integer attributes for the population of the country, total area in square miles, and total area of water in square miles

- accessors and mutators for each attribute

- initial value and default (set all attributtes to 0) constructors

- a method named "density" that calculates the population per square mile

- a method named "density_land" that calculates the population per square mile of land(not including water)

- a method that overloads the subtraction operator and calculates the difference in population density between two countries

Once your class is created, use it in a program where you read the data for three countries from the attached text file.The data is in the following order : population of country A, square miles of country A, square miles of water of country A, population of country B, square miles of country B, square miles of water of country B, population of country C, square miles of country C, and square miles of water of country C.

Write the following (with descriptions) to an output file : the population density of each country, the population density of each country not including water, and the difference in population density between the three countries

Explanation / Answer

#include <iostream>
#include <stdlib.h>
#include <fstream>

using namespace std;


class Country
{
protected:
int pop;
   int area;
   int water;
public:
   Country()
   {
       pop = 0;
       water=0;
       area = 0;
   }

   Country(int p, int a, int w)
   {
       pop = p;
       water = w;
       area = a;
   }
//   ~Country();

   int getPopulation()
   {
       return pop;
   }
   int setPopulation(int n)
   {
       pop = n;
   }
   int getArea()
   {
       return area;
   }
   int setArea(int n)
   {
       area = n;
   }
   int getWaterArea()
   {
       return water;
   }int setWaterArea(int n)
   {
       water = n;
   }

   int Density()
   {
       if(area == 0)
       {
           cout << "Enter value for area"<<endl;
           return 0;
       }
       else
           return pop/area;
   }
   int Density_land()
   {
       if(area == water)
       {
           cout << "There is no land - only water"<<endl;
           return 0;
       }
       else
           return pop/(area - water);
   }
   int operator-(Country& a)
   {
       return abs(a.Density() - Density());
   }  
};

int main()
{
   Country A, B, C;
   int p,a,w;
   ifstream inp("filename");
   if(inp.is_open())
   {
   while(! inp.eof())
   {
       inp >> p;
       inp >> a;
       inp >> w;
       A.setPopulation(p);
       A.setArea(a);
       A.setWaterArea(w);
       p=a=w=0;
       inp >> p;
       inp >> a;
       inp >> w;
       B.setPopulation(p);
       B.setArea(a);
       B.setWaterArea(w);
       p=a=w=0;
       inp >> p;
       inp >> a;
       inp >> w;
       C.setPopulation(p);
       C.setArea(a);
       C.setWaterArea(w);
   }
       inp.close();
    }
   else cout << "Unable to open file";

   ofstream myfile;
   myfile.open ("out.txt");
   myfile << "Population density of A: " << A.Density()<<endl;
   myfile << "Population density of B: " << B.Density()<<endl;
   myfile << "Population density of C: " << C.Density()<<endl;
   myfile << "Population density_land of A: " << A.Density_land()<<endl;
   myfile << "Population density_land of B: " << B.Density_land()<<endl;
   myfile << "Population density_land of C: " << C.Density_land()<<endl;
   myfile << "Population density differeance of A and B: " << A-B <<endl;
   myfile << "Population density differeance of A and C: " << A-C <<endl;
   myfile << "Population density differeance of C and B: " << C-B <<endl;
   myfile.close();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote