#ifndef t_array_h_ #define t_array_h_ #include using namespace std; // Definition for a templated array class. Most of the member // functions (methods) are self-explanatory. template class t_array { public: // A variety of constructors t_array( ); t_array( int n ); t_array( int n, const T& init ); t_array( const t_array& rhs ); // Destructor ~t_array(); const t_array& operator= ( const t_array& rhs ); void resize( int n ); // change the size of the array. int size() const { return this->n_; } T& operator[]( int i ) { return this->elements_[i]; } const T& operator[]( int i ) const { return this->elements_[i]; } bool operator==( const t_array& rhs ) const; private: void copy( const t_array & rhs ); private: T* elements_; int n_; }; // The member and friend functions are included right here in the .h // file. In fact, they could be (and often are) included inside the // class definition. The reason is that separate compilation of // templated functions requires special care. // template t_array::t_array() : elements_(0), n_(0) {} template t_array::t_array( int n ) : elements_(new T[n]), n_(n) {} template t_array::t_array( int n, const T& init ) : elements_(new T[n]), n_(n) { for ( int i=0; ielements_[i] = init; } template t_array::t_array( const t_array& rhs) { copy( rhs ); } template t_array::~t_array() { delete [] elements_; } template const t_array& t_array::operator= ( const t_array& rhs ) { if ( this != &rhs ) { delete [] elements_; copy( rhs ); } return *this; } template void t_array::resize( int n ) { delete [] this->elements_; this->n_ = n; this->elements_ = new T[n]; } template void t_array::copy( const t_array & rhs ) { this->n_ = rhs.n_; this->elements_ = new T[ this->n_ ]; for ( int i=0; in_; ++i ) this->elements_[i] = rhs.elements_[i]; } template bool t_array::operator==( const t_array& rhs ) const { if ( this->n_ != rhs.size() ) return false; for ( int i=0; in_; ++i ) if ( this->elements_[i] != rhs[i] ) return false; return true; } template ostream& operator<<( ostream& ostr, const t_array& rhs ) { for ( int i=0; i