Question
C++ Add aditional functions and convert a class into a class template. Take the included .h file and add some additional functions and conver to
C++ Add aditional functions and convert a class into a class template.
Take the included .h file and add some additional functions and conver to a class template.
.h
#include
#include
#include
using namespace std;
class DynArray
{
int *head;
int *newHead;
int cap;
int si;
void resizeArray();
public:
DynArray();
DynArray(int);
~DynArray(); //destructor to free memory
void push_back(int);
void pop_back(int);
int size();
int capacity();
void clear();
int at(int);
};
int DynArray::at(int pos)
{
if (pos < 0 || pos >= si)
{
throw runtime_error(" Given index is not a valid index");
return -1;
}
int temp = head[pos];
return temp;
}
void DynArray::resizeArray()
{
newHead = (int*)realloc(head, cap * 1.5 * sizeof(int*));
for (int i = 0; i
{
int v = *(head + i);
newHead[i] = v;
}
cap *= 1.5;
head = newHead;
}
DynArray::DynArray()
{
head = (int*)malloc(2 * sizeof(int));
si = 0;
cap = 2;
for (int i = 0; i< cap; i++)
head[i] = 0;
}
DynArray::DynArray(int n)
{
head = new int[n];
si = 0;
cap = n;
for (int i = 0; i< cap; i++)
head[i] = 0;
}
int DynArray::size()
{
return si;
}
int DynArray::capacity()
{
return cap;
}
void DynArray::clear()
{
delete[] head;
head = (int*)malloc(2 * sizeof(int));
si = 0;
cap = 2;
}
DynArray::~DynArray()
{
free(head);
delete head;
}
/*
* Function that adds the given value to the front of the array
*/
void DynArray::push_back(int val)
{
if (si == cap)
{
resizeArray();
head[si++] = val;
}
else
{
head[si++] = val;
}
}
void DynArray::pop_back(int val)
{
si--;
}
Step One
Add the following new functions to the DynArray class in project #8.
1. 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.
2. 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.
3. The function operator[](int). This function 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.
4. DynArray(const DynArray&). This is the copy constructor, which allocates new heap space for the new object it is copying from the object passed as its parameter.
5. DynArray& operator=(const DynArray&). This is the assignment opertator, which allocates new heap space for the new object it is copying from the object passed as its parameter. It also deletes its old heap space.
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 vector 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
The updated code must work with the below driver
#include
#include "DynArray.h"
using namespace std;
int main( )
{
const char START = 'A';
const int MAX = 12;
// create a vector of doubles
DynArray
// 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
// display the contents
cout << " [";
for (int i = 0; i < vectD2.size() - 1; i++)
{
cout << vectD2[i] << ", ";
}
cout << "..., " << vectD2.last() << "] ";
cin.get();
}
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