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: 3725070 • 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>
class Animal
{
public:
virtual void eat()
{
  std::cout<<" Animal is eating"<<std::endl;
}
};
class Mammal:public Animal
{
public:
void eat()
{
  std::cout<<" The Mammal eats"<<std::endl;
}
};
class Insect:public Animal
{
public:
void eat()
{
  std::cout<<" The Insect eats"<<std::endl;
}
};
class Reptile:public Animal
{
public:
void eat()
{
  std::cout<<" The Reptile eats"<<std::endl;
}
};
int main()
{
Mammal M;Reptile R;Insect I;
std::vector<Animal *>Zoo;
Zoo.push_back(&M);
Zoo.push_back(&R);
Zoo.push_back(&I);
int i=0;
while(i<3)
{
  Zoo[i]->eat();
  i++;
}
return 1;
}

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