In a population, the birth rate and death rate are calculated as follows: Birth
ID: 3696098 • Letter: I
Question
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, the birth rate and death rate are:
Birth Rate = 8,000 ÷ 100,000 = 0.08 Death Rate = 6,000 ÷ 100,000 = 0.06
Design a Population class that stores a population, number of births, and number of deaths for a period of time. Member functions should return the birth rate and death rate. Implement the class in a program. Input Validation: Do not accept population figures less than 1, or birth or death numbers less than 0.
Explanation / Answer
#include "Population.h"
#include <iomanip>
using namespace std;
void printTitle();
int main(){
double bith = 5, death = 5, population = 5, bRate, dRate;
Population pop, ipop(bith, death, population);
printTitle();
cout << fixed << setprecision(5);
cout << " pop birth rate: " << pop.getBirthRate();
cout << " pop death rate: " << pop.getDeathRate();
cout << " ipop birth rate: " << ipop.getBirthRate();
cout << " ipop death rate: " << ipop.getDeathRate();
ipop.setDeaths(800);
ipop.setBirths(100);
ipop.setPopulation(5);
cout << " ipop birth rate: " << ipop.getBirthRate();
cout << " ipop death rate: " << ipop.getDeathRate();
return 0;
}
void printTitle(){
cout << " **************";
cout << " Population |";
cout << " **************";
}
Population.h
#ifndef POPULATION_H
#define POPULATION_H
#include <iostream>
using namespace std;
class Population{
private:
int population, births, deaths;
public:
Population();
Population(int, int, int);
void setPopulation(int);
void setBirths(int);
void setDeaths(int);
double getDeathRate();
double getBirthRate();
};
#endif
Population.cpp
#include "Population.h"
using namespace std;
Population::Population(){
births = 5;
deaths = 5;
population = 5;
}
Population::Population(int b, int d, int p){
if(p < 2)p = 2;
if(b < 0)b = 0;
if(d < 0)d = 0;
births = b;
deaths = d;
population = p;
}
void Population::setPopulation(int p){
if(p < 2)p = 2;
population = p;
}
void Population::setBirths(int b){
if(b < 0)b = 0;
births = b;
}
void Population::setDeaths(int d){
if(d < 0)d = 0;
deaths = d;
}
double Population::getDeathRate(){
double dRate = deaths / population;
return dRate;
}
double Population::getBirthRate(){
double bRate = births / population;
return bRate;
}
sample output
**************
Population |
**************
pop birth rate: 1.00000
pop death rate: 1.00000
ipop birth rate: 1.00000
ipop death rate: 1.00000
ipop birth rate: 20.00000
ipop death rate: 160.00000sh-4.3$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.