CS 12 -- Lab 8

Time to get familiar with templates and operator overloading!


Making a template Array class

Copy the following file from my public directory:

~sfkaplan/public/cs12/useArray.cc

Much like last week's useIntArray.cc, the code in this file relies on an Array template class that allows it to create an array of int. The Array object does automatic bounds checking.

Modify last week's IntArray class to write your own Array class. Note that because of the way the C++ compiler works -- it only knows which type to plug into the template when some other module uses the class -- the entire Array class must be implemented in the Array.hh file. Consequently, it is only compiled by virtue of a #include into some other file (in this case, useArray.cc.


Altering Array to overload operator[]

Now copy this file from my public directory:

~sfkaplan/public/cs12/useArray2.cc

Rather than call the Array::get() and Array::assign() methods, this class uses the [] operator to index into the Array object. Note that the code has a pointer to an Array object, and that pointer must be dereferenced before operator[] can be used. Feel free to alter how the Array object is allocated and passed to avoid this syntactic ugliness; that is, it's not required, but feel free to change this aspect of useArray2.cc itself.

Your primary goal here is to write the Array::operator[]() method such that useArray2.cc works. You should add to your Array class -- do not remove Array::get() and Array::assign().


Adding operator= and operator+ to Array

Copy one more file from my public directory:

~sfkaplan/public/cs12/useArray3.cc

This program does some new things with the Array class. Specifically, it creates two such objects, assigns values into them, and then does the following:

x = x + y;

The + operator is intended to concatenate the contents of the two Array objects. Note that the right-hand side of this assignment will be translated by the compiler as:

x.operator+(y);

Therefore, the first object should create a new Array object, copy its own contents into it, and also copy the contents of the second Array object into it as well. It should then return the new object.

The = operator performs an assignment. The object named on its left is the one on which the operator=() method is invoked, and the object named on the right is passed as an argument. In fact, the whole statement gets translated like this:

x.operator=(x.operator+(y));

That is, the thing returned by the concatenation (the new object mentioned above) is passed as an argument to operator=(). The job of this method is to make a copy of the object. That is, the object named by x should take on a copy of the values in the object returned by operator+().

Your goal is to add again to the Array class to support these two new operators. Again, do not write a new class -- just add to the existing one.


What you must submit

Submit your final Array.hh file using the cs12-submit command. Submit this assignment as lab-8.


This assignment is due on Tuesday, November 18th, at 11:59 pm.

Scott F. Kaplan
Last modified: Sun Nov 5 22:48:52 EST 2000