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

Create a C++ project4 Create a pure virtual base class Animal. The base class ha

ID: 3534879 • Letter: C

Question

Create a C++ project4

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: name. The constructor makes sure that the name is a string that is neither empty nor filled with white-space characters; otherwise it throws an exception.

The base class has three pure virtual Boolean methods: AreYouAMeatEater, DoYouHaveFourLegs and AreYouAMammal. We shall call these methods characteristic methods.

Use the base class’s Name method to verify an animal’s identity.

e.g.

cin.getline(name, ‘ ’);

if(name == animal->Name())

cout << “You are who you say you are!†<< endl;

else

cout << “You are an imposter – begone!†<< endl;

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

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

AreYouAMeatEater

DoYouHaveFourLegs

AreYouAMammal

Panther

True

True

True

Komodo Dragon

True

True

False

Sperm Whale

True

False

True

Rattle Snake

True

False

False

Cow

False

True

True

Green Iguana

False

True

False

Manatee

False

False

True

Sparrow

False

False

False

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. In other words all pointers must point to different animals.

Use the array of eight pointers to write the following game.

The game randomly generates a number between zero and seven and uses the number to select an element of 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 identity. If the user guesses the animal’s identity then the player wins; otherwise the player loses. Use the base class’s Name method to verify a derived class animal’s identity.

Play the game in loop so the user may play the game as many times as he wishes.

Your program’s behavior and “project4.exeâ€â€™s behavior should be identical. That is, given the same input as “project4.exeâ€, your program should produce the same output and given the same input as your program, “project4.exe†should produce the same output.

AreYouAMeatEater

DoYouHaveFourLegs

AreYouAMammal

Panther

True

True

True

Komodo Dragon

True

True

False

Sperm Whale

True

False

True

Rattle Snake

True

False

False

Cow

False

True

True

Green Iguana

False

True

False

Manatee

False

False

True

Sparrow

False

False

False

Explanation / Answer

class Animal { protected: std::string m_strName; // We're making this constructor protected because // we don't want people creating Animal objects directly, // but we still want derived classes to be able to use it. Animal(std::string strName): m_strName(strName){ } public: std::string GetName() { return m_strName; } virtual const char* Speak() = 0; }; class Cow: public Animal { public: Cow(std::string strName): Animal(strName) { } // We forgot to redefine Speak }; int main() { Cow cCow("Betsy"); std::cout