// header: array.h
#ifndef _ADTARRAY
#define _ADTARRAY
#include <assert.h>
#include <iostream.h>
template <class BType, class IType = int> // defaults are OK here too
class Array
{
protected:
BType *arrayData; // pointer to data
int lobnd, hibnd; // 'actual' bounds
bool outOfRange(IType i); // function to test bounds
public:
Array(int SZ = 16, IType lo = 0);// constructor
Array(const Array &x); // copy constructor
~Array(); // destructor
IType lo() const; // return this->lobnd
IType hi() const; // return this->hibnd
BType& operator[](IType i); // overload the [] operator ...
BType& operator[](IType i) const;// ... and again
Array<BType, IType>& operator= (const Array<BType, IType> &x);
// overload the = operator
void grow(int); // change array size
};
// implementation
template <class BType, class IType>
Array<BType, IType>::Array(int SZ, IType lo)
: lobnd(lo), hibnd(lo + SZ - 1)
{
assert(SZ > 0);
arrayData = new BType[SZ];
assert (arrayData != 0);
}
template <class BType, class IType>
Array<BType, IType>::Array(const Array<BType, IType> &x)
: lobnd(x.lobnd), hibnd(x.hibnd)
{
arrayData = new BType[hibnd - lobnd + 1];
assert (arrayData != 0);
(*this) = x; // use assignment
}
template <class BType, class IType>
Array<BType, IType>::~Array()
{
delete [] arrayData;
}
template <class BType, class IType>
inline IType Array<BType, IType>::lo() const
{
return lobnd;
}
template <class BType, class IType>
inline IType Array<BType, IType>::hi() const
{
return hibnd;
}
template <class BType, class IType>
bool Array<BType, IType>::outOfRange(IType i)
{
if (i < lobnd || i > hibnd)
{
cout << "Array index " << i << " is out of range" << endl;
return true;
}
else return false;
}
template <class BType, class IType>
BType& Array<BType, IType>::operator[](IType i)
{
assert (!outOfRange(i));
return(arrayData[i - lobnd]);
}
template <class BType, class IType>
BType& Array<BType, IType>::operator[](IType i) const
{
assert (!outOfRange(i));
return(arrayData[i - lobnd]);
}
template <class BType, class IType>
Array<BType, IType>& Array<BType, IType>::operator=(const Array<BType, IType> &x)
{
assert (hibnd - lobnd == x.hibnd - x.lobnd); // arrays must be same size
for (int i = 0; i <= hibnd - lobnd; ++i)
arrayData[i] = x.arrayData[i];
return *this;
}
template <class BType, class IType>
void Array<BType, IType>::grow(int newS)
{
}
#endif
ADT - Array Class
I am refreshing my knowledge of C++ and would like to implement an Array class with a 'grow' function which takes an integer as an argument, and resizes the current array to the specified size. My class an implementation are in the header file array.h, at this point I have drawn a blank as to where to start from with regards to implementing the grow function and wondered if anyone could point me in the right direction?
Array.H :
"If you want to build a ship, don't drum up the men to
gather wood, divide the work, and give orders. Instead,
teach them to yearn for the vast and endless sea."
- Antoine de Saint Exupery
[edited by - BingoClownO on March 30, 2003 7:56:52 PM]
template < class BType, class IType >void Array< BType, IType >::grow(int newS){ // compute existing array dimension int sz = hibnd - lobnd + 1; // compute new array upper bound hibnd += newS; // allocate "resized" array BType * newData = new [ hibnd - lobnd + 1 ]; // use new dimensions // copy over existing values for( int i = 0; i < sz; ++i ) newData[ i ] = arrayData[ i ]; // optionally zero out remainder of newData: sz += newS; for( ; i < sz; ++i ) newData[ i ] = BType(0); // assumes the existence of null BType value // release existing array and swap class data pointer delete [] arrayData; arrayData = newData;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement