home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter08 / constructor_critter.cpp next >
Encoding:
C/C++ Source or Header  |  2004-04-11  |  590 b   |  36 lines

  1. //Constructor Critter
  2. //Demonstrates constructors
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. class Critter
  9. {
  10. public:
  11.     int m_Hunger;
  12.     
  13.     Critter(int hunger = 0);       // constructor prototype
  14.     void Greet();
  15. };
  16.  
  17. Critter::Critter(int hunger)       // constructor definition
  18. {
  19.     cout << "A new critter has been born!" << endl;
  20.     m_Hunger = hunger;
  21. }
  22.  
  23. void Critter::Greet()             
  24. {
  25.     cout << "Hi. I'm a critter. My hunger level is " << m_Hunger << ".\n\n";
  26. }
  27.  
  28. int main()
  29. {    
  30.     Critter crit(7);
  31.     crit.Greet();
  32.  
  33.     return 0;
  34. }
  35.  
  36.