CST-210 Animal Farm, Part 2
The purpose of this assignment is to assess your ability to do the following:
\begin{itemize}
\item Define abstract classes and pure virtual functions.
\item Utilize polymorphic variables in a C++ program.
\item Utilize static and dynamic casting to determine object type.
\item Implement classes that manage their memory allocation and deallocation.
\end{itemize}
For this assignment, modify the Animal Farm Part 1 assignment so that the Barn class
uses polymorphic instance variables.
Step 1:
Start by replacing the current instance variables (coop, pigPen, cowPen) with a single
arrays of type Animal. This array will hold 15 Animal variables.
Next, update the constructor so that it populates the Animal array with new Chicken,
Cow, and Pig objects. Replace the three separate feed methods with a method called
feedAnimals. The feedAnimals method should feed all the animals in the barn.
Finally, add a destructor to the Barn class. The destructor should delete all of the
Animal objects in the Barn.
Step 2:
Add a pure virtual method to the Animal class called speak (). Implement the
speak() method for each Animal subclass: Pig("Oink"), Cow("Moo"),
Chicken("Cluck"). Add a pure virtual method to the Animal class called
getTopWeight(). Implement this method in each Animal class as follows:
Pig::getTopWeight () returns 280, Chicken::getTopWeight () returns 12,
Cow::getTopWeight() returns 1350.
Step 3:
When the Barn feeds the animals, it must check to see if the animal is at or above the
top weight. If the animal meets or exceeds the top weight, then it is put out to pasture.
Create an outToPasture () method in the Barn class and have it output "<Animal
name> the <animal type> is put out to pasture." The outToPasture method should
use casting to determine each animal type and should replace all animals that are put out
to pasture with a replacement of the same species.
Step 4:
Write a main method that demonstrates your Barn class. The following image shows an
example:
#include <iostream>
#include "Chicken.h"
#include "Pig.h"
#include "Cow.h"
#include "Animal.h"
#include "Barn.h"
int main() {
Barn barn;
barn.showAll();
barn.feedAll();
barn.showAll();
return 0;
}