Question: Templates and Exceptions Here is the definition for a CheckedArray class and the implementation of one of its constructors and the length function: template class
Templates and Exceptions Here is the definition for a CheckedArray class and the implementation of one of its constructors and the length function: template class CheckedArray { public: CheckedArray(); CheckedArray(int size); ~CheckedArray(); T& operator[](int index) throw (ArrayOutOfRange); int length(); private: T *p; int size; }; template CheckedArray::CheckedArray() : size(10) { p = new T[size]; } template int CheckedArray::length() { return size; } This class behaves like a template version of an array where we set the template type to the data type we would like to store in the array. If ary is a variable of type CheckedArray and i is an illegal index then ary[i] should throw the exception ArrayOutOfRange. Complete the rest of the implementation of CheckedArray. Note that [] is an overloaded member operator of the class, not a friend. It should return the element from p at the index parameter. The destructor should free up the memory allocated in the constructor. Define the ArrayOutOfRange class so it contains the illegal index that caused the exception. Write code in main that tests your CheckedArray class with a test that causes an exception and a test that does not. When an exception occurs, the illegal index should be output from the exception object.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
