Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,

Similar presentations


Presentation on theme: "Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,"— Presentation transcript:

1 Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type, the header and implementation will be almost same except the data type for the array How to describing a generic class array and instantiating classes that are type-specific version of this generic class We define a template class which have the data type of array as parameter When it is declared in client program, it will be specified as int, float or double.

2 Case Study – class of array Template class array { friend ostream &operator<<( ostream &, const Array &); friend istream &operator>>(istream &, array &); public: array(T =10); array(const array&); // copy constructor; ~array(); // destructor int getsize() const; // return size const array &operator =(const array&); // equality operator bool operator==(const array &) const; // inequality operator; returns opposite of == operator bool operator!= (const array &right) const { return !(*this == right ); } // subscript operator T &operator[](int); const int &operator[](int) const; private: int size; T *ptr; };

3 Implementation - constructors array ::array(int arraysize) { size = (arraysize>0?arraysize : 10); ptr = new T [size]; for( int i = 0; i<size; i++) ptr[i]=0; } // copy constructor for class array // must receive a reference to prevent infinite recursion array ::array(const array &arraytocopy) :size(arraytocopy.size) { ptr = new T [size]; for( int i = 0; i<size; i++) ptr[i] = arraytocopy.ptr[i]; }

4 Implementation - destructor Array ::~array() { delete [] ptr; }

5 Implementation – get and set functions int array ::getsize() const { return size; }

6 Implementation – overload assignment operator // overloaded assignment operator const array &array ::operator =( const array &right) { if(&right != this ) { // if arrays of different sizes, deallocate origina // left-side array, then allocate new left-side array if( size != right.size) { delete [] ptr; size = right.size; ptr = new int[size]; } for( int i =0; i<size;i++) ptr[i] = right.ptr[i]; } return *this; }

7 Implementation – overload other operators bool array ::operator == (const array &right) const { if(size != right.size) return false; for(int i = 0; i, size; i++) if( ptr[i] != right.ptr[i]) return false; return true; } int &array ::operator [](int subscript) { if( subscript = size) { cout<<"\nError: subscript "<<subscript <<"out of range "<<endl;\ exit(1); } return ptr[subscript]; } const int &array ::operator [](int subscript) const { if ( subscript = size) { cout<<"\n error: subscript "<<subscript <<" out of range "<<endl; exit(10; } return ptr[subscript]; }

8 Template function - Overload extraction and insertion operators Template istream &operator>>(istream &input, array &a) { for( int i = 0; i < a.size; i++) intput>>a.ptr[i]; return input; } Template ostream &operator &a) { int i; for ( i = 0; i, a.size; i++) { output<<setw(12)<<a.ptr[i]; if ( (i+1)%4 == 0) output<<endl; } if ( i%4 != 0) output<<endl; return output; }


Download ppt "Class Array template The array class defined in last week manipulate array of integer If we need to define class of array for float, double data type,"

Similar presentations


Ads by Google