Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. FIRST PROBLEM Create a pure virtual base class Animal. The base class has one

ID: 3770110 • Letter: 1

Question

1. FIRST PROBLEM

Create a pure virtual base class Animal.

The base class has one private string attribute: _name.

The base class has one constant string method, name, that returns the class’s name attribute.

The Animal class constructor has one string parameter, new_name. The constructor makes sure that new_name is a string that is neither empty nor filled with white-space characters. It throws an exception if it is not. The constructor should not contain any input or output statements.

The base class has three pure virtual Boolean methods: AreYouAMeatEater, DoYouHaveFourLegs and AreYouAMammal. We shall call these methods characteristic methods. A characteristic method returns true if an animal has the characteristic and false otherwise.

A derived class has only a default constructor. It invokes the Animal class’s one parameter constructor.

The derived classes define the base class’s pure virtual methods. The methods should not contain any input or output statements. The following table shows the values that the derived class methods return.

Create an array of eight pointers to Animals. Assign a pointer to a particular Animal to each element of the array. No two pointers may point to the same kind of Animal. This is, all pointers point to different animals.

Use the array to write the following game:

The game randomly selects an animal from the array.

Use the selected animal’s characteristic methods to display its characteristics.

From the information that the animal reveals about itself, the user tries to guess the animal’s name. If the user guesses the animal’s name then the player wins; otherwise the player loses.

Write a Boolean function to play the game. The function returns true if the player wins and false if the player loses.

Invoke the game function in a loop so that the player can play the game an arbitrary number of times. Count the number of times the player plays the game, the number of times the player wins and the number of times the player loses. Display the counts.

2. SECOND PROBLEM

Rewrite problem 1's solution to use a different version of the game. Write the inverse of problem 1' game. The game randomly generates the animal's name and the player tries to guess the animal's characteristics.

Replace the array in problem 1 with a vector. Use iterators where ever possible.

AreYouAMeatEater DoYouHaveFourLegsAreYouAMammal True True Panther True True False Komodo Dragon Sperm Whale Rattle Snake True False True True False False True False True True Cow False False True Green Iguana False False True Manatee False False Sparrow False

Explanation / Answer

(1).

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;

string animals[] = {"Panther", "Komodo Dragon", "Sperm Whale", "Rattle Snake", "Cow", "Green Iguana", "Manatee", "Sparrow"};
/*
vector<string> animalsVector(animals, end(animals));

*/

class Animal
{
   private:
       string _name;
   public:
       const string name()
       {
           return _name;
       }

       Animal(string new_name)
       {
           if(!new_name.empty())
           {
               _name = new_name;
           }
       }

       string getName()
       {
       return _name;
       }

       void setName(string nm)
       {
       _name = nm;
       }

       virtual bool AreYouAMeatEater() = 0;
       virtual bool DoYouHaveFourLegs() = 0;
       virtual bool AreYouAMammal() = 0;
};

class derivedAnimal : public Animal
{
   derivedAnimal() : Animal("default")
   {
   }

   virtual bool AreYouAMeatEater()
   {
   string temp;
   if((temp=getName())=="Panther") return true;
else if((temp=getName())=="Komodo Dragon") return true;
else if((temp=getName())=="Sperm Whale") return true;
else if((temp=getName())=="Rattle Snake") return true;
else if((temp=getName())=="Cow") return true;
else if((temp=getName())=="Green Iguana") return true;
else if((temp=getName())=="Manatee") return true;
else if((temp=getName())=="Sparrow") return false;
   }

   virtual bool DoYouHaveFourLegs()
   {
string temp;
   if((temp=getName())=="Panther") return true;
else if((temp=getName())=="Komodo Dragon") return true;
else if((temp=getName())=="Sperm Whale") return false;
else if((temp=getName())=="Rattle Snake") return false;
else if((temp=getName())=="Cow") return true;
else if((temp=getName())=="Green Iguana") return true;
else if((temp=getName())=="Manatee") return false;
else if((temp=getName())=="Sparrow") return false;
   }

   virtual bool AreYouAMammal()
   {
string temp;
   if((temp=getName())=="Panther") return true;
else if((temp=getName())=="Komodo Dragon") return false;
else if((temp=getName())=="Sperm Whale") return true;
else if((temp=getName())=="Rattle Snake") return false;
else if((temp=getName())=="Cow") return true;
else if((temp=getName())=="Green Iguana") return false;
else if((temp=getName())=="Manatee") return true;
else if((temp=getName())=="Sparrow") return false;
   }
};

bool game(int *w, int *l)
{
   int pos = rand()%8;
   //derivedAnimal dr = new derivedAnimal(animals[pos]);
   derivedAnimal dr;
   dr.AreYouAMeatEater() ? cout<<"Meat Eater" : cout<<"Not a meat Eater";
   dr.DoYouHaveFourLegs() ? cout<<"Four legged" : cout << "Not four legged";
   dr.AreYouAMammal() ? cout<<"Mammal" : cout<<"Not a mammal";

   cout<<"Player, Can you guess the animal name ?? "; cin >> temp_name;
   if(temp_name == dr._name) w++;
   else l++;
}

int main()
{
int r = rand()%10;
int w=0, l=0;
while(r--)
{
   game(&w,&l);
}

cout << "No of times played : " << r << endl;
cout << "Player won : " << w << endl;
cout << "Player lost : " << l << endl;

return 0;
}

(2).

Insert below statement along with the string *animals statement

vector<string> animalsVector(animals, end(animals));

and

replace derivedAnimal dr = new derivedAnimal(animals[pos]);

with derivedAnimal dr = new derivedAnimal(animals.at(i));