Array Index Out of Bounds Problem, Class Template

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;
}
Last edited on
You should begin by deciding what data members your class requires and by implementing constructors that set them to reasonable values.
Is this heading in the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class T>
class myArray
{		
		T firstVal, secondVal;
	public:
		myArray(T first, T second){
			firstVal=first;
			secondVal=second;
		}
		myArray();
		~myArray();
		int& operator[](const int pos);
	private:
		int *location;
		int size;
};
Last edited on
tburns517 wrote:
Is this heading in the right direction?
myArray<int> myList (2,13); // Line 2
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]


Not really headed in the right direction, no. What are firstVal and secondVal supposed to represent? Why are location and size not set to any value?
How's this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef H_myArray
#define H_myArray

class myArray
{	
	public:
		myArray();
		myArray(int, int);
		~myArray();
		int &operator[](int i);
		void setZero();
		int getSize(); 
		int getIndex();

	private:
		int *location;
		int size;
		int start;
		int end;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include "myArray.h"

using namespace std;

int &myArray::operator[](int i)
{
   if ( i < start || i >= end )
   {
   		cout << "Error: Subscript " << i <<" out of range" << endl;
    	exit(1); 
     
   }
    return location[i];
   }

myArray::myArray(int index, int arraySize)
{
size = ( arraySize - index  ); 
   
   for ( int i = index; i < arraySize ; i++ )

   location = new int[size];
           
   start = index;
   end = arraySize;                    
}

myArray::~myArray()
{
    delete[]location;	//Deletes array
}

void myArray::setZero()          //Sets the array equal to zero
{
    for(int i = 0; i < size; i++)
    {
        location[i] = 0;
    }
}
int myArray::getSize()
{
   return size;
}
int myArray::getIndex()
{
  return start;
}

int main()
{
myArray  list(0,5);
myArray  myList(2,13);
myArray  yourList(-5,9);
cout << "Attempt to assign 444 to list[15]" << endl;
   list[ 15 ] = 444; 
return 0;
}
Looking better. It's probably a good practice not to keep more private variables in a class than is necessary, generally speaking. So, you could probably get rid of the end member, since it is easily calculated from the other private variables.

The second constructor is wrong. There should be no loop. Note that the second parameter doesn't indicate a size. It indicates a value of a one-past-the-end index. You should probably change the name to reflect that.

Also, is this not supposed to be a template class?

Topic archived. No new replies allowed.