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

Write a C++ Program: Implement a base class Animal. Derive classes Reptile, Mamm

ID: 3725075 • Letter: W

Question

Write a C++ Program:

Implement a base class Animal. Derive classes Reptile, Mammal, and Insect from the base class. In the base class, develop a virtual function

void Animal::eat()
{
std::cout << "Animal is eating" << std::endl;
}

Write the class definitions, the constructors, and a virtual function for void eat() for each of the 3 subclasses. Create dynamic instances of the 3 different subtypes and put references to the object instances in a vector. Iterate through the vector and call the eat function for the different object instances. If you did this correctly, using a virtual method, then the correct eat method will be called. Call the vector variable zoo, then iterate over the zoo.

example:

std::vector<Animal *> zoo;
// fill the zoo vector with pointers of animal base type

for(Animal* animal : zooA)
{
animal->eat();
}

If you created 3 Animals and they were Insect, Mammal and Reptile, then the output would be:

The Mammal eats
The Insect eats
The Reptile eats

Implement a base class Animal. Derive classes Reptile, Mammal, and Insect from the base class. In the base class, develop a virtual function std: cout

Explanation / Answer

#include <iostream>
#include <vector>
using namespace std;

class Animal {

public:
virtual void eat ()
{ cout << "Animal is eating" <<endl; }
};

class Reptile: public Animal {
public:
void eat ()
{ cout << "Reptile eats " <<endl; }
};

class Mammal: public Animal {
public:
void eat ()
{ cout << "Mammal eats " <<endl; }
};


class Insect: public Animal {
public:
void eat ()
{ cout << "Insect eats " <<endl; }
};


int main () {

vector<Animal *> zoo;
Mammal dog;
zoo.push_back(&dog);
Reptile lizard;
zoo.push_back(&lizard);
Insect beetle;
zoo.push_back(&beetle);

for(Animal* animal : zoo)
{
animal->eat();
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote