Answer to Question #207457 in C++ for osama

Question #207457

Consider an abstract class Animal having

A datamember “Name”

• A datamember “Zoo”

• A single function named show()

A class named Birds inherits Animal class and adds fields representing

• A bool type variable flying

• Override function named show() to display values of its all attributes

A class named Reptiles inherits BOOK class and adds fields representing

• Length

• Override function named show() to display values of its all attributes

Write a main() function that instantiates objects of derived classes to access respective show() function using dynamic binding


1
Expert's answer
2021-06-15T15:12:26-0400
#include <iostream>
#include <string>
using namespace std;
class Animal{
    protected:
    string name, zoo;
    public:
    Animal(string n, string z): name(n), zoo(z){}
    virtual void show(){
        cout<<"Name: "<<name<<endl;
        cout<<"Zoo: "<<zoo<<endl;
    }
};
class Birds: public Animal{
    bool flying;
    public:
    Birds(string n, string z, bool f): Animal(n, z){}
    void show(){
        Animal::show();
        cout<<"Flying: "; if(flying) cout<<"True\n"; else cout<<"False\n";
    }
};
class Reptiles: public Animal{
    string length;
    public:
    Reptiles(string n, string z, string l): Animal(n, z), length(l){}
    void show(){
        Animal::show();
        cout<<"Length: "<<length<<endl;
    }
};
int main(){
    Birds eagle("eagle", "Central Park", false);
    eagle.show();
    
    Reptiles alligator("Alligator", "Not Central Park", "3.5 meters");
    alligator.show();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS