// --------------------------------------------------------------------- // Simple Boss // Demonstrates inheritance // --------------------------------------------------------------------- #include using namespace std; // --------------------------------------------------------------------- // Declaration of class Enemy // This would normally be in the file Enemy.h // --------------------------------------------------------------------- class Enemy { public: int m_Damage; Enemy(); void Attack() const; }; // --------------------------------------------------------------------- // Definition/Implementation of Enemy class // This would normally be in the file: Enemy.cpp // --------------------------------------------------------------------- Enemy::Enemy(): m_Damage(10) { // nothing to do, the above instantiation list // already initialized m_Damage = 10 } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Enemy::Attack() const { cout << "Attack inflicts " << m_Damage << " damage points!\n"; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier; Boss(); void SpecialAttack() const; }; // --------------------------------------------------------------------- // Implementation of the class Boss // Normally this would be done in the file: Boss.cpp // --------------------------------------------------------------------- Boss::Boss(): m_DamageMultiplier(3) { // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3 } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Boss::SpecialAttack() const { cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n"; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- // --------------------------------------------------------------------- // main function // Normally this would be found in a separate .cpp file // perhaps named BossTest.cpp // --------------------------------------------------------------------- int main() { cout << "Creating an enemy.\n"; Enemy enemy1; enemy1.Attack(); cout << "\nCreating a boss.\n"; Boss boss1; boss1.Attack(); boss1.SpecialAttack(); return 0; } // --------------------------------------------------------------------- // files ends