C++ In a population, the birth rate and death rate are calculated as follows: Bi
ID: 3867395 • Letter: C
Question
C++
In a population, the birth rate and death rate are calculated as follows:
Birth Rate = Number of Births ÷ Population
Death Rate = Number of Deaths ÷ Population
For example, in a population of 100,000 that has 8,000 births and 6,000 deaths per year,
Birth Rate = 8,000 ÷ 100,000 = 0.08
Death Rate = 6,000 ÷ 100,000 = 0.06
Design a Population class that stores a current population, annual number of births, and annual number of deaths for some geographic area. The class should allow these three values to be set in two ways: by passing arguments to a threeparameter constructor when a new Population object is created or by calling the setPopulation, setBirths, and setDeaths class member functions. The class should also have getBirthRate and getDeathRate functions that compute and return the birth and death rates. Your program should properly use pointer data types where appropriate. Write a program that uses the Population class and illustrates its capabilities. Input Validation: If a population figure less than 2 is passed to the class, use a default value of 2. Population must be greater than zero. If a birth or death figure less than 0 is passed to the class, prompt user to type in a positive
Sample output:
Enter total population: 100000
Enter annual number of births: 8000
Enter annual number of deaths: 6000
Population Statistics
Population: 100000
Birth Rate: 0.080
Death Rate: 0.060
Explanation / Answer
#include <iostream>
using namespace std;
class Population {
private:
int totalPopulation;
int annualBirth;
int annualDeath;
public:
Population(int totalPopulation, int annualBirth,
int annualDeath) {
this->totalPopulation = totalPopulation;
this->annualBirth = annualBirth;
this->annualDeath = annualDeath;
}
void setTotalPopulation(int totalPopulation) {
this->totalPopulation = totalPopulation;
}
void setAnnualBirth(int annualBirth) {
this->annualBirth = annualBirth;
}
void setAnnualDeath(int annualDeath) {
this->annualDeath = annualDeath;
}
double getBirthRate() {
return (double) annualBirth/totalPopulation;
}
double getDeathRate() {
return (double) annualDeath/totalPopulation;
}
};
int main() {
int tp, ab, ad;
cout << "Enter total population: ";
cin >> tp;
cout << "Enter annual birthsRate: ";
cin >> ab;
cout << "Enter annual deathsRate: ";
cin >> ad;
Population* p = new Population(tp, ab, ad);
cout << "Population Statistics" << " Population: " << tp << " Birth Rate: " << p->getBirthRate() << " Death Rate: " << p->getDeathRate() << endl;
}
NOTE: Validity check is stll left!! We shuould submit wint validity check as one function
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.