//Overriding Boss //Demonstrates calling and overriding base member functions #include using namespace std; // --------------------------------------------------------------------- // Enemy Interface Declaration // Typically found in Enemy.h, // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- class Enemy { public: Enemy(int damage = 10); void virtual Taunt() const; //made virtual to be overridden void virtual Attack() const; //made virtual to be overridden private: int m_Damage; }; // --------------------------------------------------------------------- // Enemy Implementation Definition // Typically found in Enemy.cpp, // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Constructor // --------------------------------------------------------------------- Enemy::Enemy(int damage):m_Damage(damage) { // nothing left to do } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Enemy::Taunt() const { cout << "The enemy says he will fight you.\n"; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Enemy::Attack() const { cout << "Attack! Inflicts " << m_Damage << " damage points."; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Interface Declaration for // Boss class derived from Enemy class // Again typically this would be in Boss.h // --------------------------------------------------------------------- class Boss : public Enemy { public: Boss(int damage = 30); void virtual Taunt() const; //optional use of keyword virtual void virtual Attack() const; //optional use of keyword virtual }; // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Implementation Definition of Boss class // Again typically this would be in Boss.cpp // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Boss's Constructor // --------------------------------------------------------------------- Boss::Boss(int damage): Enemy(damage) // call base class constructor with argument { } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Boss::Taunt() const //override base class member function { cout << "The boss says he will end your pitiful existence.\n"; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Boss::Attack() const //override base class member function { Enemy::Attack(); //call base class member function cout << " And laughs heartily at you.\n"; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- // --------------------------------------------------------------------- // main - it all begins here // This would also typically be in a separate file, e.g. BossTester.cpp // --------------------------------------------------------------------- int main() { cout << "Enemy object:\n"; Enemy anEnemy; anEnemy.Taunt(); anEnemy.Attack(); cout << "\n\nBoss object:\n"; Boss aBoss; aBoss.Taunt(); aBoss.Attack(); return 0; }