29 The Power of Inheritance and Polymorphism
29 The Power of Inheritance and Polymorphism
29 The Power of Inheritance and Polymorphism
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> 1032DungeonItemownslink to Dungeon, pointcoord, display symboldoesdraws itself, erasesitself, reads data fromfile, reports where it isCollectableowns3 integer value fieldsdoesreads extra data,reports valuesActiveItemownshealth & strengthattributesdoesreads extra data"runs" (define in subclass)gets hitreports if still "alive"movesPlayerMonsterowns"manna", wealth attributes,movecount, links to windowsused to display statusdoesreads extra datagets user comm<strong>and</strong>, takescollectables, attacksmonsters, interpretsmovement & magic comm<strong>and</strong>sowns(nothing extra)does"runs" using"Can Attack", "Attack","Can Detect", "Advance","NormalMove"Ghost W<strong>and</strong>erer PatrolSubclasses <strong>of</strong> Monster ownunique data (patrol "route", w<strong>and</strong>erer record<strong>of</strong> last move)doessubstitute specialized NormalMove(), Advance(),functions; read any extra data params.Figure <strong>29</strong>.4DungeonItem class hierarchy.class Dungeon {public:Dungeon();~Dungeon();void Load(const char filename[]);int Run();int Accessible(Pt p) const;Window *Display();Player *Human();intValidPoint(Pt p) const;Monster *M_at_Pt(Pt p);Collectable *PI_at_Pt(Pt p);void RemoveProp(Collectable *pi);void RemoveM(Monster *m);