Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Dynamic Array Template Step 1 Starting with a copy of your DynArray class, add the following member functions: int back() This function returns the

C++

Dynamic Array Template

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.

int& operator[](int n) This implements the index operator for your DynArray. It is almost the same as the .at() function. It will return a reference to the 'nth' element of the array.

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 step #1. You can test the class in Develop mode, however.

Step 2

You will now 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:

template class 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. 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:

template int 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 type of the element 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:

template void 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} 

main.cpp:

#include #include "dynarray.h" using namespace std; int main( ) { const char START = 'A'; const int MAX = 12; // create a vector of chars DynArray vectD; // push some values into the vector for (int i = 0; i < MAX; i++) { vectD.push_back(START + i); } // remove the last element vectD.pop_back(); // add another value vectD.push_back('Z'); // test memory management DynArray vectD2 = vectD; // display the contents cout << " ["; for (int i = 0; i < vectD2.size() - 1; i++) { cout << vectD2[i] << ", "; } cout << "..., " << vectD2.back() << "] "; DynArray vectD3; vectD3 = vectD2; cout << " ["; for (int i = 0; i < vectD3.size() - 1; i++) { cout << vectD3[i] << ", "; } cout << "..., " << vectD3.back() << "] "; vectD3.front() = '{'; vectD3.back() = '}'; cout << vectD3.front(); for (int i = 1; i < vectD3.size() - 2; i++) { cout << vectD3[i] << ", "; } cout << vectD3[vectD3.size()-2] << vectD3.back() << endl; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago