Question: Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing

Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on the entire array. Errors within operators and constructors will be handled with exceptions (try, throw, catch). Demonstrate that your array operators work inside a function if the array is passed in by reference as a const. Provide a Copy method to provide the same functionality as the = operator. Also provide GetAt and SetAt methods to provide ways of getting and setting values at a particular index in the array.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Current Code that I have

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Main.Cpp

#include

using namespace std;

#include "Second.h" #include "Header.h"

using namespace DynamicArray; void main() { const int LB(-5); const int UB(15); int i = 10; Array I1; // Array I2; // variables cannot be used for a value argument to a template Array I3; Array I4; Array I6; Array D1; // named constants can be used for a value argument to a template // Array I7; Array I8; DynamicArray::Array I9(20);

Array , -5, 5> A2D;

// I1 = I3; // these are not the same type of array because bounds listed in template arguments are different I1 = I6;

I1[3] = 3; // std::cout << I1 [3] << std::endl; cout << I1[3] << endl;

A2D[-3][5] = 0; for (i = LB; i < UB; i++) // std::cout << D1 [i] << std::endl; cout << D1[i] << endl;

// std::cout << I9 [3] << std::endl; cout << I9[3] << endl; }

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Second.h

#ifndef SECOND_H #define SECOND_H

namespace DynamicArray {

#include #include

// NOTES: // Whatever data type (class) you use for the template argument below // MUST support the overloaded > for comparison purposes

template class Array { public: Array(); Array(const Array &); Array(int); ~Array(); T & At(int); T At(int) const; Array & Copy(const Array &); void Sort(); Array & operator = (const Array &); T & operator [] (int); T operator [] (int) const; private: T * pData; int NumElements; };

template inline T & Array ::At(int i) { return (*this)[i]; }

template inline T Array ::At(int i) const { return operator [] (i); }

template inline Array & Array ::Copy(const Array & A) { return operator = (A); }

template Array ::Array() { NumElements = 1; pData = new T[NumElements]; }

template Array ::Array(const Array & A) { int i; NumElements = A.NumElements; pData = new T[NumElements]; for (i = 0; i < NumElements; i++) pData[i] = A.pData[i]; // memcpy (pData, A.pData, NumElements * sizeof (T)); }

template Array ::Array(int Size) { assert(Size > 0); NumElements = Size; pData = new T[NumElements]; }

template Array ::~Array() { delete[] pData; }

template void Array ::Sort() { int i; int Num; bool Sorted; T Temp;

Num = NumElements - 1; do { Sorted = true; for (i = 0; i < Num; i++) if (pData[i] > pData[i + 1]) { Temp = pData[i]; pData[i] = pData[i + 1]; pData[i + 1] = Temp; Sorted = false; } } while (!Sorted); }

template Array & Array ::operator = (const Array & A) { if (NumElements != A.NumElements) { delete[] pData; NumElements = A.NumElements; pData = new T[NumElements]; } else; for (int i = 0; i < NumElements; i++) pData[i] = A.pData[i]; return *this; }

template T & Array ::operator [] (int i) { assert((i >= 0) && (i < NumElements)); return pData[i]; }

template T Array ::operator [] (int i) const { assert((i >= 0) && (i < NumElements)); return pData[i]; }

}; #endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Header.h

#ifndef ARRAY_H #define ARRAY_H

#include #include

template class Array { public: Array(); Array(const Array &); ~Array(); T & At(int); T At(int) const; Array & Copy(const Array &); void Sort(); Array & operator = (const Array &); T & operator [] (int); T operator [] (int) const; private: T data[UB - LB + 1];

template inline T & Array ::At(int i) { return (*this)[i]; }

template inline T Array ::At(int i) const { return operator [] (i); }

template inline Array & Array ::Copy(const Array & A) { return operator = (A); }

template Array ::Array() { }

template Array ::Array(const Array & A) { int i; for (i = 0; i < (UB - LB); i++) data[i] = A.data[i];

}

template Array ::~Array() { }

template void Array ::Sort() { int i; int Num; bool Sorted; T Temp;

Num = NumElements - 1; do { Sorted = true; for (i = 0; i < Num; i++) if (data[i] > data[i + 1]) { Temp = data[i]; data[i] = data[i + 1]; data[i + 1] = Temp; Sorted = false; } } while (!Sorted); }

template Array & Array ::operator = (const Array & A) { if (NumElements != A.NumElements) { delete[] data; NumElements = A.NumElements; data = new T[NumElements]; } else; for (int i = 0; i < NumElements; i++) data[i] = A.data[i]; return *this; }

template T & Array ::operator [] (int i) { assert((i >= LB) && (i <= UB)); return data[i - LB]; }

template T Array ::operator [] (int i) const { assert((i >= 0) && (i < NumElements)); return data[i - LB]; } };

#endif

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!