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

IN C++ 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.

what i have so far:

Array.h

#ifndef ARRAY_H #define ARRAY_H

template // the value used for lower, upper in main must be a constant class Array { public: enum ErrorCodes { ChangingValues, DisplayingValues }; Array(); Array(const Array &); ~Array(); const T & GetAt(int) const; void SetAt(int, const T &); Array & Copy(const Array &); Array & operator = (const Array &); T & operator [] (int); const T & operator [] (int) const; private: T Data[upper - lower + 1]; };

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

template inline void Array ::SetAt(int i, const T & Value) { (*this)[i] = Value; }

template Array ::Array() { }

template Array ::Array(const Array & A) { int i;

for (i = 0; i < lower, upper; i++) Data[i] = A.Data[i]; }

template Array ::~Array() { }

template Array & Array ::Copy (const Array & A) { int i;

for (i = 0; i < upper; i++) Data[i] = A.Data[i]; return *this; }

template Array & Array ::operator = (const Array & A) { int i;

for (i = 0; i < upper; i++) Data[i] = A.Data[i]; return *this; }

template T & Array ::operator [] (int i) { // assert ((i >= 0) && (i < lower, upper)); if ((i < lower) || (i >= upper)) { delete[] & Array; throw ChangingValues; // you can only throw one thing } else return Data[i]; }

template const T & Array ::operator [] (int i) const { // assert ((i >= 0) && (i < lower, upper)); if ((i < lower) || (i >= upper)) { delete[] & Array; throw DisplayingValues; } else return Data[i]; }

#endif

><><><><><><>><><<><><><><><><<><><><<><><><>

main.cpp

#include

using namespace std;

#include "Array.h"

void Func(const Array &);

void main() { try { Array A1; Array , 0, 20> A2D; char * pTemp;

pTemp = new char[10]; A1[3] = 99; Func(A1); delete[] pTemp; // if a throw occurs with the [] operator, this delete will not be done (memory leak) } catch (Array ::ErrorCodes e) { switch (e) { case Array ::ChangingValues: cout << "Error in index when changing values" << endl; break; case Array ::DisplayingValues: cout << "Error in index when displaying values" << endl; break; default: cout << "Internal error 1 in system" << endl; } } cin.get(); }

void Func(const Array & A) { cout << A[13] << endl; }

pretty sure my header file is right but when i go to test in main all i get is the error messages im throwing. how do i make it work and how do i display that everything is working and all the conditions are tested and correct?

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!