CS12, Program 4

 

Assigned: February 26th, 2002

 

Due: March 4th, 2002, 11pm

 

 

 

Topics

 

·      Template classes

 

 

The Program

 

The array class that you wrote in program 3 was only good for storing doubles.  In this program, you will use templates to extend the array class to store any data type.

 

After you write your template class, you will need to test it. Write a main() that will instantiate two different Array objects with different data types, assign them values and print them out. 

 

Please keep in mind the following points:

·      The changes to the program are minimal, and the test program is very simple, but don’t let that fool you into leaving things for the last minute.  You will spend most of your time on trying to get the program to compile.  This could be a frustrating, head-banging time.  Recall that g++ requires the template code to be in the same file as the code that uses it.  Please put your entire class (header and implementation) into the .h file.  That way, when you include the header file in your main, you are in effect placing the implementation code in the same file as the main. 

·      This program builds on program 3, so if program 3 did not work, you have a bit of a problem.  However, this program does not require all the methods from program 3.  In fact, you can still get full credit for this program with only the [] operator working. 

 

To Do

 

1.    Rework the DArray class into a class template called Array.

 

2.    Write a program that tests your class template by doing the following:

·      Declare an Array object for storing doubles

·      Store some doubles and print the Array

·      Declare an Array object for storing chars

·      Store some chars and print the Array

 

 

The Input

 

None.  Please hardwire the values into your main.

 

 

The Output

 

The output should be similar to this example (values will differ). 

 

Contents of double array:

3.14 5.123 2.718 0

 

Contents of char array:

h e l l o

 

 

What to turn in

 

·      arraytemplate.h

 

·      main.cc

 

 

Please note:

 

·      Do not use any other names for your files.  These names are case sensitive – do not use capital letters anywhere in the file names.  Failure to name your files as above will cost you 10 points. 

 

·      Please make sure that both arraytemplate.h and main.cc includes your name, login and lab section.  Each is worth 8 points on this assignment.

 

 

Extra Credit

 

·      Test your template class’s ability to store user-defined objects: Counter, Purse and DArray objects.