I'm pretty new to C++, and have only been programming for about 10 weeks now. I have this homework assignment that I have been struggling greatly on, and after trying for hours to get it started, I just can't seem to grasp exactly what the question is asking me to do. Below I put what I have started. I know it's not much, but I would like to at least understand the problem entirely before I continue. Here is the problem:
"Recall that in C++ there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++ the array index starts at 0.
Design and implement the class myArray that solves the array index out of bound problem, and also allow the user to begin the array index starting at any integer, positive or negative. Every object of the type myArray is an array of the type int. During execution, when accessing an array component, if the index is out of bounds, the program must terminate with an appropriate error message. Consider the following statements:
myArray<int> list (5); // Line 1
myArray<int> myList (2,13); // Line 2
myArray<int> yourList (-5,9); // Line 3
The statement in Line 1 declares list to be an array of 5 components, the component type is int, and the components are: list[0], list[1], …, list[4]; the statement in Line 2 declares myList to be an array of 11 components, the component type is int, and the components are myList[2], myList[3], …., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], …., yourList[0], …., yourList[8]. Write a program to test the class myArray."
Note: I am not asking for someone to hand over the code for this, just someone to help me get started and to let me know what I need to do in order for this program to work. I want to learn from this. The code I have so far probably isn't exactly right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
using namespace std;
template <class T>
class myArray
{
public:
int& operator[](const int pos);
};
template <class T>
int& myArray::operator[](const int pos)
{
pos = startIndex - pos;
return pos;
}
|