Assignment 2, C++ Intermediate - Template Exercises


This page last updated on: Apr. 22, 2002


Contents:


Due Date

Apr. 29, 2002 by end of day

Description

Factorial Function

Part 1: Convert the following function into a template function which can take any type:

unsigned long fact (unsigned long n) {
   return (n < 2 ? 1 : n * fact(n-1));
}

Document the operations that are required on the templatized type.

Part 2 (optional): Convert the templatized function to be a compile time computational function instead of a run-time computational function. Discuss tradeoffs between the two approaches.

Stack Class Declaration

Part 1: Convert the following class declaration into a template class declaration (named Stack) which can contain any type. Implement the Count method outside of the class declaration. Document the requirements on the templatized type (in particular, what is the requirement on a type when an array of the objects is new'ed?):

class Employee { /* ... */ };

class EmpStack {
public:
   EmpStack ();
   ~EmpStack ();
   EmpStack (EmpStack const &);
   EmpStack & operator=(EmpStack const &);

   unsigned int Count() const; // # of Employee's in the stack
   void     Push(Employee const &);
   Employee Pop();

private:
   Employee*     mEmps;  // pointer to a memory area big enough
                         // for 'mSize' Employee objects
   unsigned int  mSize;  // the size of the 'mEmps' area
   unsigned int  mUsed;  // the number of Employee's actually
                         // used in the 'mEmps' area
};

Part 2: Specialize the Stack declaration for a Vehicle class.

Part 3: Add a second template parameter which specifies the data structure to be used in the template Stack class. The original code above uses a pointer to an array that is new'ed from the heap - change this to be container based. This 'storage policy' class should be defaulted to a standard library container class. Document the requirements on the 'storage policy' class.

Distance Calculation Function

Part 1 (optional): Convert the following function into a template function:

#include <cmath>
double dist_lat_long (double lat1, double long1, double lat2, double long2)
{

   using std::acos;
   using std::sin;
   using std::cos;

   double const Pi (3.14159265358979323846264);
   double const DegToRad (180.0 / Pi);
   double const EarthRadius (6378.0); // for distance in kilometers

   double const lat1Rads (lat1 / DegToRad);
   double const lat2Rads (lat2 / DegToRad);

   return acos(sin(lat1Rads)*sin(lat2Rads)+cos(lat1Rads)*cos(lat2Rads)*
      cos(long2/DegToRad - long1/DegToRad)) * EarthRadius;

}

Part 2 (optional): There are three constants in the function that could be further genericized (instead of always being type double). Create a generic class / struct declaration for the three constants, then specialize them for common types. Modify the distance function to use the generic constant declarations. An example general declaration is:

template <typename T>
struct Constants {
   static T const pi();
   static T const deg_to_rad();
   static T const earth_radius_k();
};

An example specialization is:

template <>
struct Constants <long double> {
   static long double const pi() { return 3.141592653589793238462643383279502884197L; }
   static long double const deg_to_rad() { return 180.0L / pi(); }
   static long double const earth_radius_k() { return 6378.0L; }
};

Topics Learned

Template functions, template classes, non-class template parameters, template specialization, traits classes.


This page constructed by Cliff Green, Copyright © 2002.