29 The Power of Inheritance and Polymorphism
29 The Power of Inheritance and Polymorphism
29 The Power of Inheritance and Polymorphism
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Inheritance</strong> <strong>and</strong> <strong>Polymorphism</strong> 1040class W<strong>and</strong>erer : public Monster {public:W<strong>and</strong>erer(Dungeon *d);protected:virtual void NormalMove();virtual int CanDetect();virtual void Advance();int fLastX, fLastY;Pt fPath[20];};class Patrol : public Monster {public:Patrol(Dungeon *d);virtual void Read(ifstream& in);protected:virtual void NormalMove();virtual int CanDetect();virtual void Advance();Pt fPath[20];Pt fRoute[100];int fRouteLen;int fNdx, fDelta;};Object InteractionsFigure <strong>29</strong>.6 illustrates some <strong>of</strong> the interactions involved among different objectsduring a cycle <strong>of</strong> Dungeon::Run().DungeonobjectfInhabitants(Dynamic Array)Monsterobject(s)PlayerobjectRun()Nth(i)Run()CanAttack()loopHuman()Where()Human()GetHit()Attack()Figure <strong>29</strong>.6Some <strong>of</strong> the object interactions in Dungeon::Run.<strong>The</strong> diagram illustrates aspects <strong>of</strong> the loop where each Monster object in thefInhabitants collection is given a chance to run. <strong>The</strong> Dungeon object will firstinteract with the DynamicArray fInhabitants to get a pointer to a particularMonster. This will then be told to run; its Run() function would call itsCanAttack() function.