OOPortal OOPortal


Eco Simulation  «Prev  Next»
Lesson 4Lifeform inheritance hierarchy
Objective Examine the inheritance hierarchy for the lifeforms in the ecological simulation.

Lifeform inheritance hierarchy

The inheritance hierarchy for living will be one level deep:
//currently only predator class
class fox : public living {
public:
   fox(int r, int c, int a = 0) : age(a)
      { row = r; column = c; }
   state  who() { return FOX; }
     //deferred method for foxes
   living*  next(world w);
protected:
   int  age;   //used to decide on dying
};

//currently only prey class
class rabbit : public living {
public:
   rabbit(int r, int c, int a = 0) : age(a)
      { row = r; column = c; }
   state  who() { return RABBIT; }
   living*  next(world w);
protected:
   int  age;
};

//currently only plant life
class grass : public living {
public:
   grass(int r, int c) { row = r; column = c; }
   state who() { return GRASS; }
   living* next(world w);
};

//nothing lives here
class empty : public living {
public:
   empty(int r, int c) { row = r; column = c; }
   state  who() { return EMPTY; }
   living*  next(world w);
};
Notice how the design allows other forms of predator, prey, and plant life to be developed using a further level of inheritance.
The characteristics of how each lifeform behaves are captured in its version of next(), which we will examine in the next lesson.