All the animals in this lab inherit from the abstract class Animal. Animal has a
ID: 3602381 • Letter: A
Question
All the animals in this lab inherit from the abstract class Animal. Animal has a name and a weight in grams. It has a public function eat(), which takes a double parameter that represents the weight in grams of a food item and adds that much to the animal's weight. It also has a call() function. Since different animals have different calls, call() must be a pure virtual function. Animal also needs a virtual << operator.
The abstract class Prey inherits from Animal and adds a flee() function. Since different Prey animals flee differently, this must be a pure virtual function.
The abstract class Predator is a subclass of Animal which also has a pure virtual predate() function. Predate() takes a reference to a Prey animal (one that extends Prey) as its parameter.
The concrete class Cat inherits from predator and implements call() by showing a message like "Fifi says 'Meow'". It also overrides the << operator to show a message like "Fifi is a Cat weighing 982.3 grams." Its predate() function shows a message like "Fifi pounces and devours Bambi, who weighs 6234 grams" using the actual names of the cat and the Prey animal and the actual weight of the Prey. Cat's call() method prints out the name of the cat and the message "says Meow!" Predate then calls the eat() function using the weight of the Prey animal as the double argument. Cat also has an overloaded friend << operator that shows a message like "Fifi is a cat weighing 501 grams"
Bird and Mouse are concrete subclasses of Prey. They implement flee() by printing messages that show how Birds and Mice flee (for example, "Minnie scurries off"). They also implement call() by printing messages like "Donald says 'tweet.'"
It is up to you to decide where to put constructors, getters, setters, and any other necessary methods.
Write a driver method that does the following: creates a Cat on the heap and calls its overloaded << operator and its call function creates a vector of pointers to Prey, creates at least two Birds and at least two Mice on the heap, and adds pointers to the Prey to the vector iterates through the vector. For each Prey animal, calls the Prey's call() function, generates a random number between 0 and 1, and, if the number is less than .5, calls the Cat's predate() method with the prey as parameter, otherwise calls the Prey's flee() method.
Here is a sample of the output :
Precious is a cat and weighs 1223.34 grams.
Precious says 'meow!'
Mickey says 'eek!'
Mickey scampers off
Minnie says 'eek!'
Precious pounces and eats Minnie, who weighs 22.3 grams
Daisy says 'tweet!'
Precious pounces and eats Daisy, who weighs 69.23 grams
Donald says 'tweet!'
Donald flies away
Explanation / Answer
Given below is the completed code for the question. Please note that each time the output will be random i.e. the cat will eat different preys depending on the random number generation. Please don't forget to rate the answer if it helped. Thank you.
animals.h
#ifndef animals_h
#define animals_h
#include <iostream>
using namespace std;
class Animal
{
protected:
string name;
double weight;
public:
Animal(string n, double w);
string getName() const;
double getWeight() const;
void eat(double foodwt);
virtual void call() = 0; //pure virtual function
friend ostream& operator <<(ostream& out, const Animal& a) ;
};
class Prey : public Animal
{
public:
Prey(string n, double w) :Animal(n,w){}
virtual void call() = 0; //pure virtual function
virtual void flee() = 0; //pure virtual function
};
class Predator : public Animal
{
public:
Predator(string n, double w):Animal(n, w){}
virtual void call() = 0; //pure virtual function
virtual void predate(const Prey& prey) =0;
};
class Cat : public Predator
{
public:
Cat(string n, double w):Predator(n, w){}
void call();
friend ostream& operator << (ostream& out, const Cat& c);
void predate(const Prey& prey);
};
class Bird: public Prey
{
public:
Bird(string n, double w):Prey(n, w){}
void flee();
void call();
friend ostream& operator << (ostream& out, const Bird& b);
};
class Mouse: public Prey
{
public:
Mouse(string n, double w):Prey(n, w){}
void flee();
void call();
friend ostream& operator << (ostream& out, const Mouse& b);
};
#endif /* animals_h */
animals.cpp
#include "animals.h"
//============== Animal functions
Animal::Animal(string name, double wt)
{
this->name = name;
this->weight = wt;
}
string Animal::getName() const
{
return name;
}
double Animal::getWeight() const
{
return weight;
}
void Animal::eat(double foodwt)
{
weight += foodwt;
}
//======== Cat functions
void Cat::call()
{
cout << name << " says 'meow'!" << endl;
}
ostream& operator << (ostream& out, const Cat& c)
{
out << c.name << " is a cat weighing " << c.weight << " grams." << endl;
return out;
}
void Cat::predate(const Prey& prey)
{
eat(prey.getWeight());
cout << name << " pounces and devours " << prey.getName() << ", who wieghs " << prey.getWeight() << " grams" << endl;
}
//====== Bird functions
void Bird::flee()
{
cout << name << " flies away" << endl;
}
void Bird::call()
{
cout << name << " says 'tweet'!" << endl;
}
ostream& operator << (ostream& out, const Bird& b)
{
out << b.name << " is a bird weighing " << b.weight << " grams." << endl;
return out;
}
//===== Mouse functions
void Mouse::flee()
{
cout << name << " scampers off" << endl;
}
void Mouse::call()
{
cout << name << " says 'eek'! " << endl;
}
ostream& operator << (ostream& out, const Mouse& b)
{
cout << b.name << " is a mouse weighing " << b.weight << " grams." << endl;
return out;
}
TestAnimals.cpp
#include <iostream>
#include "animals.h"
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
Cat *cat = new Cat("Precious", 1223.34);
cout << *cat;
cat->call();
vector<Prey*> preys;
//create 2 mice and 2 birds
Prey* m1 = new Mouse("Mickey", 25.3);
Prey* m2 = new Mouse("Minne", 22.3);
Prey* b1 = new Bird("Donald", 75.8);
Prey* b2 = new Bird("Daisy", 69.23);
//add preys to vector
preys.push_back(m1);
preys.push_back(m2);
preys.push_back(b1);
preys.push_back(b2);
//iterate through vector
srand(time(0));
for(int i = 0; i < preys.size(); i++)
{
preys[i]->call();
double r = (double)rand() / (double)((unsigned)RAND_MAX + 1); //generate random number between 0- 1
if(r < 0.5)
cat->predate(*preys[i]);
else
preys[i]->flee();
}
}
output
$ g++ animals.cpp TestAnimals.cpp
$ ./a.out
Precious is a cat weighing 1223.34 grams.
Precious says 'meow'!
Mickey says 'eek'!
Mickey scampers off
Minne says 'eek'!
Precious pounces and devours Minne, who wieghs 22.3 grams
Donald says 'tweet'!
Donald flies away
Daisy says 'tweet'!
Precious pounces and devours Daisy, who wieghs 69.23 grams