Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Assignment #1 SafeArray Template Credit: 10 pts, DUE: 2/16/21 We all know that the regular array has weaknesses, such as the size has to be
Assignment #1 SafeArray Template Credit: 10 pts, DUE: 2/16/21 We all know that the regular array has weaknesses, such as the size has to be known when declaring it (constant), the indices have to start with zero, there is no boundary checking. Now we would like you to design a customized array type, called SafeArray, to overcome these weaknesses. Even better we want SafeArray be a class template that stores an array of any type of thing it works on, rather than creating a set of type-specific arrays - an intArray is an array of integers, a floatArray is an array of floats; a catArray is an array of cats. SafeArray is a parameterized template that provides you with the ability to create a general array class and pass types as parameters to that class to build specific instances. We use the class name SA, short for SafeArray. The SA template class has following features: The memory for array elements is always allocated from dynamic memory area, i.e. freestore. And the size of array is determined during the run time. The index of elements in this array does not have to start with zero. Using wrong index (out of bounds) when accessing array elements will not cause any serious consequences, instead just pop up proper error messages if the index goes out of bounds additional The skeleton of the SA template is as follows, but you need to add and implement member functions listed below it const int dSize = 10; // default size template class SA private: T* PT; // pointer of T type int lIdx; // low index int hIdx; // high index public: // the following nine member funtions go here 1. SA() - will allocate a 1D T array in freestore with default size of 10 and which can be indexed between 0 and 9. [default constructor) 2. SA( int n) - will allocate a ID Tarray in freestore with size of n and which can be indexed between 0 and n- 1. [1 parameter constructor) 3. SA( int l, int h ) - will allocate a 1D T array in freestore with size of (h-1+1) and which can be indexed between / and h. [2 parameter constructor 4 SA( const SA& arr ) - will make a separate copy of SafeArray a with the same size and indexing as arr. [copy constructor) 5. operator const SA& rhs ) - assignment operator 6. -SA( ) - will release the memory space allocated for the 1D T array from dynamic memory freestore. [destructor] 7. operator[ ] ( int offset ) will allow user to treat SA names as if they were regular array names. You will need two version of it, one for read, the other for write. 8. getSize ( ) member function will return the number of elements in SA. 9. operator () as friend function. Of course, on the flip side of the SA, usually we declare an array with a pair of square brackets for the size, such as float arr[100]; with SA template we do it with a pair angle brackets of type and a pair of parentheses of size, such as SA arr(100); to declare a SA array of 100 floats, that may look some awkward. In order to practice the SA template with user definced types, you will need to have a Cat class defined and implemented. This will allow user to define a SafeArray of Cats. You will also need to write a driver (main) function with at least three different types of SafeArray (say SA intArr, SA dArr, and SA catArr ) to test all of these class member functions, overloaded operators. Submission: YOU MUST FOLLOW THE FOLLOWING INSTRUCTIONS. Please comment your code as needed. Put everything into a single file, name it as LastNameFirstInitial_a01.cpp (if your name is John Doe, it will be DoeJ_a01.cpp), and then upload it to your blackboard account by the due day
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started