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

C++ Templates and Exceptions Here is the definition for a CheckedArray class and

ID: 3828035 • Letter: C

Question

C++ 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 T> class CheckedArray
{
public:
CheckedArray(); CheckedArray(int size);
~CheckedArray();
T& operator[](int index) throw (ArrayOutOfRange); int length();
private:
T *p;
int size;
};

template<class T> CheckedArray<T>::CheckedArray() : size(10)
{
p = new T[size];
}

template<class T>
int CheckedArray<T>::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.

1.   Complete the rest of the implementation of CheckedArray. Note that [] is an overloaded member operator of the class, not a friend. The destructor should free up the memory allocated in the constructor.
2.   Define the ArrayOutOfRange class so it contains the illegal index that caused the exception.
3.   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.

Explanation / Answer

#include <iostream>
using namespace std;

//Template class definition
template <class T>
class CheckedArray
{
private:
T* p;
int size;
public:
//Default constructor
CheckedArray();
//Parameterized constructor
CheckedArray(int);
//Destructor
~CheckedArray()
{
delete p;
};//End of destructor

//Displays the size of the array
void printResults();

//Overload the [] operator
T &operator[] (int position);
};//End of class

//Overloads operator []
//Throws exception if index position is out of bound
template <class T>
T & CheckedArray<T>::operator[] (int position)
{
//Checks for invalid position
if((position < 0) || (position > size))
{
//Throws exception with the invalid position
throw (position);
}//End of if
//Otherwise returns the value
else
return p[position];
}//End of function

//Constructor to assign data to the array
template <class T>
CheckedArray<T>::CheckedArray()
{
//Loops up to size specified for the array
for(int x = 0; x < size; x++)
//Assigns zero
p[x] = 0;
}//End of constructor

//Constructor to set the size of the array and value
template <class T>
CheckedArray<T>::CheckedArray(int newSize)
{
//Assigns given size to the member
size = newSize;
//Allocates memory
p = new T[size];
//Loops up to size specified for the array
for(int x = 0; x < size; x++)
//Assigns index position multiplied by two value to the array index position
p[x] = x * 2;
}//End of Constructor

//Displays the size of the array
template <class T>
void CheckedArray<T>::printResults()
{
cout <<" Your Array is " << size << " elements long" << endl;
}//End of function

//Main function
int main()
{
//Creates an array of type integer with size 5
CheckedArray<int> first(5);
//Creates an array of type double with size 10
CheckedArray<double> second(10);
//Displays the size of the first array
first.printResults();
//Try block begin
try
{
cout <<" Position = "<<1<<" Value = "<<first[1] << endl;
cout <<" Position = "<<1<<" Value = "<<first[-1] << endl;
}//End of try
//Catch begin
catch(int e)
{
cout<<" Array out of bound exception! Invalid index position "<<e;
}//End of catch
//Displays the size of the second array
second.printResults();
//Try block begin
try
{
cout <<" Position = "<<8<<" Value = "<<second[8] << endl;
cout <<" Position = "<<12<<" Value = "<<first[12] << endl;
}//End of try
//Catch begin
catch(int e)
{
cout<<" Array out of bound exception! Invalid index position "<<e;
}//End of catch
return 0;
}//End of function main

Output:

Your Array is 5 elements long

Position = 1 Value = 2

Array out of bound exception! Invalid index position -1
Your Array is 10 elements long

Position = 8 Value = 16

Array out of bound exception! Invalid index position 12

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote