Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming Project: Dynamic Array Template Note: The only thing that needs

ID: 3920822 • Letter: C

Question

C++ Programming Project: Dynamic Array Template

Note: The only thing that needs to be written for the new project is the dynarray.h file (main.cpp is already included).

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.

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:

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:

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:

The driver for this project is provided for you.

The expected output follows:

Note that you must insert trace statements for "grow" and "assign". This verifies that your code is functioning properly.

YOUR FINAL PROGRAM SHOULD ONLY INCLUDE TWO FILES: DYNARRAY.H and MAIN.CPP (main.cpp is provided and cannot be changed)

----------------------------------------------

//main.cpp

//cannot be changed

#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 << " [";
for (int i = 0; i < vectD2.size() - 1; i++)
{
  cout << vectD2[i] << ", ";
}

cout << "..., " << vectD2.back() << "] ";

DynArray<char> 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;
}

Explanation / Answer

here is your file : --------------->>>>>>>>>>>

#ifndef DYN_ARRAY_H
#define DYN_ARRAY_H
#include<iostream>
#include<stdexcept>

using namespace std;

template<typename T>
class DynArray{
private:
  T *_data;
  int _size;
  int _cap;
  void resize();
  void clear();
public:
  DynArray();
  DynArray(const DynArray<T> &oth);
  ~DynArray();
  DynArray<T>& operator=(const DynArray<T> &oth);
  int size()const;
  int capacity()const;
  T& back()const;
  T& front()const;
  void push_back(T item);
  void pop_back();
  T& operator[](const int ind);
  const T& operator[](const int ind)const;
};
template<typename T>
const T& DynArray<T>::operator[](const int ind)const{
if(ind < 0 || ind >= _size){
  throw runtime_error("Index out of bound !!! ");
}
return _data[ind];
}
template<typename T>
T& DynArray<T>::operator[](const int ind){
if(ind < 0 || ind >= _size){
  throw runtime_error("Index out of bound !!! ");
}
return _data[ind];
}
template<typename T>
void DynArray<T>::pop_back(){
if(_size <= 0){
  throw runtime_error("Vector Empty!!!");
}
_data[_size-1].~T();
_size--;
}
template<typename T>
void DynArray<T>::push_back(T item){
if(_size >= _cap){
  resize();
}
_data[_size++] = item;
}
template<typename T>
T& DynArray<T>::back()const{
if(_size <= 0){
  throw runtime_error("Vector Empty!!!");
}
return _data[_size-1];
}
template<typename T>
T& DynArray<T>::front()const{
if(_size <= 0){
  throw runtime_error("Vector Empty!!!");
}
return _data[0];
}
template<typename T>
void DynArray<T>::resize(){
if(_cap == 0){
  _cap = 10;
}else{
  _cap = _cap * 2;
}
T *temp = new T[_cap];
for(int i = 0;i<_size;i++){
  temp[i] = _data[i];
}
delete[] _data;
_data = temp;
}
template<typename T>
int DynArray<T>::size()const{
return _size;
}
template<typename T>
int DynArray<T>::capacity()const{
return _cap;
}
template<typename T>
DynArray<T>::~DynArray(){
clear();
}
template<typename T>
void DynArray<T>::clear(){
_cap = 0;
_size = 0;
delete[] _data;
}
template<typename T>
DynArray<T>& DynArray<T>::operator=(const DynArray<T> &oth){
if(this == &oth){
  return *this;
}
clear();
_cap = oth.capacity();
_data = new T[_cap];
for(int i = 0;i<oth.size();i++){
  _data[i] = oth._data[i];
}
_size = oth.size();
return *this;
}

template<typename T>
DynArray<T>::DynArray(const DynArray<T> &oth){
*this = oth;
}

template<typename T>
DynArray<T>::DynArray(){
_data = new T[10];
_cap = 10;
_size = 0;
}

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote