Problem2215--Given the class definition and associated code.

2215: Given the class definition and associated code.

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 0  Solved: 0
[Submit] [Status] [Web Board] [Creator:]

Description

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). #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

Source/Category

52