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

In C++: Create a pure virtual base class Animal. The base class has one private

ID: 3771969 • Letter: I

Question

In C++:

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.

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

This may helps you..please check once

class Animal
{
public:
Animal(){
printf("%s ",__FUNCTION__);
}
~Animal(){
printf("%s ",__FUNCTION__);
}
void move(){
printf("Animal %s ",__FUNCTION__);
}
virtual void eat(){
printf("Animal %s ",__FUNCTION__);
}
};

class Dog: public Animal
{
public:
Dog(){
printf("%s ",__FUNCTION__);
}
~Dog(){
printf("%s ",__FUNCTION__);
}
void move(){
printf("Dog %s ",__FUNCTION__);
}
void eat(){
printf("Dog %s ",__FUNCTION__);
}
};

int main()
{
{
Dog dog;
Animal* pAnimal = &dog;
Dog* pDog = &dog;
pAnimal->eat();
pAnimal->move();
pDog->eat();
pDog->move();
}
{
Dog* pDog = new Dog();
Animal* pAnimal = pDog;
pAnimal->eat();
pAnimal->move();
pDog->eat();
pDog->move();
delete pAnimal;
}
return 0;
}