Create a class implements a safe array


Discuss the below:

Q: Create a C++ class that implements a "safe" array, where the arrays bounds are checked before accessing its elements. The class constructor must accept an integer parameter and create a dynamic array of that size for storing integers, implemented as a vector. The constructor must initialize all array elements with 0. Add two methods get(int) that returns the i-th element of the array and set(int) that uses return by reference for setting the corresponding array element, whose index is passed as the parameter. The method must allow to be used in the left-hand-side of assignment operator like array.set(i) = 5;. Report an error if the array index is out of bounds.

In the main() method, create an array instance of size n provided by the user, assign its values to 1,2,...,n and print them out.

 

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Create a class implements a safe array
Reference No:- TGS01937307

Now Priced at $20 (50% Discount)

Recommended (95%)

Rated (4.7/5)