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

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