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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Inheritance</strong> <strong>and</strong> <strong>Polymorphism</strong> 1042WindowRep.h Geom.hD.hDungeon.h Ditem.hmain.cpDitem.cpGeom.cpDungeon.cp WindowRep.cpD.cpmain.oDitem.oGeom.oDungeon.o WindowRep.oD.oFigure <strong>29</strong>.8Module structure for Dungeon game example.<strong>The</strong> files D.h <strong>and</strong> D.cp contain the DynamicArray code defined in Chapter 21.<strong>The</strong> Geom files have the definition <strong>of</strong> the simple Pt class. <strong>The</strong> WindowRep filescontain WindowRep, Window <strong>and</strong> its subclasses. DItem.h <strong>and</strong> DItem.cp contain thedeclaration <strong>and</strong> definition <strong>of</strong> the classes in the DungeonItem hierarchy while theDungeon files contain class Dungeon.An outline for main() has already been given; function Terminate(), whichprints an appropriate "you won" or "you lost" message, is trivial.<strong>29</strong>.3.1 Windows classes<strong>The</strong>re are two aspects to class WindowRep: its "singleton" nature, <strong>and</strong> itsinteractions with a cursor addressable screen.<strong>The</strong> constructor is private. WindowRep objects cannot be created by client code(we only want one, so we don't want to allow arbitrary creation). Clients (like thecode <strong>of</strong> class Dungeon) always access the unique WindowRep object through thestatic member function Instance().WindowRep *WindowRep::Instance(){if(sWindowRep == NULL)sWindowRep = new WindowRep;

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

Saved successfully!

Ooh no, something went wrong!