Question
AnimalColony is a class with one int* and one double* data member pointing to the population and rate of growth of the animal colony, respectively.
AnimalColony is a class with one int* and one double* data member pointing to the population and rate of growth of the animal colony, respectively. An integer and a double are read from input to initialize myAnimalColony. Write a copy constructor for AnimalColony that creates a deep copy of myAnimalColony. At the end of the copy constructor, output "Called AnimalColony's copy constructor" and end with a newline.
Ex: If the input is 12 4.00, then the output is:
Called AnimalColony's copy constructor Initial population: 12 whales with 5.00 rate of growth Called AnimalColony's copy constructor After 1 month(s): 72 whales After 2 month(s): 432 whales After 3 month(s): 2592 whales Custom value interest rate 12 whales with 4.00 rate of growth
#include
class AnimalColony { public: AnimalColony(int startingPopulation = 0, double startingRate = 0.0); AnimalColony(const AnimalColony& col); void SetPopulation(int newPopulation); void SetRate(double newRate); int GetPopulation() const; double GetRate() const; void Print() const; private: int* population; double* rate; };
AnimalColony::AnimalColony(int startingPopulation, double startingRate) { population = new int(startingPopulation); rate = new double(startingRate); }
/* Your code goes here */
void AnimalColony::SetPopulation(int newPopulation) { *population = newPopulation; }
void AnimalColony::SetRate(double newRate) { *rate = newRate; }
int AnimalColony::GetPopulation() const { return *population; }
double AnimalColony::GetRate() const { return *rate; }
void AnimalColony::Print() const { cout << *population << " whales with " << fixed << setprecision(2) << *rate << " rate of growth" << endl; }
void SimulateGrowth(AnimalColony c, int months) { for (auto i = 1; i <= months; ++i) { c.SetPopulation(c.GetPopulation() * (c.GetRate() + 1.0)); cout << "After " << i << " month(s): " << c.GetPopulation() << " whales" << endl; } }
int main() { int population; double rate;
cin >> population; cin >> rate;
AnimalColony myAnimalColony(population, rate); AnimalColony myAnimalColonyCopy = myAnimalColony; myAnimalColony.SetRate(rate + 1.0); cout << "Initial population: "; myAnimalColony.Print(); SimulateGrowth(myAnimalColony, 3); cout << endl; cout << "Custom value interest rate" << endl; myAnimalColonyCopy.Print();
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started