The example program named TEMPLAT2.CPP is a little more involved since it provides a template for an entire class rather than a single function.

The template code is given in lines 6 through 16 and a little study will show that this is an entire class definition. The diligent student will recognize that this is a very weak stack class since there is nothing to prevent popping data from an empty stack, and there is no indication of a full stack. Our intent, however, is to illustrate the use of the parameterized type and to do so using the simplest class possible.

In the main program we create an object named int_stack in line 25 which will be a stack designed to store integers, and another object named float_stack in line 26 which is designed to store float type values. In both cases, we enclose the type we desire this object to work with in “<>” brackets, and the system creates the object by first replacing all instances of ANY_TYPE with the desired type, then creating the object of that type. You will note that any type can be used that has an assignment capability since lines 13 and 14 use the assignment operator on the parameterized type.

Even though the strings are all of differing lengths, we can even use the stack to store a stack of strings if we only store a pointer to the strings and not the entire string. This is illustrated in the object named string_stack declared in line 27 and used later in the program.

This program should be fairly easy for you to follow if you spend a bit of time studying it. You should compile and run it if you have a compiler that will handle this new construct.


The program named TEMPLAT3.CPP uses the same class with the template as defined in the last program but in this case, it uses the date class developed earlier as the stack members. More specifically, it uses a pointer to the date class as the stack member.

Because class assignment is legal, you could also store the actual class in the stack rather than just the pointer to it. To do so however, would be very inefficient since the entire class would be copied into the stack each time it is pushed and the entire class would be copied out again when it was popped. Use of the pointer is a little more general, so it was illustrated here for your benefit.

All three of the previous programs can be compiled and executed if you have a copy of Borland C++ version 3.0. Other compilers may not work with these programs since parameterized types are not yet a part of the C++ specification.


A future version of C++ will have some form of exception handling to allow the programmer to trap errors and prevent the system from completely shutting down when a fatal error occurs. The Ada language allows the programmer to trap any error that occurs, even system errors, execute some recovery code, and continue on with the program execution in a very well defined way. Bjarne Stroustrup, working in conjunction with the ANSI-C++ committee, has announced that some form of exception handling will be implemented but he has not stated what form it would take as of this writing.