[Solved] C Project Note Maincpp Already Provided Project Cannot Edited Dynarrayh Needs Written Proj Q37165018
C++ Project
Note:
main.cpp is already provided with this project and cannot beedited
dynarray.h is what needs to be written
Project Objective:
Successfully implement a generic class using templates inC++.
Step 1
Starting with a copy of your DynArray class, add the followingmember functions:
- int& back()
This function returns the value of the last integer in the usedportion of the vector, but it does not remove it. Throw aruntime_errorif the vector is empty. This function returns byreference. - int& front()
This function returns the value at the beginning the vector, but itdoes not remove it. Throw a runtime_error if the vector is empty.This function returns by reference. - DynArray(const DynArray& origObject)
This copy constructor does a deep copy of the origObject’scontents. - DynArray& operator=(const DynArray& origObject)
This assignment operator does a deep copy of theorigObject’s contents. Don’t forget to delete the old data.
Also, change to at() function to return by reference(int&).
After you made these changes, write a driver that tests this newfunctionality. Once you are satisfied that your DynArray classworks, move on to step two. You won’t turn in your test code forthis step.
Step 2
Make your DynArray class generic by making it a template. Yournew class template will have the type of the items it will hold asthe template parameter:
template<typename T>class DynArray { … your content here …};
You will need to move all of your code into a single dynarray.hfile, since template code must be defined that way. You won’t havea dynarray.cpp. Place the class template definition first, followedby the member function implementations (alternatively, you coulddefine your member function bodies inside the class template if youlike). You will need to add the usual template preamble before eachfunction defined outside of the class template definition, forexample:
template<typename T>int DynArray<T>::capacity() const { return cap; // Or whatever you named your capacity data member}
Now go through your code and replace all occurrences of int withT wherever the element type is referred to. Don’t just blindlyreplace all occurrences of int. For example, capacity still returnsan int, since it is a number. push_back, however, will look likethis:
template<typename T>void DynArray<T>::push_back(const T& t) { …your code here…}
Note: Wherever DynArray appears in thenon-template version (except for the class name and the names ofconstructors), it must now appear as DynArray<T>.
The driver for this project is provided for you.
The expected output follows:
growgrowgrowcopy[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”, “copy”, and “assign”. This verifies that your code isfunctioning properly.
// main.cpp
#include <iostream>
#include “dynarray.h”
using namespace std;
int main( )
{
const char START = ‘A’;
const int MAX = 12;
// create a vector of chars
DynArray<char> 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<char> vectD2 = vectD;
// display the contents
cout << “n[“;
for (int i = 0; i < vectD2.size() – 1; i++)
{
cout << vectD2.at(i) <<“, “;
}
cout << “…, ” << vectD2.back()<< “]n”;
DynArray<char> vectD3;
vectD3 = vectD2;
cout << “n[“;
for (int i = 0; i < vectD3.size() – 1; i++)
{
cout << vectD3.at(i) <<“, “;
}
cout << “…, ” << vectD3.back() <<“]n”;
vectD3.front() = ‘{‘;
vectD3.back() = ‘}’;
cout << vectD3.front();
for (int i = 1; i < vectD3.size() – 2; i++)
{
cout << vectD3.at(i) <<“, “;
}
cout << vectD3.at(vectD3.size()-2) <<vectD3.back() << endl;
}
Expert Answer
Answer to C++ Project Note: main.cpp is already provided with this project and cannot be edited dynarray.h is what needs to be wri… . . .
OR

