Sign in to follow this  

How can i do this? Help meee!!

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


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

[quote]

/*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 class
class 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[i]->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[i]; // invokes the destructor. Again polymorphism will make sure
// that each animal is cleaned up in the appropriate way.
}
}

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this