Cay Horstmann & Timothy Budd

Laboratory Notebook
Chapter 22 – Templates

Your name:

Your email address:

Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

Lab Objectives

To gain experience with

        using and defining template functions

        using and defining template classes

        understanding the relationship between template functions and classes and ordinary functions and classes

        learning how to use typedef statements to create alias names for long type names

        understanding the interactions between template classes, inheritance, and static data fields

        Learning how policy can be established by the selection of template arguments


P1. Oceanic Military Defense


You are in charge of a new oceanic missile defense system. As such, you decide that you require a small program for determining the amount of time it takes for a missile to reach its target. Suppose an enemy submarine is a certain distance from your ocean-bottom military platform. Also, suppose that in response to this threat you fire a missile, with a certain speed, on a straight line trajectory from this platform towards the oncoming danger. Your program should compute the time it takes for your missile to reach the enemy submarine provided that the velocity of the missile is slowly reduced by water resistance at a rate of three times Earth’s surface gravitational pull, or approximately 3*9.81 m/s/s. Assume that the density of the missile is exactly crafted to the density of sea water such that it can be considered weightless. Hence, the problem becomes a linear, one-dimensional problem.


The position of the missile, s, can be computed at each time interval, delta_t, through the formula s = s + v * delta_t where v is the velocity of the missile. The velocity must be updated iteratively to take into account the dragging force of the water. Use the formula v = v – g * delta_t where g is 3 * 9.81. The values s, v, delta_t, and g should all be double values as this is a one-dimensional problem.


Your program should consist of a main function that calls a function taking v, g, and delta_t as parameters. The function will return a double value indicating the time to impact. The function definition is provided below.


double computeTime(double v, double g, double delta_t);



Submit all of the code for your answer in the space below.


R1. Template Functions


Because you do not want to keep repeatedly writing functions, you decide to modify your computeTime() function such that it utilizes templates. Modify the computeTime() function to accept any type of object for input arguments v and g. Assume for now that any object passed to the function will implement the appropriate multiplication and addition operators. Therefore, you will not need to modify the body of your function except to add template functionality. Use the definition provided below.


template<typename T>

double computeTime(const T& v, const T& g, double delta_t)


// Insert code here



Submit all code in the space below.


P2. Land Defense


Suppose that your superiors are so impressed with your oceanic defense program that they want you to implement a similar defense system on land. Such an application is more complex as the missile’s movement must be charted in two dimensions instead of just one. Therefore, for this application, you will implement a class called Vector2D. Assume that you are provided with the initial velocity and angle required to hit your target. Your program should compute the time required to hit as was done in Problem P1. The main program from Problem P1 should be duplicated exactly, except you will need to call a modified function that accepts Vector2D arguments v and g. The body of the computeTime() function should not change from that in Problem P1. The definition for Vector2D as well as the computeTime() function is provided below.


class Vector2D


Vector2D(double ix, double iy);

Vector2D operator*(double right);

Vector2D operator+(const Vector2D& right);


double x;

double y;



double computeTime(const Vector2D& v, const Vector2D& g, double delta_t, Vector2D distance_to_target);

Provide all code in the space below.


R2. Template Classes

Suppose that the Vector2D class written in Problem P2 might be used in multiple applications. It would therefore be useful for this class to hold values other than type double. Modify the Vector2D class such that it is a template class. A single template parameter should determine the type of the encapsulated variables. Ensure that you modify the entire class as necessary to ensure its continued functionality.

Provide your answer in the space below.


P3. Templates Functions


Write a main() function to test your computeTime() function for both double, Vector2D<float>, and Vector2D<double> variables types.


Insert all code in the space below.


R3. Non-Type Template Arguments


Sometimes it is useful to identify parameters using templates for reasons other than specifying variable types. Suppose that you require a video sequence of your missile’s flight. Also, suppose that prewritten code periodically returns grayscale snapshots of the missile as a low-level 2D array of integer values. You wish to encapsulate a 3D array in which to store each snapshot so as to create a video sequence. Define and implement a simple class for encapsulating a three dimensional low-level array. All three dimensions should be specified using template arguments. Implement a default constructor that sets each element of the array to zero and a member function for modifying a single value within the array. The class definition is provided below.


template <int numFrames, int numRows, int numCols>

class VideoSequence




SetPixel(int frame, int row, int col, int pixel);


int sequence[numFrames][numRows][numCols];



Provide your answer in the space below.


P4. Template Functions Continued


You no longer want the arguments v and g to the template function to implement both the addition and multiplication operators. Therefore, add two more template arguments to your template function so that each operation may be specified. Implement the classes below so that the appropriate function objects can be used as the parameters to the template function whenever either a double or Vector2D is passed as an argument.


template<typename T, typename SCALMULT, typename ADD>

double computeTime(const T& v, T g, double delta_t, T origin, T distance_to_target, SCALMULT mult, ADD add)


// Insert code here



class Vector2DMultiplication



Vector2D<double> operator() (const Vector2D<double>& a, double b);



class Vector2DAddition



Vector2D operator() (const Vector2D<double>& a,

const Vector2D<double>& b);



class DoubleMultiplication


double operator() (double left, double right);



class DoubleAddition


double operator() (double left, double right);


Submit code for computeTime() in the space below.


Submit all code for Vector2DMultiplication in the space below.



Supply all code for Vector2DAddition in the space below.



Supply all code for doubleAddition in the space below.



Supply all code for doubleMultiplication in the space below.




R4. Typedef

Re-write your main() function using typedef statements to define aliases for your template class declarations. See page 804 for assistance or syntax help.

Do not forget to send your answers when you are finished.