CS 311 Fall 2009
Assignment 2
Assignment 2 is due at 5 p.m. Thursday, September 24.
It is worth 25 points.
Procedures
This assignment is to be done individually.
E-mail
answers to the exercises below to
ffggc@uaf.edu,
using the subject
“DA2”.
- Your answers should consist of one file:
ksarray.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 (25 pts total)
Exercise A — “Kinda-Smart Array” Class
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
(for example, it knows its size, and has copy operations).
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
This assignment is to be done individually.
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, 3C, or 3D.
- Name your class template “KSArray”, so that, for example, a KSArray of doubles
would be declared as KSArray<double>.
- Implement your class template in the file ksarray.h.
Since everything in this file will be 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 KSArray 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.
- 2-parameter ctor: first parameter is a non-negative integer giving the number of items in the array, as above.
Second parameter is an item of the value type;
each item in the array should be set to this value.
- 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 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 KSArrays with the same value type
are equal if they have the same size and their corresponding items are all equal.
Two KSArrays with different value types cannot be compared.
- Operators “<”, “<=”, “>”, and “>=”.
As above, two KSArrays with different value types cannot be compared.
For two KSArrays with the same value type, this should be lexicographic order, which works like alphabetical order.
So, when comparring KSArrays a and b, if a[0] < b[0], then a < b.
If a[0] > b[0], then a > b.
Otherwise, we proceed to a[1] and b[1], and compare them similarly.
If each item in a is equivalent to the corresponding item in b,
and the size of a is less than that of b, then we should have a < b.
Lastly, all of these operators should be consistent,
so that, for example a > b precisely when b < a,
and a >= b when !(a < b).
- 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 KSArray<T> should be one that does not allow modification of the items in its array.
A non-const KSArray 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 or class templates in your implementation.
(You may use simple types, like std::size_t, and functions or function templates, like std::swap.)
- You may not create any new implicit type conversions.
Hint: 1-parameter constructors, “explicit”.
- Your code must compile with the test program—see below.
Example Code
Here is some code using a KSArray.
Some of it will not compile, as noted.
KSArray<int> ia(10); // array of 10 ints
KSArray<int> iax; // another array of 10 ints
KSArray<double> da(40, 3.2); // array of 40 doubles, each of which is set to 3.2
KSArray 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 KSArray<int> ia2(ia); // Copy constructor
if (ia2 == ia) // Condition should be true
cout << "Equal!" << endl;
KSArray<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:
ksarray_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 ksarray_test.cpp.