Lab 1 - Template Classes and Dynamic Arrays

Due: Today at 5:00pm
This lab is worth 10 points.

Log in to Helios and create a new subdirectory for this lab. Copy the following files to your current directory:

Compile the code with the command g++ -o lab1 lab1main.cpp and run the code with the command lab1.

The genericlist.cpp file contains a template class that allocates a dynamic array based on the data passed to the constructor. Pay special attention to the syntax above the template class. This is required whenever you wish to have a friend function of a template class. In this case, the friend function is the output operator which prints all of the elements to the given output stream. The report() function on the other hand prints the array pointer address and the size of the array to standard out.

Even when the array is cloned to a second array, note that their array addresses are different. This means the clone is a copy of the original; they do not both use the same chunk of memory. You must define a copy constructor and assignment operator to see this behavior. If they are not defined, then the clone's array address would be the same as the original's. This is not desirable because if the original is destructed before the clone (or vis versa), the delete operation in the destructor would free the memory for use by other programs even though there is still an object using that memory.

In the lab1main.cpp file, the pretty_print() function wraps around the output operator to create a nicer looking output. The double version sets the precision to two decimal places while the template version adds a tab before the output.

Lab Assignment

Add a character version of the prett_print() function to lab1main.cpp that adds a string of decorative characters (such as ***** or +++++) before and after printing the elements. Email the new version of lab1main.cpp to me by 5pm.