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
Array
// 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
// NOTES: // Whatever data type (class) you use for the template argument below // MUST support the overloaded > for comparison purposes
template
template
template
template
template
template
template
template
template
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
template
template
}; #endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Header.h
#ifndef ARRAY_H #define ARRAY_H
#include
template
template
template
template
template
template
}
template
template
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
template
template
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
