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

class hierarchy program in C++ : animal protected: char sound[10]; // contains \

ID: 3768077 • Letter: C

Question

class hierarchy program in C++ :

animal
  protected:   char sound[10];              // contains "moo", "quack"
unsigned char data; // 0 - cannot fly, 1 - can fly
// number of legs - 3 bits

  public:      virtual void displaydata(); // prints the value of sound and fly

cow: derived from animal
  public:       void displaydata(); // prints the value of sound, number of legs and fly

bird: derived from animal
  public:       void displaydata(); // prints the value of sound and fly

Requirements:

1. Make .cpp and .h files for all class: animal.cpp, animal.h, cow.cpp, cow.h, bird.cpp, bird.h.

2. Use bit-wise operators to both set and retrieve fly and number of legs

3. Write a main function which creates instances of the animal, cow and bird class.

4. Use the vector and map classes in the main function.

5. Initialize the instances and call the animaldisplaydata function to test polymorphism on the displaydata function of the bird and cow class.

void animaldisplaydata(animal * a)
{

// Your code goes here

}

Explanation / Answer

I have written below C++ code as per the given requirements in the probelm statement with neat comments
for time being i have created all classed and methods here ..
Inially I have created animal class with data members and display method..
so this animal class is publically inherited by cow and bird..
In cow and bird created display method and override by animal class display with virtual..
The important methid animaldisplaydata created with passing of animal class objcet from main method.
this method will handle the display data method of cow and bird method.

#include<iostream>
using namespace std;

// Created animal class with two parameters sound and data
class animal
{
protected:
   string sound;       // contains "moo", "quack"
   unsigned char data;   // 0 - cannot fly, 1 - can fly
public:
    // Created animal constructor for initialise the data members with default values
   animal()
   {
       sound = "";
       data = 0;
   }
   virtual void displaydata() // prints the value of sound and fly
   {
        cout<<"Animal Data :"<<sound<<endl;
       cout<<"Sound is :"<<sound<<endl;
       cout<<"Fly is :"<<data<<endl;
   }
};

// Publically Inherited animal class with cow class
class cow : public animal
{
public:
   void displaydata() // prints the value of sound, number of legs and fly
   {
       cout<<"Cow Data is"<<endl;
       cout<<"Sound is : 'moo' "<<endl;
       cout<<"Fly is :"<< (0&4)<<endl;
   }
};

// Publically Inherited animal class with bird class
class bird : public animal
{
public:
   void displaydata() // prints the value of sound and fly
   {
       cout<<"Bird Data is"<<endl;
       cout<<"Sound is : 'quack'"<<sound<<endl;
       cout<<"Fly is :"<<(1 & 2)<<endl;
   }
};

// Created method for display and polymorphism achieved through the animaldisplaydata
void animaldisplaydata(animal *a)
{
    // Calling display method of a particular class
   a->displaydata();
}
int main()
{
   // Declaring object of cow class with animal object
    animal *a_obj = new cow;
   // Passing cow object to animaldisplaydata to display cow data
    animaldisplaydata(a_obj);
   // Declaring object of bird class with animal object
    animal *a_obj_1 = new bird;
   // Passing bird object to animaldisplaydata to display bird data
    animaldisplaydata(a_obj_1);
   return 1;
}