120 likes | 139 Views
Learn how to define and instantiate parameterized types based on a generic class in C++ using templates. Develop custom template classes for various data types easily.
E N D
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
Define template class • template <class T> • class ClassName • { • public: • // constructor • void set( T a); • T get(); • // other functions; • private: • T x; • };
Implementation • template <class T> • Before each function definition • template <class T> • ClassName<T>::ClassName(){} • template <class X> • ClassName<X>::set( X a) • { x = a;} • template <class Y> • Y ClassName<Y>::get(){ return x;}
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<int> x; • ClassName<float> y;
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
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; • };
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]; • }
Implementation - destructor • array::~array() • { • delete [] ptr; • }
Implementation – get and set functions • int array::getsize() const • { • return size; • }
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; • }
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 < 0 || subscript >= size) • { • cout<<"\nError: subscript "<<subscript <<"out of range "<<endl;\ • exit(1); • } • return ptr[subscript]; • } • const int &array::operator [](int subscript) const • { • if ( subscript < 0 || subscript >= size) • { • cout<<"\n error: subscript "<<subscript • <<" out of range "<<endl; • exit(10; • } • return ptr[subscript]; • }
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; • }