Download presentation
Presentation is loading. Please wait.
1
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types Require one or more type parameters to specify how to customize a generic class
2
Define template class template class ClassName { public: // constructor void set( T a); T get(); // other functions; private: T x; };
3
Implementation template Before each function definition template ClassName ::ClassName(){} template ClassName ::set( X a) { x = a;} template Y ClassName ::get(){ return x;}
4
Use template – main function When create a object of the template class, specify the data type be used for this object in a angle bracket Declare a variable ClassName x; ClassName y;
5
5 Case Study – Class of Array TYPE array DOMAIN Each array has pointer to the data type of array and the size of the array OPERATIONS Set size of the array overload operators print the array constructors destructor
6
Case Study – class of array class array { friend ostream &operator<<( ostream &, const Array &); friend istream &operator>>(istream &, array &); public: array(int =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 int &operator[](int); const int &operator[](int) const; private: int size; int *ptr; };
7
Implementation - constructors array::array(int arraysize) { size = (arraysize>0?arraysize : 10); ptr = new int [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 int [size]; for( int i = 0; i<size; i++) ptr[i] = arraytocopy.ptr[i]; }
8
Implementation - destructor array::~array() { delete [] ptr; }
9
Implementation – get and set functions int array::getsize() const { return size; }
10
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; }
11
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]; }
12
Overload extraction and insertion operators istream &operator>>(istream &input, array &a) { for( int i = 0; i < a.size; i++) intput>>a.ptr[i]; return input; } ostream &operator<<(ostream &output, const array &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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.