1 / 12

Class template

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>

calhounk
Download Presentation

Class template

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 T> • class ClassName • { • public: • // constructor • void set( T a); • T get(); • // other functions; • private: • T x; • };

  3. 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;}

  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<int> x; • ClassName<float> y;

  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 < 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]; • }

  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; • }

More Related