C++ Add aditional functions and convert a class into a class template. Take the
ID: 3822063 • Letter: C
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 a class template.
.h
#include <cstdlib>
#include <iostream>
#include<stdlib.h>
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 <si; 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 <typename T>
int DynArray<T>::capacity( ) const
{
return cap;
}
The updated code must work with the below driver
#include <iostream>
#include "DynArray.h"
using namespace std;
int main( )
{
const char START = 'A';
const int MAX = 12;
// create a vector of doubles
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 << " [";
for (int i = 0; i < vectD2.size() - 1; i++)
{
cout << vectD2[i] << ", ";
}
cout << "..., " << vectD2.last() << "] ";
cin.get();
}
Explanation / Answer
The functions back() , front() that needs to be added in the first requirement won't work for the second. So i am keeping it separate.
STEP-1:
int& DynArray::back()
{
if (si == 0)
{
throw runtime_error(" List is empty");
return -1;
}
return head[si - 1];
}
int& DynArray::front()
{
if (si == 0)
{
throw runtime_error(" List is empty");
return -1;
}
return head[0];
}
int& DynArray::operator[](int i)
{
if (i < 0 || i >= si)
{
throw runtime_error(" Given index is not a valid index");
return obj;
}
return head[i];
}
DynArray::DynArray(const DynArray& obj)
{
head = (int*)malloc(obj.cap * sizeof(int));
for (int i = 0; i< obj.si; i++)
head[i] = obj.head[i];
si = obj.si;
cap = obj.cap;
}
DynArray& DynArray::operator=(const DynArray& obj)
{
if(head != NULL)
free(head);
head = (int*) malloc(obj.cap * sizeof(int) );
for (int i = 0; i< obj.si; i++)
head[i] = obj.head[i];
si = obj.si;
cap = obj.cap;
return *this;
}
STEP:2
#include <cstdlib>
#include <iostream>
#include<stdlib.h>
#include<cstring>
using namespace std;
template <class T> class DynArray
{
T *head;
T *newHead;
int cap;
int si;
void resizeArray();
public:
DynArray();
DynArray(int);
DynArray(const DynArray& obj) ;
~DynArray(); //destructor to free memory
void push_back(T);
void pop_back();
int size();
int capacity();
void clear();
T& at(int);
T& back();
T& front();
T& operator[](int i);
DynArray& operator=(const DynArray& obj);
};
template <class T> T& DynArray<T>::at(int pos)
{
if (pos < 0 || pos >= si)
{
// throw runtime_error(" Given index is not a valid index");
// return -1;
}
return head[pos];
}
template <class T> void DynArray<T>::resizeArray()
{
newHead = (T*)realloc(head, cap * 1.5 * sizeof(T));
cap *= 1.5;
head = newHead;
}
template <class T> DynArray<T>::DynArray()
{
head = (T*)malloc(2 * sizeof(T));
si = 0;
cap = 2;
for (int i = 0; i< cap; i++)
memset(&head[i],0,sizeof(T));
}
template <class T> DynArray<T>::DynArray(int n)
{
head = (T*)malloc(n * sizeof(T));
si = 0;
cap = n;
for (int i = 0; i< cap; i++)
memset(&head[i],0,sizeof(T));
}
template <class T> int DynArray<T>::size()
{
return si;
}
template <class T> int DynArray<T>::capacity()
{
return cap;
}
template <class T> void DynArray<T>::clear()
{
free(head);
head = (T*)malloc(2 * sizeof(T));
si = 0;
cap = 2;
for (int i = 0; i< cap; i++)
memset(&head[i],0,sizeof(T));
}
template <class T> DynArray<T>::~DynArray()
{
free(head);
}
/*
* * Function that adds the given value to the front of the array
* */
template <class T> void DynArray<T>::push_back(T val)
{
if (si == cap)
{
resizeArray();
head[si++] = val;
}
else
{
head[si++] = val;
}
}
template <class T> void DynArray<T>::pop_back()
{
si--;
}
template <class T> T& DynArray<T>::back()
{
T obj ;
if (si == 0)
{
// throw runtime_error(" List is empty");
return obj;
}
return head[si - 1];
}
template <class T> T& DynArray<T>::front()
{
T obj;
if (si == 0)
{
// throw runtime_error(" List is empty");
return obj;
}
return head[0];
}
template <class T> T& DynArray<T>::operator[](int i)
{
T obj;
if (i < 0 || i >= si)
{
// throw runtime_error(" Given index is not a valid index");
return obj;
}
return head[i];
}
template <class T> DynArray<T>::DynArray(const DynArray& obj)
{
head = (T*)malloc(obj.cap * sizeof(T));
for (int i = 0; i< obj.si; i++)
head[i] = obj.head[i];
si = obj.si;
cap = obj.cap;
}
template <class T> DynArray<T>& DynArray<T>::operator=(const DynArray& obj)
{
if(head != NULL)
free(head);
head = (T*) malloc(obj.cap * sizeof(T) );
for (int i = 0; i< obj.si; i++)
head[i] = obj.head[i];
si = obj.si;
cap = obj.cap;
return *this;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.