CS 311 Spring 2008  >  Assignment 2

CS 311 Spring 2008
Assignment 2

Assignment 2 is due at 5 p.m. Thursday, February 14. It is worth 20 points.

Procedures

E-mail answers to the exercises below to ffggc@uaf.edu, using the subjectDA2”.

Exercises (20 pts total)

Exercise A — “Smart-ish Array” Class Template

Purpose

In this exercise, you will write a simple class that manages an array. It will not be quite as “smart” as it could be (std::vector is much smarter), but it should be better than a built-in C++ array.

Key to this assignment is proper management of a resource in a class. Be sure you have no resource leaks, and ownership and related issues are properly documented.

And as before, make your code high quality.

Instructions

Implement a C++ class template that manages and allows access to a fixed-size array. The type of item in the array and the number of items in the array should be specified by the client. Be sure to follow the coding standards. Standard 3A (“Requirements on template parameter types must be documented.”) now applies. You do not need to follow standards 3B or 3C.

Example Code

Here is some code using a SArray. Some of it will not compile, as noted.

SArray<int> ia(20);         // array of 20 ints
SArray<int> iax;            // array of 10 ints
SArray<double> da(40);      // array of 40 doubles
SArray x(10);               // WILL NOT COMPILE; no template parameter

// Set all items (counter loop)
for (int c = 0; c < ia.size(); ++c)
{
    ia[c] = c * c;
}

// Print all items (iterator loop)
const int * iter;
for (iter = ia.begin(); iter != ia.end(); ++iter)
{
    cout << "Item :" << *iter << endl;
}

const SArray<int> ia2(ia);   // Copy constructor
if (ia2 == ia)                // Condition should be true
    cout << "Equal!" << endl;

SArray<double> da2;
da2 = da;                // Copy assignment
da2 = ia;                // WILL NOT COMPILE; different types

if (da == ia)            // WILL NOT COMPILE; different types
    cout << "ZZZ" << endl;

Test Program

I have written a test program: sarray_test.cpp. If you compile and run your package with this program (unmodified!), then it will test whether your package works properly.

Do not turn in sarray_test.cpp.


CS 311 Spring 2008: Assignment 2 / Updated: 9 Feb 2008 / Glenn G. Chappell / ffggc@uaf.edu Valid HTML 4.01!