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

  1. //Abstract Creature
  2. //Demonstrates abstract classes
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class Creature  //abstract class
  8. {
  9. public:
  10.     Creature(int health = 100);
  11.     virtual void Greet() const = 0;   //pure virtual member function
  12.     virtual void DisplayHealth() const;
  13.  
  14. protected:
  15.     int m_Health;
  16. };
  17.  
  18. Creature::Creature(int health): 
  19.     m_Health(health)
  20. {}
  21.  
  22. void Creature::DisplayHealth() const
  23. {
  24.     cout << "Health: " << m_Health << endl;
  25. }
  26.  
  27. class Orc : public Creature
  28. {
  29. public:
  30.     Orc(int health = 120);
  31.     virtual void Greet() const;
  32. };
  33.  
  34. Orc::Orc(int health): 
  35.     Creature(health)
  36. {}
  37.  
  38. void Orc::Greet() const
  39. {
  40.     cout << "The orc grunts hello.\n";
  41. }
  42.  
  43. int main()
  44. {
  45.     Creature* pCreature = new Orc();
  46.     pCreature->Greet();
  47.     pCreature->DisplayHealth();
  48.     
  49.     return 0;
  50. }
  51.