Question: Hi , I need my program to follow this steps but it cannot debug and it needs to test int, double, or char. Also needs

Hi , I need my program to follow this steps but it cannot debug and it needs to test int, double, or char. Also needs to test an object, like string. Exact requeriments are described below. Thanks

Write and test a data structures template. The resulting template can be used in any program in place of a C++ array.

Requirements. Develop MyDynamicArray.h as you write a test driver with class MyDynamicArray, defined and fully tested. Write the public interface exactly as specified below -- do not add to, or change the public interface as specified.

Write the template for an array of 2 values (initially) of unspecified type.

Include a public square bracket getter and setter pair, both with index range-checking, returning whatever value you wish if out of range. But apply capacity auto-adjusting for the setter if out of range high.

Include a public getter named MyDynamicArray::capacity( ) to return the data structure's now-variable capacity

Include a public setter named MyDynamicArray::capacity(int) to change the capacity.

Do tests with int, double, or char. Also do tests with an object, like string.

Note that there is no good reason to copy the "dummy" value in the dynamic memory management functions, so don't include it in your testing of const object copy or object assignment.

Header:

#ifndef DynamicArray_h #define DynamicArray_h

#include using namespace std;

template class DynamicArray { V* values; int cap; V dummy;

public: DynamicArray(int=2); DynamicArray(const DynamicArray&); ~DynamicArray(){delete [] values;} int capacity() const {return cap;} void capacity(int); V operator[](int) const; V& operator[](int); DynamicArray& operator=(const DynamicArray&); };

template DynamicArray::DynamicArray(int cap) { this->cap = cap; values = new V[cap];

for (int index = 0; index < cap; index++){ values[index] = V(); } }

template V DynamicArray::operator[](int index) const { if (index < 0 || index >= cap) return V(); // a copy return values[index]; // a copy }

template V& DynamicArray::operator[](int index) { if (index < 0){ return dummy; // a copy } else if (index >= cap){ capacity(2 * index); } return values[index]; // a copy }

template void DynamicArray::capacity(int newCap){ V* temp = new V[newCap];

// get the lesser of the new and old capacities int limit = min(newCap,cap);

// copy the contents for (int i = 0; i < limit; i++){ temp[i] = values[i]; }

// set added values to their defaults for (int i = limit; i < cap; i++){ temp[i] = V(); }

// deallocate original array delete [] values;

// switch newly allocated array into the object values = temp;

// update the capacity cap = newCap; }

template DynamicArray::DynamicArray(const DynamicArray& original) { cap = original.cap; // still copy values = new V[cap]; // not copy, is new for (int i = 0; i < cap; i++){ // contents copy original to new values[i] = original.values[i]; } }

template DynamicArray& DynamicArray::operator=(const DynamicArray& original) { if (this != &original) //check if copy or not, better not be tho { // same as destructor delete [] values;

// same as copy constructor cap = original.cap; values = new V[cap]; // not copy, is new for (int i = 0; i < cap; i++){ // contents copy original to new values[i] = original.values[i]; } } return *this; // return self reference }

#endif

CPP File

#include #include using namespace std;

#include

#include "DynamicArray.h" #include "DynamicArray.h"

int main(){

DynamicArray a(100); cout << "Using int values:" << endl; cout << "Testing Array::Array "; for (int i = 0; i < a.capacity(); i++){ assert(a[i] == 0); }

cout << " Testing the Array::operator[] setter "; a[1] = 123; a[2] = 546; cout << "EXPECTED: 123 for a[1] "; cout << "ACTUAL: " << a[1] << endl; assert(123 == a[1]); cout << "EXPECTED: 546 for a[2] "; cout << "ACTUAL: " << a[2] << endl; assert(546 == a[2]); a[-1000] = 222; cout << "EXPECTED: 222 for a[-1000] "; cout << "ACTUAL: " << a[-1000] << endl; assert(123 == a[1]); assert(546 == a[2]); assert(222 == a[-3]); assert(0 == a[100]); // should increase array capacity here and make new values 0 (for int) assert(222 != a[11]); assert(222 != a[0]);

cout << " Testing the Array::operator[] getter "; const DynamicArray b = a; for (int i = 0; i < 10; i++){ assert(a[i] == b[i]); }

cout << " Const object test" << endl; const DynamicArray c; assert(c.capacity()); assert(c[0] == c[0]);

cout << " Object copy test "; DynamicArray d(a); // making a copy, c = a would have worked too assert(a.capacity() == d.capacity()); for (int i = 0; i < a.capacity(); i++){ assert(a[i] == d[i]); // uses the setter version for both a and b }

// object assignment test cout << " Object assignment test "; DynamicArray e; e = a; assert(a.capacity() == e.capacity()); for (int i = 0; i < 10; i++) assert(a[i] == e[i]);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!