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 subject
“DA2”.
- Your answers should consist of one file:
sarray.h, from Exercise A.
This file (or a single archive file containing it)
should be attached to your e-mail message.
- Be sure to include your name in your e-mail.
- I may not read your homework e-mail immediately.
If you wish to discuss the assignment (or anything else)
with me, send me a separate message with a different subject line.
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.
- Call your class template “SArray”, so that, for example, a SArray of doubles
would be declared as SArray<double>.
- Implement your class template in the file sarray.h.
Since this is a template, there should be no associated source file.
- Your class template should act as if it maintains a single array with a given size and value type.
- Include the following functions, and no others, in the public interface of your package:
- Default ctor: This should create an SArray of size 10.
All items in the array should be default-constructed.
- Copy ctor, copy assignment, dctor:
- The copy operations should create an entirely new copy of the data, so that modifying the copy does not change the original.
- Be sure that the dctor frees any dynamically allocated memory.
- 1-parameter ctor: parameter is a non-negative integer giving the number of items in the array.
Again, all items in the array should be default-constructed.
- Bracket operator: Given an integer subscript from 0 to size–1 (where size is the number of items in the array),
return a reference to the proper item.
- Member function size: no parameters. Returns the number of items in the array.
- Member function empty: no parameters. Returns a bool, which is true if the size of the array is zero.
- Member function begin: no parameters. Returns the address of item 0 in the array (think “iterator”).
- Member function end: no parameters. Returns the address of the item one-past the end of the array (think “iterator”).
- Operators “==” and “!=” two SArrays with the same value type
are equal if they have the same size and their corresponding items are all equal.
Two SArrays with different item value types cannot be compared.
- Include the following member types in your class:
- value_type: the type of each item in the array.
- size_type: the type of the size of an array and an index into an array.
Be sure to make a good choice for what this type is!
- A const SArray<T> should be one that does not allow modification of the items in its array.
A non-const SArray should allow such modification.
Hint: This has implications for how you implement functions begin and end, as well as the bracket operator.
- You may not use any C++ Standard Library classes in your implementation.
(You may use simple types, like std::size_t, and functions, like std::swap.)
- You may not create any new implicit type conversions.
Hint: 1-parameter constructors, “explicit”.
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.