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 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.
headers, and 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::IntArrayint s
arraySize s;
aptr new int s;
for int count ; count arraySize; count
aptr count;
Copy Constructor for IntArray class.
IntArray::IntArrayconst IntArray &obj
arraySize obj.arraySize;
aptr new int arraySize;
forint count ; count arraySize; count
aptr countobjaptr count;
Destructor for IntArray class.
IntArray::~IntArray
if arraySize
delete aptr;
aptr nullptr;
subscriptError function. Throws a BadSubscript exception
when a subscript is out of range.
void IntArray::subscriptErrorint sub
throw BadSubscriptsub;
Overloaded operator. The argument is a subscript.
This function returns a reference to the element
in the array indexed by the subscript.
int &IntArray::operatorconst int &sub
if sub sub arraySize
subscriptErrorsub;
return aptrsub;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
