// Array template class #ifndef ARRAYTEMPLATE_H #define ARRAYTEMPLATE_H #include #include using namespace std; //--------------------------------------------------------------------------- // Array class: like an array (retains all functionality) but also // includes additional features: // -- allows input and output of the whole array // -- allows for comparison of 2 arrays, element by element // -- allows for assignment of 2 arrays // -- size is part of the class (so no longer needs to be passed) // -- includes range checking, program terminates for out-of-bound subscripts // // Assumptions: // -- size defaults to a fixed size of 10 if size is not specified // -- in <<, display 10 per line //--------------------------------------------------------------------------- template class Array { // g++ has problems with friend template functions and class templates // so put the code for operator<< here, in the class definition friend ostream& operator<< (ostream &output, const Array &a) { int i; for (i = 0; i < a.size; i++) { output << a.ptr[i] << ' '; if ((i + 1) % 10 == 0) // display 10 per line output << endl; } if (i % 10 != 0) output << endl; return output; } public: Array(int = 10); // default constructor Array(const Array &); // copy constructor ~Array(); // destructor int getSize() const; // return size of array const Array& operator=(const Array &); // assign arrays bool operator==(const Array &) const; // compare for equal of 2 arrays bool operator!=(const Array &) const; // compare not equal of 2 arrays T& operator[](int); // subscript operator private: T* ptr; // pointer to first element of array int size; // size of the array }; //-------------------------- Constructor ------------------------------------ // Default constructor for class Array template Array::Array(int arraySize) { size = (arraySize > 0 ? arraySize : 10); // default size is 10 ptr = new T[size]; // create space for array assert(ptr != NULL); // terminate if memory not allocated } //---------------------------- Copy ----------------------------------------- // Copy constructor for class Array template Array::Array(const Array &init) { size = init.size; ptr = new T[size]; assert(ptr != NULL); for (int i = 0; i < size; i++) ptr[i] = init.ptr[i]; } //---------------------------- Destructor ----------------------------------- // Destructor for class Array template Array::~Array() { delete [] ptr; ptr = NULL; } //----------------------------- getSize ------------------------------------- // Get the size of the array template int Array::getSize() const { return size; } //----------------------------- = ----------------------------------------- // Overloaded assignment operator template const Array& Array::operator=(const Array& right) { if (&right != this) { // check for self-assignment delete [] ptr; size = right.size; ptr = new T[size]; assert(ptr != NULL); for (int i = 0; i < size; i++) ptr[i] = right.ptr[i]; } return *this; } //------------------------------ == --------------------------------------- // Determine if two arrays are equal. // The type or object T has the responsibility for overloading the operator!= template bool Array::operator==(const Array& right) const { if (size != right.size) return false; // arrays of different sizes for (int i = 0; i < size; i++) if (ptr[i] != right.ptr[i]) return false; // arrays are not equal return true; // arrays are equal } //-------------------------------- != ------------------------------------- // Determine if two arrays are not equal. template bool Array::operator!=(const Array& right) const { return !(*this == right); } //------------------------------- [] -------------------------------------- // Overloaded subscript operator, terminates if subscript out of range error template T& Array::operator[](int subscript) { assert(0 <= subscript && subscript < size); return ptr[subscript]; // reference return creates lvalue } #endif