Hi
I have this problem
Animal should be abstract. It should hold two data members, name and size to keep the weight of the animal.
Reptile should have habitat as data member to keep information of the habitat such as land, salt water, fresh
water, etc Lastly, Snake should be a class with poisonous as data member to indicate whether it is poisonous or
not.
There should be a showInfo() function which is overridden through the hierarchy. It should provide appropriate
information about the objects. Classes Reptile and Snake should have a non-default constructor to initialize the
data members, while Animal needs a function to set its data member.
The main() function should construct 2 instances each of Reptiles and Snake. These should be stored in an array
of type Animal. A loop should be used to output information on all the objects
My solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <string>
using namespace std;
class Animal{
protected:
string name;
float weight;
public:
virtual void setAnimal(string, float);
void showInfo();
};
void Animal::setAnimal(string nname, float weightt){
name=nname;
weight=weightt;
}
void Animal::showInfo(){
cout<<"Name of animal : "<<endl;
cout<<"Weight : "<<endl;
}
class Reptile: public Animal{
private:
string habitat;
public:
Reptile(string);
~Reptile();
void setAnimal(string, float);
void showInfo();
};
Reptile::Reptile(string habitate):habitat(habitate){
}
Reptile::~Reptile(){}
void Reptile::showInfo(){
this->Animal::showInfo();
cout<<"Habitat type: "<<habitat<<endl;
}
class Snake: public Reptile{
private:
bool poisonous;
public:
Snake(string,bool);
~Snake();
void showInfo();
};
Snake::Snake(string habitatt, bool poison_ous):Reptile(habitatt),poisonous(poison_ous){}
Snake:: ~Snake(){}
void Snake::showInfo(){
this->Animal::showInfo();
this->Snake::showInfo();
cout<<"Poisonous: ";
if (poisonous==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
int main(){
Animal** animals= new Animal* [4];
//animals[0]->setAnimal("Crocs", 225);
animals[0]=new Reptile("Saltwater");
/*animals[1]->setAnimal("Lizards", 0.005);
animals[1]=new Reptile("Lands");
animals[2]->setAnimal("Cobra", 1.5);
animals[2]=new Snake("Land", true);
animals[3]->setAnimal("Anaconda", 5);
animals[3]=new Snake("Swamps", false );
*/
animals[0]->showInfo();
return 0;
}
|