Question: IntArray Class Exception Chapter 1 4 presented an IntArray class that dynamically creates an array of integers and performs bounds checking on the array. If

IntArray Class Exception
Chapter 14 presented an IntArray class that dynamically creates an
array of integers and performs bounds checking on the array. If an
invalid subscript is used with the class, it displays an error message and aborts the program. Modify the class so it throws an exception instead.
2 headers, and 2.ccp modules
My cpp file is included
// Implementation file for the IntArray class
#include
#include "BadSubscript.h"
#include "IntArray.h"
using namespace std;
//*******************************************************
// Constructor for IntArray class. Sets the size of the *
// array and allocates memory for it.*
//*******************************************************
IntArray::IntArray(int s)
{
arraySize = s;
aptr = new int [s];
for (int count =0; count arraySize; count++)
*(aptr + count)=0;
}
//******************************************************
// Copy Constructor for IntArray class. *
//******************************************************
IntArray::IntArray(const IntArray &obj)
{
arraySize = obj.arraySize;
aptr = new int [arraySize];
for(int count =0; count arraySize; count++)
*(aptr + count)=*(obj.aptr + count);
}
//******************************************************
// Destructor for IntArray class. *
//******************************************************
IntArray::~IntArray()
{
if (arraySize >0)
{
delete [] aptr;
aptr = nullptr;
}
}
//***********************************************************
// subscriptError function. Throws a BadSubscript exception *
// when a subscript is out of range. *
//***********************************************************
void IntArray::subscriptError(int sub)
{
throw BadSubscript(sub);
}
//*******************************************************
// Overloaded [] operator. The argument is a subscript. *
// This function returns a reference to the element *
// in the array indexed by the subscript. *
//*******************************************************
int &IntArray::operator[](const int &sub)
{
if (sub 0|| sub >= arraySize)
subscriptError(sub);
return aptr[sub];
}
IntArray Class Exception Chapter 1 4 presented an

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 Accounting Questions!