Project #9 Solution

$30.00 $24.90



At this point you should have written code for a complete vector of integers class (DynArray in project #6). We will add a few additional functions and turn DynArray into a class template so it can store any type of sequence.


In this project you will test your ability to write class and function templates.

Step One

Add the following new functions to the DynArray class in project #8.
The function
int back()
This function returns the value of the last used integer in the vector, but it does not remove it. Throw a runtime_error if the vector is empty. This function returns by reference.
The function
int front()
This function returns the value at the beginning the vector, but it does not remove it. Throw a runtime_error if the vector is empty. This function returns by reference.
The function
This function replaces the at function from the previous assignment, and overloads the [ ] operator we use to index into an array. If you need help understanding how to do this, review the chapter on operator overloading in Big C++. Note that this function returns an item by reference.
The copy constructor, DynArray(const DynArray&), which does as deep copy when creating a new DynArray instance.
The assignment operator, DynArray& operator=(const DynArray&), which does a deep copy when assigning one DynArray instance to another.
After you have added these functions to your DynArray class, write a driver that tests these new functions. Once you are satisfied that your DynArray class works, move on to step two.

Step Two

After adding the required code to your DynArray class, it is time to turn it into a class template. You will need to change any occurrences of the token “int” in your code that refer to an item in the DynArray to a template parameter (usually T). All of your code must appear in a header file (DynArray.h; you will need to move functions from DynArray.cpp in project #8 into this header file. You will have no DynArray.cpp).

To get you started implementing the member functions as templates, here is code implementing the capacity function template:

template <typename T>
int DynArray<T>::capacity( ) const
return cap;

Adding the Driver

To test your code, add the driver located here to your project.

When you are satisfied that your program works correctly, format and document your code in accordance with the course style guidelines. Include a file prologue identifying you as the author. Submit your project using the instructions outlined in the Course Syllabus, Programming Projects section.

File(s) to Submit:

Place all of your source code files and an executable file into a zip file. Submit your zip file to Canvas as project #9.

Grading Criteria

Points possible
Your points
Your program meets the grading guidelines:

Source code files contain a declaration that you did not copy any code
Submitted to Canvas
Code meets style guidelines
Code is properly documented

Your DynArray class has been converted into a class template.


Member functions have been converted into function templates.


Your program works correctly with the included driver.


– Late Penalty (20% per day)