/* University College London Dept of P&A course in C++ 3C59 | all rights reserved 2000 | | Module: Polymorphism: | | Class: SuperHero + sub classes | | | Author: P.Clarke */ #include //======================= Base class ================================= // SuperHero //===================================================================== class SuperHero { private: // Basic superhereo stuff int m_strength; int m_numberOfLives; int m_weaponStrength ; std::string m_name ; public: // methods which we dont want to be overridden void addLife() ; std::string name() ; // Methods we expect to be overridden in sub classes virtual bool isAlive( ) ; virtual bool canFight( ) ; virtual int power( ) ; virtual void fightResult( bool result ) ; virtual std::string announce( ) ; //Constructor SuperHero( std::string name, int strength, int numberOfLives, int weaponStrength ) ; }; //================================ sub class ============================= // InvisibleSuperHero //=================================================================== // Here we add variables and methods to do with whether the hero is // invisible or not class InvisibleSuperHero : public SuperHero { private: // Extra members bool m_isInvisible ; public: // Extra methods to change visibility state void disappear() ; void appear() ; // Methods we need to override : virtual int power( ) ; // need to modify power if invisible virtual std::string announce(); // modify announcment message // Constructor InvisibleSuperHero( std::string name, int strength, int numberOfLives, int weaponStrength ) ; }; //========================= sub class =============================== // FlyingSuperHero //=================================================================== // Here we add variables and methods to do with whether the hero is // flying or not class FlyingSuperHero : public SuperHero { private: // Extra members for flying bool m_inAir ; public: // Extra methods void fly() ; void land() ; // Methods we need to override virtual bool canFight( ) ; virtual std::string announce() ; // Constructor FlyingSuperHero( std::string name, int strength, int numberOfLives, int weaponStrength ) ; }; //============================= sub class ============================ // LifeSuckingSuperHero //=================================================================== // Here we add variables and methods to do with // sucking the life of any hero it vanquishes class LifeSuckingSuperHero : public SuperHero { private: // No Extra members public: // Methods we need to override: virtual void fightResult( bool result ) ; // need to increment lives if we win virtual std::string announce() ; // Constructor LifeSuckingSuperHero( std::string name, int strength, int numberOfLives, int weaponStrength ) ; };