29 The Power of Inheritance and Polymorphism
29 The Power of Inheritance and Polymorphism
29 The Power of Inheritance and Polymorphism
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Inheritance</strong> <strong>and</strong> <strong>Polymorphism</strong> 1060}y += dy;if(!fD->ValidPoint(Pt(x,y))) return;Monster* m = fD->M_at_Pt(Pt(x,y));if(m != NULL) {m->GetHit(power);if(!m->Alive())fD->RemoveM(m);}power /= 2;}Monster<strong>The</strong> constructor <strong>and</strong> destructor functions <strong>of</strong> class Monster both have empty bodiesfor there is no work to be done; the constructor passes its arguments back to theconstructor <strong>of</strong> its parent class (ActiveItem):Monster::Monster(Dungeon *d, char sym) : ActiveItem(d, sym){}Function Monster::Run() was defined earlier. <strong>The</strong> default implementations <strong>of</strong>the auxiliary functions are:int Monster::CanAttack(){Player *p = fD->Human();Pt target = p->Where();return fPos.Adjacent(target);}void Monster::Attack(){Player *p = fD->Human();p->GetHit(fStrength);}int Monster::CanDetect() { return 0; }void Monster::Advance() { }Ghost<strong>The</strong> Ghost::CanDetect() function uses the Pt::Distance() member functionto determine the distance to the Player (this function just takes the normalEuclidean distance between two points, rounded up to the next integral value).int Ghost::CanDetect(){Player *p = fD->Human();