fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. class Population
  6. {
  7. private:
  8. int pop;
  9. int births;
  10. int deaths;
  11.  
  12. public:
  13. void setPopulation (int);
  14. void setBirths(int);
  15. void setDeaths(int);
  16.  
  17. int getPopulation();
  18. double getBirthRate();
  19. double getDeathRate();
  20.  
  21. Population() : pop(0), deaths(0), births(0) {}
  22. };
  23.  
  24. void Population::setPopulation(int p)
  25. {
  26. pop = p;
  27. }
  28.  
  29. void Population::setBirths(int B)
  30. {
  31. births = B;
  32. }
  33.  
  34. void Population::setDeaths(int d)
  35. {
  36. deaths = d;
  37. }
  38.  
  39. int Population::getPopulation()
  40. {
  41. return pop;
  42. }
  43.  
  44. double Population::getBirthRate()
  45. {
  46. return births / static_cast<double>(pop);
  47. }
  48. double Population::getDeathRate()
  49. {
  50. return deaths / static_cast<double>(pop);
  51. }
  52.  
  53.  
  54. int main()
  55. {
  56. Population Town;
  57. int People;
  58. int Births,
  59. Deaths;
  60.  
  61. cout << "Enter total population: ";
  62. cin >> People;
  63. while (People < 1)
  64. { cout << "ERROR: Value has to be greater than 0. ";
  65. cin >> People;
  66. }
  67. Town.setPopulation(People);
  68.  
  69. cout << "Enter annual number of births: ";
  70. cin >> Births;
  71. while (Births < 0)
  72. { cout << "ERROR: Value cannot be negative. ";
  73. cin >> Births;
  74. }
  75. Town.setBirths(Births);
  76.  
  77. cout << "Enter annual number of deaths: ";
  78. cin >> Deaths;
  79. while (Deaths < 0)
  80. { cout << "ERROR: Value cannot be negative. ";
  81. cin >> Deaths;
  82. }
  83. Town.setDeaths(Deaths);
  84.  
  85. cout << "\nPopulation Statistics ";
  86. cout << fixed << showpoint << setprecision(3);
  87. cout << "\n\tPopulation: " << setw(7) << Town.getPopulation();
  88. cout << "\n\tBirth Rate: " << setw(7) << Town.getBirthRate();
  89. cout << "\n\tDeath Rate: " << setw(7) << Town.getDeathRate() << endl;
  90.  
  91. return 0;
  92. }
Success #stdin #stdout 0s 3300KB
stdin
5 6 7
stdout
Enter total population: Enter annual number of births: Enter annual number of deaths: 
Population Statistics 
	Population:       5
	Birth Rate:   1.200
	Death Rate:   1.400