17. class SpaceShip {}; class GiantSpaceShip : public SpaceShip {}; class Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << "Asteroid hit a SpaceShip" << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << "Asteroid hit a GiantSpaceShip" << endl; } }; class ExplodingAsteroid : public Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << "ExplodingAsteroid hit a SpaceShip" << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << "ExplodingAsteroid hit a GiantSpaceShip" << endl; } };
18. class SpaceShip {}; class GiantSpaceShip : public SpaceShip {}; class Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << "Asteroid hit a SpaceShip" << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << "Asteroid hit a GiantSpaceShip" << endl; } }; class ExplodingAsteroid : public Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << "ExplodingAsteroid hit a SpaceShip" << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << "ExplodingAsteroid hit a GiantSpaceShip" << endl; } }; Asteroid theAsteroid; SpaceShip theSpaceShip; GiantSpaceShip theGiantSpaceShip; theAsteroid . CollideWith (theSpaceShip); theAsteroid . CollideWith (theGiantSpaceShip); Asteroid * theAsteroidReference = new ExplodingAsteroid (); theAsteroidReference -> CollideWith (theSpaceShip); theAsteroidReference -> CollideWith (theGiantSpaceShip); SpaceShip & theSpaceShipReference = theGiantSpaceShip; theAsteroid . CollideWith (theSpaceShipReference); theAsteroidReference -> CollideWith (theSpaceShipReference);
19. class SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; class GiantSpaceShip : public SpaceShip {}; Asteroid hit a SpaceShip ExplodingAsteroid hit a SpaceShip SpaceShip & theSpaceShipReference = theGiantSpaceShip; Asteroid & theAsteroidReference = theExplodingAsteroid; theSpaceShipReference. CollideWith (theAsteroid); theSpaceShipReference. CollideWith (theAsteroidReference); class SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; class GiantSpaceShip : public SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; Asteroid hit a GiantSpaceShip ExplodingAsteroid hit a GiantSpaceShip