The following functions and operators are available in my array class.
template < class Type>
class Array
{
public:
Array(int = 0,int = ReSize);
Array(int,int,Type);
Array(const Array&);
~Array();
Type& operator [](int);
Type operator [](int)const;
Array& operator = (const Array&);
Array copy(); //makes a copy that will not allow destruction of original
int size()const {return data->_size;} // how much memory is allocated for array
int numberInArray()const {return data->_active;} //how many active items in array
int operator ==(const Array &)const;
int operator != (const Array &a)const{return !(*this == a);}
void operator += (const Array&);
// concatenates array at end of current array
// data related functions
int find(const Type &)const;
// find returns -1 if not found and subscript if found
// find uses == for Type so make sure to override ==
// properly
int insertItem(const Type &);
// inserts the item at the end of the list
int insertItemAt(const Type &,int);
// inserts at the place indicated by second parameter
void deleteItem(const Type &);
// locates item Type and deletes if it is found
Type deleteItemAt(int);
// deletes item at the indicated location
void clearArray();
// empties the array
protected:
// the parts only the programmer needs to know
};
Array< X> myXs;No size need be specified, since arrays defined using this class expand when the need arrises. Array subscripts always start with subscript 0. In the case above the first time something is added to myXs, the array expands by one. If an item is placed in a specific location and that location is beyond the end of the array the array expands to include that location. If we have:
Array< double> myDouble(10,5);
mDouble[23] = 1.23;
The location 23 is beyond the last location in the array (namely 9) since the array is initized with 10 items
(0-9). The array must be increased by memory blocks that are multiples of 5. In this case, if we increase the
size of the array by 15, the upper bound of the array will be 24. All of this is performed automatically, so
the user will never know that it is happening unless there is not enough memory to allocate the new block.
There are two distinct types of functions (operators) available in the array class. The first type of function are those used in list manipulation. These are the most frequently used functions since they are data oriented. The second class are standard array functions. These functions are more frequently use when creating a new type of array (descendant of array with special properties).
Array functions are most often used to store and retrieve data. The operations needed are insert delete, find and clearArray.
Array< int> myInts;
Array< double> myDoubles(10,5);
The second constructor is similar to
the first, but allows the user to specify the data contained in all the entries in the array. The following line
will create an array of 10 integers all set to 1. If the array is increased in size, the increase will be a multiple
of 15 (15, 30, 45, etc).
Array< int> myInts(10,15,1);
The last constructor is the copy constructor. It creates a new array from an existing array. It is always called by the
system when a parameter is passed by value. As in the assigment operator, the new copy contains a counted
pointer to the data, not a new block of memory.
The insertItem function simply inserts an item at the end of the list, while the insertItemAt function inserts the item at a given location in the array and moves all items from this position on down on location in the list. Both insert functions return the location of the item in the array. The first item in an array is always at location 0. The insert functions automatically resize the array if more memory is needed.
There are two delete functions. The DeleteItem function searches for a specific item (and uses find therefore the == operator for the class of objects in the array). If the item is present it deletes that item. The function DeleteItemAt deletes the item at the indicated location in the array. It returns the item that was deleted.
The find function returns the location of the item sought if it is in the array. This value will range between 0 and one less than the number of items in the array. If the item is not found, find returns -1
This function returns the array to its empty condition. It is usefull when you are reading a new file and want this data to replace the old data in the array.
You will notice that there is no method for looping through the array. In object oriented programming, the process of traversing a structure is left to a separate class called an iterator. In some structures (trees), more than one way traversing the structure is available. Each traversal type has its own iterator. To see the simple array iterator described click here.