CS 311 Fall 2024  >  Assignment 2


CS 311 Fall 2024
Assignment 2

Assignment 2 is due at 5 pm Thursday, September 19. It is worth 65 points.

Procedures

This assignment is to be done individually.

Turn in answers to the exercises below on the UA Canvas site, under Assignment 2 for this class.

Exercises (65 pts total)

Exercise A — Moderately Smart Array Class Template

Purpose

In this exercise, you will write an array class template. It will not be quite as “smart” as it could be (std::vector is much smarter), but it will be much 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 that ownership and related issues are properly handled and also documented, where required.

And as before, make your code high quality. In particular, follow the applicable coding standards.

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 is specified by the client code.

Be sure to follow the coding standards. Standards 4A and 4B now apply. You do not yet need to follow standards 4C, 4D, or 4E.

Example Code

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

MSArray<int> ia(8);           // Array of 8 ints
MSArray<int> iax;             // Another array of 8 ints
MSArray<double> da(40, 3.2);  // Array of 40 doubles; each is set to 3.2
MSArray x(8);                 // DOES NOT COMPILE; no template parameter

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

// Range-based for-loop calls MSArray member functions begin, end
for (auto x : ia)
{
    cout << "Item :" << x << endl;
}

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

MSArray<double> da2;
da2 = da;                     // Copy assignment
da2 = ia;                     // DOES NOT COMPILE; different value types

if (da == ia)                 // DOES NOT COMPILE; different value types
    cout << "blah blah" << endl;

Test Program

A test program is available: msarray_test.cpp. If you compile and run the test program (unmodified!) with your code, then it will test whether your code works properly.

The test program requires doctest.h, the header for the doctest unit-testing framework, version 2.

Do not turn in the test program or the doctest framework.

Note. As before, the test program does not check all of the requirements of the assignment. Some of these cannot be checked by a test program.

Thoughts