TITLE:

Identifying Template Class Parameter Requirements

RESOURCE CONTACT:

Martin L. Barrett, East Tennessee State University, barrettm@etsu-tn.edu

GOAL:

To teach students to: 1) identify the requirements placed on a class that will be used as the argument to a template class 2) evaluate existing classes against those requirements 3) modify existing classes to meet the requirements.

AUDIENCE:

Students at the end of a comprehensive CS1 course or early in a CS2 course, using C++ (but can be adapted to other OO languages that support templates).

PREREQUISITES:

Basic OO material in C++: classes, methods, members, overloaded operators, and an introduction to templates and containers.

MOTIVATION:

Object oriented design can take good advantage of template classes to simplify the design of data structures. While the design of a template class itself is a more advanced topic, the use of templates is fairly straightforward and can be used as a tool to produce solutions to complicated problems. Using a template correctly involves, among other things, evaluating a candidate class that will be substituted into the template against a set of requirements placed on it by the template's code. Those requirements are often implicit and poorly documented, so identifying them is a necessary skill prerequisted to using the template. A second skill is deciding whether a candidate class meets the requirements, which usually involve member functions and overloaded operators. Finally, if a candidate fails to meet all requirements, it is necessary to modify the candidate class to add the necessary functionality.

APPLICABILITY:

While the range of applicability of this module is narrow, it does provide an introduction to the use of template classes, and it can be extended to include the use of the Standard Template Library. It is appropriate for use towards the end of a comprehensive CS1 course that covers templates. Its more likely use will be in a CS2 course that covers templates in the context of data structures and particularly data structures. In addition, it provides practice in code reading, reuse, and identification of module interaction.

STRUCTURE:

0. Introduction to template classes and functions - may be part of material covered previous to this module, or included as part of it.

1. Examination of an existing simple template class (for example, a template array class). This class should use just a few obvious member functions of the parameter class. Students as a group, with the instructor's guidance, review the implementation of a template class. Each instance of the template class parameter is identified and classified by usage. All parameter class member functions and operators (and possibly, though not likely, member data) are listed. This list is the requirements that any candidate class to be substituted for the parameter class must meet.

2. An existing candidate class (for example, the standard string class) is examined to see if the requirements are met by that class. A class that meets the requirements should be used first. Repeat using another class that does not meet the requirements (for example, a user developed employee class) and identify the missing member functions and operators.

3. Discuss how the candidate class can be modified to add the missing class behaviors. If inheritance has already been covered, subclassing is a possible strategy; if not, then actual modification of the existing class and wrapper classes are possible strategies. In each case, discuss the implications on reuse, maintenance, and design of each possible solution.

4. Repeat the exercise with a more complicated template class that includes more subtle parameter class member functions and operators, such as a copy constructor, assignment operator, and overloaded display operator. Discuss ways to document the requirements of a template class that make it easier to reuse.

5. For a CS2 course only, have students design a container class and at least two candidate parameter classes that meet the container's requirements for a parameter.

6. For a CS2 course only, introduce the Standard Template Library. Using its documentation, not its implementation, discuss the requirements on candidate parameter classes.

CONSEQUENCES:

Using this resource, a student will be able to: identify the member functions and operators required by a template class parameter; determine whether a candidate parameter class has the correct member functions and operators to satisfy those requirements; modify an existing candidate class or create a new candidate class that does satisfy the requirements.

IMPLEMENTATION:

This resource can be implemented using large group interaction, small group interaction, or as an individual assignment. Initial training is probably best done in a large group setting. The activity would require approximately one class period, plus time out of class to complete a corresponding assignment.

RELATED RESOURCES:

None.

EXAMPLE INSTANCES:

See the STRUCTURE section above.

REFERENCES:

Cohoon, James, and Davidson, Jack, C++ Program Design, Irwin.

Musser, David, and Saini, Atul, STL Tutorial and Reference Guide, Addison-Wesley.

Weiss, Mark Allen, Algorithms, Data Structures, and Problem Solving With C++, Addison-Wesley.