Module 8. Class Templates Exercises

1.  The class ZooAnimal is going to become a Class Template.  Declare
a class template with a single template type parameter weightType.
Declare the data name weight to be of whatever type is used for
weightType.  Make the last parameter to the constructor function to be
of weightType.  The constructor function is not in the body of class
template ZooAnimal.  Modify it so that it shows that this is the
constructor function for the class template ZooAnimal with template
type parameter weightType.

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

   // ---------- the constructor function
     ZooAnimal::ZooAnimal (char* who, int whichCage, int weighDay, int pounds)
   {
    char *name = new char[20];
    strcpy (name, who);
    cageNumber = whichCage;
    weightDate = weighDay;
    weight = pounds;
   }


2.  The class template ZooAnimal has been modified to include another
template type parameter.  Write a statement that declares a class
template with two template type parameters: first weightType and
second cageType.

   template <class weightType, class cageType>
   class ZooAnimal  
   {
    private:
      char *name;
      cageType cageNumber;
      int weightDate;
      weightType weight;
    public:
      ZooAnimal (char*, cageType, int, weightType); // constructor function
      inline ~ZooAnimal () { delete [] name; }; // destructor function
      void changeWeight (weightType pounds);
      char* reptName ();
      weightType reptWeight ();
      int daysSinceLastWeighed (int today);
   };


3.  The class template ZooAnimal once again has only the single
template type parameter weightType.  Modify the declaration of object
bozo so that it is the declaration of a ZooAnimal object with template
type parameter weightType to be of type integer.

   template <class weightType>
   class ZooAnimal  
   {
    private:
      char *name;
      int cageNumber;
      int weightDate;
      weightType weight;
    public:
      ZooAnimal (char*, int, int, weightType); // constructor function
      inline ~ZooAnimal () { delete [] name; }; // destructor function
      void changeWeight (weightType pounds);
      char* reptName ();
      weightType reptWeight ();
      int daysSinceLastWeighed (int today);
   };

   // ---------- the constructor function
   template <class weightType>
   ZooAnimal<weightType>::ZooAnimal (char* who, int whichCage, int weighDay, 
                          weightType pounds)
   {
    char *name = new char[20];
    strcpy (name, who);
    cageNumber = whichCage;
    weightDate = weighDay;
    weight = pounds;
   }

   // ========== an application to use the ZooAnimal class
   void main ()
   {
    ZooAnimal bozo ("Bozo", 408, 1027, 400);
   }