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.

Implementation: DungeonItems 1057ActiveItem<strong>The</strong> constructor for class ActiveItem again just initializes some data members tozero after passing the given arguments to the DungeonItem constructor. FunctionActiveItem::Read() is similar to Collectable::Read() in that it invokes theDungeonItem::Read() function then reads the extra data values (fHealth <strong>and</strong>fStrength).<strong>The</strong>re are a couple <strong>of</strong> trivial functions (GetHit() { fHealth -= damage; };<strong>and</strong> Alive() { return fHealth > 0; }). <strong>The</strong> Move() operation involves callsto the (inherited) Erase() <strong>and</strong> Draw() functions. Function Step() works out thex, y <strong>of</strong>fset (+1, 0, or -1) coordinates <strong>of</strong> a chosen neighboring Pt.void ActiveItem::Move(const Pt& newpoint){Erase();fPos.SetPt(newpoint);Draw();}Pt ActiveItem::Step(int dir){Pt p;switch(dir) {case 1: p.SetPt(-1,1); break;case 2: p.SetPt(0,1); break;case 3: p.SetPt(1,1); break;case 4: p.SetPt(-1,0); break;case 6: p.SetPt(1,0); break;case 7: p.SetPt(-1,-1); break;case 8: p.SetPt(0,-1); break;case 9: p.SetPt(1,-1); break;}return p;}Player<strong>The</strong> constructor for class Player passes its arguments to its parents constructor <strong>and</strong>then sets its data members to 0 (NULL for the pointer members). <strong>The</strong> Read()function is similar to Collectable::Read(); it invokes the inherited DungeonItem::Read() <strong>and</strong> then gets the extra "manna" parameter.<strong>The</strong> first call to ShowStatus() creates the NumberItem <strong>and</strong> EditText windows<strong>and</strong> arranges for their display. Subsequent calls update the contents <strong>of</strong> theNumberItem windows if there have been changes (the call to SetVal() results inexecution <strong>of</strong> the NumberItem object's ShowContents() function so resulting inchanges to the display).void Player::ShowStatus(){if(fWinH == NULL) {

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

Saved successfully!

Ooh no, something went wrong!