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
#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;
}
Comments
Leave a comment