Module 4. Static Members, Friends Exercises

1. Add the following below: Declare the integer static data member
oldestWeightDate.  Create the static member function
changeOldestWeightDate with void return type and a single integer
parameter.  Set oldestWeightDate to that parameter.  Do not forget a
function prototype for changeOldestWeightDate.  Also, be sure to
define the static member at global scope.

   class ZooAnimal  
   {
    private:
      char *name;
      int cageNumber;
      int weightDate;
      int weight;
    public:
      ZooAnimal (char*, int, int, int); // constructor function
      void changeWeight (int pounds);
      char* reptName ();
      int reptWeight ();
      int daysSinceLastWeighed (int today);
   };


2.  Write a non-member function reptOldestZooAnimalWeightDate that
returns the value of the ZooAnimal class static data member
oldestWeightDate.

   class ZooAnimal  
   {
    private:
      char *name;
      int cageNumber;
    public:
      static int oldestWeightDate;
    private:
      int weightDate;
      int weight;
    public:
      ZooAnimal (char*, int, int, int); // constructor function
      void changeWeight (int pounds);
      static changeOldestWeightDate (int date);
      char* reptName ();
      int reptWeight ();
      static int reptOldestWeightDate ();
      int daysSinceLastWeighed (int today);
   };

   int ZooAnimal::oldestWeightDate;  //define static member at global scope


3. Write the ZooAnimal member function reptNutrition that returns
the result of the member function reptMealNutrition from the nested
class nutrition invoked on the ZooAnimal class data member nutr.

   class ZooAnimal  
   {
    public:
      class nutrition
      {
       private:
         int numberMeals;
         int maxPoundsFed;
         float avgNutrValue;
       public:
         inline nutrition () {
           numberMeals = 4;
           maxPoundsFed = 75;
           avgNutrValue = 38.21;}; // constructor function
         inline float reptMealNutrition () 
          {return numberMeals * avgNutrValue;};
       };

    private:
      nutrition nutr;
    public:
      ZooAnimal (char*, int, int, int); // constructor function
      float reptNutrition ();
   };


4.  Declare that the class cageAssignment is a friend class to
ZooAnimal.

   class ZooAnimal  
   {
    private:
      char *name;
      int cageNumber;
      int weightDate;
      int weight;
    public:
      ZooAnimal (char*, int, int, int); // constructor function
      ~ZooAnimal (); // destructor function
      void changeWeight (int pounds);
      char* reptName ();
      int reptWeight ();
      int daysSinceLastWeighed (int today);
   };


5.  Write the changeCage member function of class cageAssignment.  It
should set the data member cageNumber of the ZooAnimal object
parameter to the value of the cageAssignment data member enclosure and
then increment enclosure.

   class ZooAnimal;

   class cageAssignment
   {
    private:
      int enclosure;
    public:
      inline cageAssignment (int num) {enclosure = num;}; // constructor
      inline ~cageAssignment () {}; // destructor function
      void changeCage (ZooAnimal&);
      int reptCage (ZooAnimal&);
   };

   class ZooAnimal  { int cageNumber; };


6.  Write a function prototype for the friend function declaration
which is the cageAssignment class member function reptCage.  This
function has a single ZooAnimal parameter (called by reference) and
returns an integer value.

   class ZooAnimal;

   class cageAssignment
   {
      int reptCage (ZooAnimal&);
   };

   class ZooAnimal  
   {
    private:
      char *name;
      int cageNumber;
      int weightDate;
      int weight;
    public:
      ZooAnimal (char*, int, int, int); // constructor function
      ~ZooAnimal (); // destructor function
      void changeWeight (int pounds);
      char* reptName ();
      int reptWeight ();
      int daysSinceLastWeighed (int today);
   };