a) Modify the code implementing the operator[] to throw an int exception if the index is out of the declared array range. The exception object is to have the value of the out-of-range index. Be sure you add an exception specification to the operator function. b) Modify the code in main that calls operator[] to catch the exception and terminate the program using exit(1).
Given the class definition and associated code.
What will be an ideal response?
```
#include
using namespace std;
class intVec
{
public:
intVec();
intVec(int n);
// other members
int& operator[](int index);
private:
int* v;
int size;
};
intVec::intVec(int n): v(new int[n])
{
for (int i=0; i < n; i++) v[i] =0;
}
int& intVec::operator[](int index)
{
return v[index];
}
int main()
{
intVec myVec(10);
{
//Block enclosing "for" is because VC++ does not
//define for loop control variables inside the
//"for" loop block as the C++ Standard requires
for(int i = 0; i < 10; i++)
myVec[i] = (i-5)*(i-5);
}
{
for(int i = 0; i < 10; i++)
cout << myVec[i] << " ";
cout << endl;
}
int i = -1;
cout << "accessing myVec[" << i << "]" << endl;
cout << myVec[i] << endl;
return 0;
}
```
```
I have bolded the lines changed in the problem
statement to create the answer:
#include
#include
using namespace std;
class intVec
{
public:
intVec();
intVec(int n);
// other members
int& operator[](int index) throw (int);
private:
int* v;
int size;
};
intVec::intVec(int n): v(new int[n]), size(n)
{
for (int i=0; i < size; i++) v[i] =0;
}
//this function changed to an add exception specification
//and catching out-of-range index values
int& intVec::operator[](int index) throw (int)
{
if( 0 <= index && index < size)
return v[index];
else
throw index;
}
int main()
{
intVec myVec(10);
try
{
for(int i = 0; i < 10; i++)
myVec[i] = (i-5)*(i-5);
}
catch(int e)
{
cout << "Caught illegal index of " << e
<< "\nTerminating the program\n";
exit(1);
}
try
{
for(int i = 0; i < 10; i++)
cout << myVec[i] << " ";
cout << endl;
}
catch(int e)
{
cout << "Caught illegal index of " << e
<< "\nTerminating the program\n";
exit(1);
}
int i = -1;
cout << "accessing myVec[" << i << "]" << endl;
try
{
cout << myVec[i] << endl;
}
catch (int e)
{
cout << "Caught illegal index of" << e
<< "\nTerminating the program\n";
exit(1);
}
return 0;
}
```
You might also like to view...
The format specifier %.2f specifies that two digits of precision should be output ________ in the floating-point number.
a. to the left of the decimal point b. centered c. to the right of the decimal point d. None of the above.
How many and what kind of variables (primitive or object) are created in the code below?
``` > double cost = 19.20; // cost is a primitive variable > double percentOff = 0.4; // percentOff is a primitive variable > double salePrice = cost * (1.0 - percentOff); // salePrice is a primitive variable ```
In PowerPoint, the ________ button controls how long it takes for the transition to execute, how slow or fast it displays
A) Duration B) Preview C) Effect Options D) Advance Slide
The Command Button Wizard allows you to create buttons that control Record Navigation
Indicate whether the statement is true or false