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.

Finalising the clases 1037intintintintvoidvoidvoidprotected:voidintcharvoidcharcharintintintintint};X() const;Y() const;Width() const;Height() const;ShowAll() const;ShowContent() const;PrepareContent();Change(int x, int y, char ch);Valid(int x, int y) const;Get(int x, int y, char **img) const;SetFrame();**fBkgd;**fCurrentImg;fX;fY;fWidth;fHeight;fFramed;<strong>The</strong> declarations for the specialized subclasses <strong>of</strong> class Window were givenearlier.DungeonItem class hierarchy<strong>The</strong> base class for the hierarchy defines a DungeonItem as something that can readits data from an input stream, can draw <strong>and</strong> erase itself, <strong>and</strong> can say where it is. Itowns a link to the Dungeon object, its Pt coordinate <strong>and</strong> a symbol.class DungeonItem {public:DungeonItem(Dungeon *d, char sym);virtual ~DungeonItem();PtWhere() const;virtual void Draw();virtual void Read(ifstream& in);virtual void Erase();protected:Dungeon *fD;PtfPos;charfSym;};DungeonItemSince class DungeonItem is the base class in a hierarchy, it provides a virtualdestructor <strong>and</strong> makes its data members protected (allowing access by subclasses).A Collectable object is just a DungeonItem with three extra integer datamembers <strong>and</strong> associated access functions that can return their values. Because aCollectable needs to read the values <strong>of</strong> its extra data members, the class redefinesthe DungeonItem read function.

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

Saved successfully!

Ooh no, something went wrong!