Advertisement
reellearning

Overloaded Insertion Operator - VPet.cpp

Jun 4th, 2012
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. /*
  2.  * Vpet.cpp
  3.  *
  4.  *  Created on: Jun 4, 2012
  5.  *      Author: Derek
  6.  */
  7.  
  8. #include "VPet.h"
  9.  
  10. VPet::VPet(double w, bool hun) :
  11.         weight(w), hungry(hun)
  12. {
  13. }
  14.  
  15. VPet::VPet() :
  16.         weight(100), hungry(true)
  17. {
  18. }
  19.  
  20. void VPet::feedPet(int amt)
  21. {
  22.     if (amt >= weight * 0.5)
  23.     {
  24.         hungry = false;
  25.     }
  26.     else
  27.     {
  28.         hungry = true;
  29.     }
  30.  
  31.     weight = weight + 0.25 * amt;
  32. }
  33.  
  34. double VPet::getWeight() const
  35. {
  36.     return weight;
  37. }
  38.  
  39. bool VPet::getHungry() const
  40. {
  41.     return hungry;
  42. }
  43.  
  44.  
  45. std::ostream& operator<<(std::ostream& os, const VPet& vp)
  46. {
  47.     std::string hungerStatus = "";
  48.  
  49.     if(vp.hungry)
  50.     {
  51.         hungerStatus = "hungry";
  52.     }
  53.     else
  54.     {
  55.         hungerStatus = "not hungry";
  56.     }
  57.  
  58.     return os << "weight: " << vp.weight << " hunger status: "
  59.             << hungerStatus << std::endl;
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement