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.
- Your answers must consist of
the source code for Exercise A
(file
msarray.hpp
). Attach this file to your homework submission. - Send only the above! I do not want project files or executables.
- I may not look at your homework submission immediately. If you have questions, e-mail me.
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.
- Name your class template
MSArray
, so that, for example, an object of typeMSArray
whose items have typedouble
would be declared asMSArray<double>
. - Implement your class template in the file
msarray.hpp
. Since everything in this file will be a template, there is no associated source file. - Your class template acts 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 creates an object
of type
MSArray
with size 8. All items in the array are default-constructed. - Dctor, copy ctor, move ctor, copy assignment, move assignment:
- Be sure that the dctor frees any dynamically allocated memory.
- Each copy operation creates an entirely new copy of the data, so that modifying the copy does not change the original.
- Write these functions as described in the slides (Invisible Functions II).
- 1-parameter ctor: parameter is a non-negative integer giving the number of items in the array. All items in the array are 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 will 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 “!=
”. TwoMSArray
objects with the same value type are equal if they have the same size and their corresponding items are all equal. TwoMSArray
objects with different value types cannot be compared. - Operators “
<
”, “<=
”, “>
”, and “>=
”. As above, twoMSArray
objects with different value types cannot be compared. For twoMSArray
objects with the same value type, these operators use lexicographic order, which works like alphabetical order. So, when comparingMSArray
objectsa
andb
, ifa[0] < b[0]
, thena < b
. Ifa[0] > b[0]
, thena > b
. Otherwise, we proceed toa[1]
andb[1]
, and compare them similarly. If each item ina
is equivalent to the corresponding item inb
, and the size ofa
is less than that ofb
, then we havea < b
. Lastly, all of these operators are consistent, so that, for example,a > b
precisely whenb < a
, anda >= b
when!(a < b)
.
- Default ctor: This creates an object
of type
- Include the following member types, and no others,
in the public interface of your package:
value_type
: the type of each item in the array.size_type
: the type of the size of an array and also of an index into an array. Be sure to make a good choice for what this type is!
- A
const MSArray<T>
does not allow modification of the items in its array. A non-constMSArray
allows such modification. Hint: This has implications for how you implement functionsbegin
andend
, as well as the bracket operator. - The
==
and!=
operators forMSArray
are usable for value types that do not define any comparison operators other thanoperator==
andoperator!=
. - The
<
,>
,<=
, and>=
operators forMSArray
is usable for value types that do not define any comparison operators other thanoperator<
. - 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, likestd::swap
,std::copy
,std::equal
,std::sort
, etc.) - You may not create any new implicit type conversions.
Hint: 1-parameter constructors,
“
explicit
”.
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
- Once again, this is to be high-quality code.
- The requirements above specify what is to be in the public interface of the package. Nothing is said about private functions. You may write any private functions you want. However, these still must follow the coding standards, including standards 4A and 4B.