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

Create a simple program which uses the following class hierarchy: Parent class:

ID: 3867029 • Letter: C

Question

Create a simple program which uses the following class hierarchy:

Parent class: animal (should be an abstract class)

  protected:
      String sound;              // "moo", "quack", etc
      unsigned int number_of_legs;

Three different child classes: (three different specific animals)

Requirements:

Each class must have its own .cpp and .h file, including constructors and destructors.

Write a main function which creates instances of the three animals

Use the vector and Map classes in the main function to store multiple instances of the animals. Find a way to use these classes.

Create a polymorphic function called displayanimaldata which prints all the data within each animal.
This is not a member function in any class.

void displayanimaldata(animal * a)
{
    a->displaydata();
}

Initialize the instances and call the displayanimaldata function to test polymorphism of the displaydata function

Explanation / Answer

Given below are the needed files for the question. Please don't forget to rate the answer if it helped. Thank you.

animal.h


#ifndef animal_h
#define animal_h
#include <iostream>
using namespace std;
class animal
{
protected:
string sound;
unsigned int number_of_legs;
public:
animal();
animal(string sound, unsigned int legs);
string get_sound();
int get_number_of_legs();
virtual void displaydata() = 0;
~animal() {}
};

#endif /* animal_h */

animal.cpp


#include "animal.h"
animal::animal()
{
   sound = "";
   number_of_legs = 0;
}
animal::animal(string sound, unsigned int legs)
{
   this->sound = sound;
   this->number_of_legs = legs;
}
string animal::get_sound()
{
   return sound;
}
int animal::get_number_of_legs()
{
   return number_of_legs;
}

cow.h


#ifndef cow_h
#define cow_h
#include "animal.h"
class cow : public animal
{
protected:
string breed;
public:
cow();
cow(string breed);
void displaydata();
~cow(){}
};

#endif /* cow_h */

cow.cpp

#include "cow.h"
cow::cow() : cow("Jersey") //default constructor
{
  
}
cow::cow(string breed) : animal("moo", 4)
{
this->breed = breed;
}
void cow::displaydata()
{
cout << "this cow is of " << breed << " breed and says " << sound << ". It has " << number_of_legs << " of legs." << endl;
}

duck.h

#ifndef duck_h
#define duck_h
#include "animal.h"
class duck : public animal
{
protected:
string color;
public:
duck();
duck(string color);
void displaydata();
~duck(){}
};
#endif /* duck_h */

duck.cpp

#include "duck.h"
duck::duck() : duck("white") //default constructor
{
  
}
duck::duck(string color) : animal("quack", 2)
{
this->color = color;
}
void duck::displaydata()
{
cout << "this duck is " << color << " in color and says " << sound << ". It has " << number_of_legs << " of legs." << endl;
}

cat.h


#ifndef cat_h
#define cat_h
#include "animal.h"
class cat : public animal
{
protected:
string color;
public:
cat();
cat(string color);
void displaydata();
~cat(){}
};
#endif /* cat_h */

cat.cpp

#include "cat.h"
cat::cat() : cat("white") //default constructor
{
  
}
cat::cat(string color) : animal("meow", 4)
{
this->color = color;
}
void cat::displaydata()
{
cout << "this cat is " << color << " in color and says " << sound << ". It has " << number_of_legs << " of legs." << endl;
}

animal_main.cpp


#include <stdio.h>
#include <vector>
#include "cow.h"
#include "duck.h"
#include "cat.h"
using namespace std;
void displayanimaldata(animal *a)
{
a->displaydata();
}
int main()
{
vector<animal*> animals;
animal *a1 = new cat("black");
animal *a2 = new duck();
animal *a3 = new cow();
animal *a4 = new duck("brown");
  
animals.push_back(a1);
animals.push_back(a2);
animals.push_back(a3);
animals.push_back(a4);
  
  
cout << "there are " << animals.size() << " animals" << endl;
for(int i = 0; i < animals.size(); i++)
displayanimaldata(animals[i]);
  
  
}

output

there are 4 animals
this cat is black in color and says meow. It has 4 of legs.
this duck is white in color and says quack. It has 2 of legs.
this cow is of Jersey breed and says moo. It has 4 of legs.
this duck is brown in color and says quack. It has 2 of legs.

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