# How can i do this? Help meee!!

This topic is 4983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I know this may sound stupid but can u help me anyway plz. Zoo with the following animals : Sanke, Monkey, Crocodile. Snake eats 100g of of food. monkey eats 1 kilograms of food. Crocodile eats 4 Kilograms of food I must write a method where all the animals are fed, but the first animal are handed all the food and must eat only his share of the food etc.. And i also must write a method where one animal eats. I know this looks alot like home but plz help. :) Thanks in advance Bhangie

##### Share on other sites
You're right. It does look like homework. Show us what you've done so far and we'll help you on specific issues, but we won't do the whole thing for you - see the FAQ for why.

Enigma

##### Share on other sites
Can you specify your problem a bit more?
What is the 'food'? Does this exercise has to cover polymorphism?
...

##### Share on other sites
Hi

Yes this exercise covers inheritance and Ploymorphism.
class animal
{
public:
animal(const string &name, const int &age, const string &colour, const char &sex);
~animal();

/*Set the animal behavior, differnt for each animal*/
void set_behavior(string b) {_behave = b; }
string get_behavior() const { return _behave; }

/*Show animal information*/
void display_info()const;

private:
string _name;
int _age;
string _colour , _behave;
char _sex;
};

//Snake class derived from animal class
class snake : public animal
{
public:
snake(const string &name, const int &age, const string &colour, const char &sex);
~snake();

int amount_of_food_eaten();
void set_length(int l) { _lenght = l; }
int get_lenght() const { return _lenght; }

//Eat your share of the food
void eat();

private:
int _total_food; /*grams*/
int _lenght;
};

I am not verry far where would these methods/member functions go in my base class or derived class.
The food is just food, i gues i can have something private data member
int food = 1000; and initialize it to some value? not sure.

##### Share on other sites
Edit: the board is being stupid and not recognizing my quote tags. Figure it out for yourself. :P

  /*Set the animal behavior, differnt for each animal*/  void set_behavior(string b) {_behave = b; }   string get_behavior() const { return _behave; }// No, get rid of this part (and "_behave"). The polymorphism will take care of// the behaviour. However, in order to make things work, you will require the// base "animal" to declare the interface for behaviour that's common to all// the animals. In your case, that means:  virtual void eat(int& food_supply) = 0;// Notice the keyword "virtual", and the weird "=0" instead of an// implementation. This is a "pure virtual" function, which implies your Animal// class is abstract (that is, you won't create instances of unspecified// Animals, but only of the specific subtypes).// Here I have the eat() function taking the food supply as a parameter; likely// the demonstration code will have some single food supply somewhere as a// global. I passed the supply by reference, so that the function may change// the size of the supply (representing the animal's consumption) with that// change being reflected in the calling code.// One other thing you need that's very important:  virtual ~animal();// The destructor must also be virtual; this is very important. Otherwise,// animal::~animal gets called for each animal, which will neglect important// "species-specific" object cleanup.//Snake class derived from animal classclass snake : public animal {  // As a style note: You don't need to copy my indentation, but two tabs on  // each side of the ':' for a class declaration is rather odd (and excessive).public:  // Now within the 'snake' class, we can declare the eat() concretely:  void eat(int& food_supply);};

I am not verry far where would these methods/member functions go in my base class or derived class.
The food is just food, i gues i can have something private data member
int food = 1000; and initialize it to some value? not sure.[/quote]

See comments in the source box. The food definitely shouldn't be a data member of anything, unless maybe you need to model the Zookeeper or something. Think: which animal would "have" the uneaten food? A bit illogical.

Anyway, supposing we just allocate some food in main():

int main() {  int theFood = 1000;  // Create pointers to animals. Note we have an array of pointers; an array of  // plain Animals wouldn't work, because of object slicing (google it).  Animal* zoo[3];  zoo[0] = new Snake(); // but supply the needed arguments, of course  zoo[1] = new Monkey();  zoo[2] = new Crocodile();  // Just for example. Anyway, now we can do our feeding:  for (int i = 0; i < 3; ++i) {    zoo->eat(theFood); // Here the polymorphism is at work.    // Depending on the actual animal (checked at run-time), an appropriate    // version of eat() - Snake::eat, Monkey::eat or Crocodile::eat - is called.  }  cout << "There are " << theFood << " units of food left." << endl;  // Since I dynamically allocated the Animals here, they must be deallocated  // as well:  for (int i = 0; i < 3; ++i) {    delete zoo; // invokes the destructor. Again polymorphism will make sure    // that each animal is cleaned up in the appropriate way.  }}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634042
• Total Posts
3015204
×