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

Utilizing polymorphism, construct a class hierarchy that totals up feeding histo

ID: 3778012 • Letter: U

Question

Utilizing polymorphism, construct a class hierarchy that totals up feeding history of 4 animal types (elephant, dolphin, lion, and beaver).

You must at a minimum include the following methods:

getAnimalType (Returns the animal’s type. Should return “Elephant”, “Lion”, etc.)

getAnimalName (Return animal’s name, not what animal type it is. For instance, “Simba”, “Dumbo”, etc.)

getAnimalLocation (returns the animal’s location in the zoo. For instance, “Lion Cage 1”)

getFoodTypes (Returns a list of the types of food the animal eats and the unit the food is measured in (HINT: Enumerations may help))

addFeeding (this will take in the type and amount of food fed)

getFeedingTotals (this returns a list of the animal's total food intake. For Dumbo in the table above, this would be 80 lbs of hay and 5 lbs of peanuts)

Construct a tester program that tests your class hierarchy (no user input necessary). Your program must have at least one of each animal instantiated. At a minimum, your tester program must feed each of the animals and then print out records of their feedings. Each public method must be called at least once in the test.

Explanation / Answer

#include <string>
#include <iostream>
using namespace std;
class Animal
{
      protected:
      string m_name;

    Animal(string name)
        : m_name(name)
    {
    }
     public:
      string getAnimalType() {
           return m_name;
      }
      virtual string getAnimalName() { return "???"; } // virtual features to achieve the Run-time polymorphism and avoid the funtion overriding
      virtual string getAnimalLocation(){ return "???" ; }
      virtual string getFoodTypes(){ return "???"; }
      virtual int addFeeding(int amount[], string foodtype[]) {/*int amt = 0; tring type = "???"; */ }
      virtual int getFeedingTotals(){ return 0; }
  
};
class elephant: public Animal
{
    public:
    elephant(string name)
        : Animal(name)
    {
    }
    public:
   int total;
    int amount[2];
    string type[2];
    virtual string getAnimalName() {
          return "Simba";
      }
     virtual string getAnimalLocation(){
           return "elephant Cage 1" ;
      }
      virtual string getFoodTypes(){
          
           return "plant";
      }
      virtual int addFeeding(int amount[], string foodtype[]) {
        total = 0;
           for(int i=0; i<2;i++)
          {
              total = total + amount[i];
              cout<<amount[i]<<" Lbs FoodType "<<foodtype[i]<<" ";  
          }
        
      }
      virtual int getFeedingTotals(){
         
           return total;
      }
};
class dolphin: public Animal
{
public:
    dolphin(string name)
        : Animal(name)
    {
    }
    public:
   int total;
    int amount[2];
    string type[2];
    virtual string getAnimalName() {
          return "Dumba";
      }
     virtual string getAnimalLocation(){
           return "dolphin Cage 2" ;
      }
      virtual string getFoodTypes(){
          
           return "plants";
      }
      virtual int addFeeding(int amount[], string foodtype[]) {
        total = 0;
           for(int i=0; i<2;i++)
          {
              total = total + amount[i];
              cout<<amount[i]<<" Lbs FoodType "<<foodtype[i]<<" ";  
          }
        
      }
      virtual int getFeedingTotals(){
         
           return total;
      }
  
};
class beaver: public Animal
{
   public:
    beaver(string name)
        : Animal(name)
    {
    }
    public:
   int total;
    int amount[2];
    string type[2];
    virtual string getAnimalName() {
          return "Dumba";
      }
     virtual string getAnimalLocation(){
           return "beaver Cage 3" ;
      }
      virtual string getFoodTypes(){
          
           return "carnivore";
      }
      virtual int addFeeding(int amount[], string foodtype[]) {
        total = 0;
           for(int i=0; i<2;i++)
          {
              total = total + amount[i];
              cout<<amount[i]<<" Lbs FoodType "<<foodtype[i]<<" ";  
          }
        
      }
      virtual int getFeedingTotals(){
         
           return total;
      }
};

class lion: public Animal
{
   public:
    lion(string name)
        : Animal(name)
    {
    }
    public:
   int total;
    int amount[2];
    string type[2];
    virtual string getAnimalName() {
          return "Babar";
      }
     virtual string getAnimalLocation(){
           return "Lion Cage 4" ;
      }
      virtual string getFoodTypes(){
          
           return "obligate carnivore";
      }
      virtual int addFeeding(int amount[], string foodtype[]) {
        total = 0;
           for(int i=0; i<2;i++)
          {
              total = total + amount[i];
              cout<<amount[i]<<" Lbs FoodType "<<foodtype[i]<<" ";  
          }
        
      }
      virtual int getFeedingTotals(){
         
           return total;
      }
};


void Report(Animal &animal)
{
    cout << animal.getAnimalType() << " " << animal.getAnimalName() << " "<<animal.getAnimalLocation()<<" ";
    cout << animal.getFoodTypes()<<" ";
   int amt[2];
   string type[2];
   cout<<"provide food to animal ";
   for(int i=0; i<2;i++)
   {
       cout<<"amount :"; cin>>amt[i];
       cout<<"FoodType :"; cin>>type[i];  
   }
   animal.addFeeding(amt, type);
   cout<<"Total : "<<animal.getFeedingTotals()<<"Lbs ";
}
int main()
{
    elephant elephant("elephant"); // elephant class have elephant object
    Report(elephant); cout <<" ";
   lion lion("Lion");
   Report(lion); cout <<" ";
   beaver beaver("Beaver");
   Report(beaver); cout <<" ";
   dolphin dolphin("Dolphin");
   Report(dolphin); cout <<" ";
  
}