Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We all low that the regular array has wealnesses, such as the size has to be kno

ID: 3883282 • Letter: W

Question



We all low that the regular array has wealnesses, such as the size has to be known when declaring it (constant), the indices have to start with zero, there is no boundary checking. Now we would like you to design a customized array type, called Safe Array, to overcome these wealnesses. Even better we want Safe Array be a class template that stores an aray of any type of thing it works on, rather than creating a set of type-specific arrays-an intArray is an array of integers; a floatArray is an array of floats; a catAray is an array of cats. SafeArray is a parameterized template that provides you with the ability to create a general aray class and pass types as parameters to that class to build specific instances. We use the class name SA, short for SafeArray. The SA template class has following features: The memory for array elements is always allocated from dynamic memory area, ie freestore. And the size of array is determined during the run time The index of element s in this array does not have to start with zero. · Using wrong index (out of bounds) when accessing array elements will not cause any serious consequences, instead just pop up proper enror messages if the index goes out of bounds. The skeleton of the SA template is as follows, but you need to add and implement the additional member functions listed below it. const int dSize = 10; // default size template class SA I private // pointer of T type int lldx: / low index int hIdx: / high index public: // the following nine member funtions go here SA ( indexed between 0 and 9. [default constructor] )-will allocate a l D T array in freestore with default size of 10 and which can be 1. 2. SA int n -will allocate a 1D T aray in freestore with size of n and which can be 3. 4. SA const SA& arr-will make a separate copy of SafeArray a with the same size 5, indexed between 0 and n1. [1 parameter constructor] SA ( int 1, int h )-will allocate a 1D T array in teestore with size of (h-l+ 1) and which can be indexed between I and h. [2 parameter constructor] and indexing as arr. [copy constructor] operator=( const SAs rhs )-assignment operator -SA ( )-will release the memory space allocated for te ID T array from dynamic memory freestore. [destructor] 6.

Explanation / Answer

Below is your program. Let me know if you have any issue in this

#include<iostream>

using namespace std;

const int dSize = 10;

template<class T>

class SA{

private:

// pointer of type T

T* pT;

// low index

int lIdx;

// high index

int hIdx;

public:

SA()

{

// initiaize array of size 10

pT = new T[dSize];

lIdx = 0;

hIdx = 9;

}

// parametarized constructor

SA(int n)

{

// initiaize array of size n

pT = new T[n];

lIdx = 0;

hIdx = n - 1;

}

// parametarized constructor

SA(int l, int h)

{

// initiaize array of size n

pT = new T[h - l + 1];

lIdx = l;

hIdx = h;

}

// copy constructor

SA(const SA& arr)

{

int i;

// initilize array of appropriate size

this->pT = new int[arr.hIdx - arr.lIdx + 1];

this->lIdx = arr.lIdx;

this->hIdx = arr.hIdx;

// assign the array values of arr to the current object array

for (i = arr.lIdx; i <= arr.hIdx; i++)

this->pT = arr.pT[i];

}

// overloading operator =

void operator=(const SA& rhs)

{

int i;

// initilize array of appropriate size

this->pT = new int[rhs.hIdx - rhs.lIdx + 1];

this->lIdx = rhs.lIdx;

this->hIdx = rhs.hIdx;

// assign the array values of rhs to the current object array

for (i = rhs.lIdx; i <= rhs.hIdx; i++)

this->pT = rhs.pT[i];

}

~SA()

{

delete[] pT;

}

// overload [] operator

T& operator[](int offset)

{

// if index is valid

if (offset >= lIdx && offset <= hIdx)

return this->pT[offset];

else

{

cout << "Please enter valid index ";

return this->pT[0];

}

}

// method to get size of array pT

int getSize() const

{

// return the size of array

return hIdx - lIdx + 1;

}

friend ostream& operator<<(ostream &output, const SA &ob)

{

int i;

int size = ob.getSize();

for (i = 0; i<size; i++)

output << ob.pT[i] << " ";

output << endl;

return output;

}

};

class cat {

private:

string catName;

public:

cat(string name) {

catName = name;

}

cat() {

catName = "";

}

friend ostream& operator<<(ostream &output, const cat &ob)

{

cout<<ob.catName<<" says meow"<<endl;

return output;

}

};

int main()

{

int i;

// object with values of type double

SA<int> intArr(5);

for (i = 0; i<5; i++)

// initialize array of intArr object

intArr[i] = i;

cout << "Array contents are ... ";

// print content of intArr

cout << intArr;

// print size of array

cout << " Size = " << intArr.getSize() << endl << endl;

// object with values of type double

SA<double> dArr(10);

for (i = 0; i<10; i++)

// initialize array of dArr object

dArr[i] = i * 1.1;

cout << "Array contents are ... ";

// print content of dArr

cout << dArr;

// print size of array

cout << " Size = " << dArr.getSize();

SA<cat> catArr(3);

catArr[0] = cat("catA");

catArr[1] = cat("catB");

catArr[2] = cat("catC");

cout << " Array contents are ... ";

// print content of dArr

cout << catArr;

// print size of array

cout << " Size = " << catArr.getSize();

return 0;

}

Sample Output

Array contents are ...
0 1 2 3 4

Size = 5

Array contents are ...
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9

Size = 10

Array contents are ...
catA says meow
catB says meow
catC says meow


Size = 3