home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter10 / simple_boss2.cpp < prev   
Encoding:
C/C++ Source or Header  |  2006-11-02  |  932 b   |  60 lines

  1. //Simple Boss 2.0
  2. //Demonstrates access control under inheritance
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class Enemy
  8. {
  9. public:
  10.     Enemy();  
  11.     void Attack() const;
  12.  
  13. protected:
  14.     int m_Damage;
  15. };
  16.  
  17. Enemy::Enemy(): 
  18.     m_Damage(10)
  19. {}    
  20.  
  21. void Enemy::Attack() const
  22.     cout << "Attack inflicts " << m_Damage << " damage points!\n";
  23. }  
  24.  
  25. class Boss : public Enemy
  26. {
  27. public:
  28.     Boss();
  29.     void SpecialAttack() const;
  30.  
  31. private:
  32.     int m_DamageMultiplier;
  33. };
  34.  
  35. Boss::Boss(): 
  36.     m_DamageMultiplier(3)
  37. {}  
  38.  
  39. void Boss::SpecialAttack() const
  40. {
  41.     cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); 
  42.     cout << " damage points!\n";
  43. }
  44.  
  45. int main()
  46.     cout << "Creating an enemy.\n";
  47.     Enemy enemy1;
  48.     enemy1.Attack();
  49.  
  50.     cout << "\nCreating a boss.\n";
  51.     Boss boss1;
  52.     boss1.Attack();
  53.     boss1.SpecialAttack();
  54.  
  55.     return 0;
  56.  
  57.