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
template
public: DynamicArray(int=2); DynamicArray(const DynamicArray
template
for (int index = 0; index < cap; index++){ values[index] = V(); } }
template
template
template
// 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
template
// 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
#include "DynamicArray.h" #include "DynamicArray.h"
int main(){
DynamicArray
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
cout << " Const object test" << endl; const DynamicArray
cout << " Object copy test "; DynamicArray
// object assignment test cout << " Object assignment test "; DynamicArray
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
