CS 342 Lab 2: Design and Implementation of an Array Class

 

http://classes.cec.wustl.edu/~cs342/current-semester/lab/lab2/

Lab date: 25 January - 3 February 2000
Due date: Sunday 6 February 2000 11:59 pm

Objective:

A simple, dynamic, bounds-checking array class will be designed in this lab. You will have an opportunity to use parameterized types, i.e., template classes and overloaded operators, explicitly manage memory, and incorporate checking for error conditions.

Preparation:

  1. Review the header file for the Array class (Array.h) that is part of the distribution for this lab. Do not vary from this specification.

  2. Review Templates:

Assignment:

Given a specification for an Array template class, write the methods and operators for that class. The class represents an ADT with operations such as get, set, and size. The array will be different from C++ builtin arrays in two ways:

  1. The initial size of the array can be a run-time parameter. The C++ operators new and delete will be used.

  2. Set/get operations on the array will be range-checked. If a get or set operation is attempted on the array which is out of bounds, the operation returns -1. (The overloaded operator[] operations are not range-checked.)

Our array implementation will require the use of C++ templates, and checking for error conditions:

  1. Templates (parameterized types) - this will allow the user of the array to specify the type of the objects held in the array.

  2. Errors can arise for two notable reasons:

    1. Array out of bounds
    2. Out of memory

    Your implementation needs to check for these conditions, and take appropriate action.

Obtaining the Lab 2 distribution:

At a Unix shell prompt, type ~cs342/bin/lab2. That will copy several files to a new lab2 subdirectory. You can cd lab2 and enter make to compile the code, if you like. However, it will not link until your implementation is filled in.

NOTE: you can use the distribution mechanism to revert one or more of your files at any time. First, delete the file (or better, mv it to a file with a different name, such as Array.cc.BAD). Then, run cvs update to update your workspace. That will replace any missing files. It will also let you know of any files that you modified but did not delete. cvs is in pkg gnu, in case you don't find it at first.

An Array.cc file is included which shows an implementation of the copy constructor. Use this as a model for implementing the rest of the assignment.

What to Submit:

By the due date, submit all source files from your final solution. Include a laboratory writeup file (named readme) documenting what you did to satisfy this assignment. Specifically mention what the most troublesome parts were. These files will be submitted automatically when you execute the command:
make turnin
NOTE: there is a Makefile target that allows you to test what you are going to turn in:
make test_turnin

It places the output that will be turned in into the TEST_TURNIN directory. Please use make test_turnin, and verify that the files that you will submit are correct.


Hints on Compiling Templates:

  1. Some compilers do not handle templates very well. If you have what you believe to be correct code which is generating link errors complaining about missing operators or functions which you know to be defined, try
    make clean
    before re-making.

  2. Some compilers, such as g++, require that the implementation code for templates be available in the code that uses the parameterized type. That's why the Array.h file #includes the Array.cc implementation file.

  3. Some compilers require that a separate implementation file explicitly instantiate templatized code. If you use Sun CC, g++ (pkgadd g++), or egcs' g++ (pkgadd egcs), you shouldn't have to worry about this.

  4. Consult references for your specific compiler, or ask for advice.

  5. If you'd like to explore the various template instantiation approaches, this is a good example to experiment with. The textbook discusses these approaches near the end of Chapter 18. Here are the compiler options and minor code changes to select the instantiation strategies. For explicit instantiation, you'll have to add the explicit instantiations to your main.cc, e.g.,
    
    template class Array<char>;
        

    If you run nm -C on your executable, you'll see the template instantiations.

    Compiler Automatic instantiation Implicit instantiation Explicit instantiation
      Easy, but slow
    and sometimes fragile
    Easiest, but largest code size
    and non-unique static data members!
    Usually requires #include of .cpp
    in template header file.
    Gives programmer control of instantiations.
    Usually requires #include of .cpp
    in template header file.
    Sun CC 4.2 default -instances=static -instances=explicit
    g++ 2.8.1 (pkgadd gnu) not available default -fno-implicit-templates
    egcs (pkgadd gnu_199909) -frepo default -fno-implicit-templates

CS 342 home page