Question
13.4 Programming Project #8: Dynamic Array Template Background At this point you should have written code for a complete vector of integers class (The Dynamic
13.4 Programming Project #8: Dynamic Array Template
Background
At this point you should have written code for a complete vector of integers class (The Dynamic Array project). We will add a few additional functions to DynArray, and then make it a class template so it can store objects of any type.
Objective
Successfully implement a generic class using templates in C++.
Step 1
Starting with a copy of your DynArray class, add the following member functions:
int back() This function returns the value of the last integer in the used portion of the vector, but it does not remove it. Throw a runtime_error if the vector is empty. This function returns by reference.
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.
DynArray(const DynArray&) This copy constructor does a deep copy of the object's contents.
DynArray& operator=(const DynArray&) This assignment operator does a deep copy of the object's contents. Don't forget to delete the old data.
After you have added these functions to your new DynArray class, write a driver that tests these new functions. Once you are satisfied that your DynArray class works, move on to step two. You won't turn in your test code for this step.
Step 2
Make your DynArray class generic by making it a template. Your new class template will have the type of the items it will hold as the template parameter:
templateclass DynArray { ... your content here ... };
You will need to move all of your code into a single dynarray.h file, since template code must be defined that way. You won't have a dynarray.cpp. Place the class template definition first, followed by the member function implementations (alternatively, you could define your member function bodies inside the class template if you like). You will need to add the usual template preamble before each function defined outside of the class template definition, for example:
templateint DynArray ::capacity() const { return cap; // Or whatever you named your capacity data member }
Now go through your code and replace all occurrences of int with T wherever the element type is referred to. Don't just blindly replace all occurrences of int. For example, capacity still returns an int, since it is a number. push_back, however, will look like this:
templatevoid DynArray ::push_back(const T& t) { ...your code here... }
The driver for this project is provided for you.
The expected output follows:
grow grow grow copy [A, B, C, D, E, F, G, H, I, J, K, ..., Z] assign [A, B, C, D, E, F, G, H, I, J, K, ..., Z] {B, C, D, E, F, G, H, I, J, K}
Note that you must insert trace statements for "grow" and "assign". This verifies that your code is functioning properly.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started