13.07.2015 Views

29 The Power of Inheritance and Polymorphism

29 The Power of Inheritance and Polymorphism

29 The Power of Inheritance and Polymorphism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

WindowRep <strong>and</strong> Window classes 10<strong>29</strong><strong>29</strong>.2.3 DungeonItem hierarchyClass Monster is meant to be an abstraction; the real inhabitants <strong>of</strong> the dungeon areinstances <strong>of</strong> specialized subclasses <strong>of</strong> class Monster.Class Monster has to provide the following functions (there may be othersfunctions, <strong>and</strong> the work done in these functions may get exp<strong>and</strong>ed later, this listrepresents an initial guess):• Constructor <strong>and</strong> destructor<strong>The</strong> constructor will set a Dungeon* pointer to link back to the Dungeon object<strong>and</strong> set a char data member to the symbol used to represent the Monster on themap view.Since class Monster is to be in a hierarchy, it had better define a virtualdestructor.• ReadA Monster is supposed to read details <strong>of</strong> its initial position, its "health" <strong>and</strong>"strength" from an input file. It will need data members to store thisinformation.• Access functions to get position, to check whether "alive", …• A Run() function that as already outlined will work through redefinableauxiliary functions like CanAttack(), Attack(), CanDetect(), Advance()<strong>and</strong> NormalMove().• Draw <strong>and</strong> Erase functions.• A Move function.Calls Erase(), changes coords to new coords given as argument, <strong>and</strong> callsDraw().• GetHit functionReduces "health" attribute in accord with damage inflicted by Player.<strong>The</strong> example implementation has three specializations: Ghost, Patrol, <strong>and</strong>W<strong>and</strong>erer. <strong>The</strong>se classes redefine member functions from class Monster asneeded.A Ghost is a Monster that:class Ghost• uses the default "do nothing" implementation defined by Monster::NormalMove() along with the st<strong>and</strong>ard CanAttack(), Attack() functions;• has a CanDetect() function that returns true when the player is within a fixeddistance <strong>of</strong> the point where the Ghost is located (the presence <strong>of</strong> interveningwalls makes no difference to a Ghost object's power <strong>of</strong> detection);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!