int square int number return number number float

  • Slides: 16
Download presentation

int square ( int number ) { return number * number ; } float

int square ( int number ) { return number * number ; } float square ( float number ) { return number * number ; } 采用函数模板实现square: template < class T > T square ( T number ) { return number * number ; }

【例7. 1】函数模板应用举例。 template < class T > T square ( T number ) {

【例7. 1】函数模板应用举例。 template < class T > T square ( T number ) { return number * number ; } int main ( ) { int user. Int ; float user. Float ; cin >> user. Int >> user. Float ; cout << "它们的平方分别是:" ; cout << square ( user. Int ) << square ( user. Float ); return 0; 7 -1. cpp }

7. 1. 4 函数模板重载 形 式 之 一 template < class T > T

7. 1. 4 函数模板重载 形 式 之 一 template < class T > T sum ( T value. One , T value. Two ) { return value. One + value. Two ; } template < class T > T sum ( T value. One , T value. Two , T value. Three ) { return value. One + value. Two + value. Three ; } 调用形式:sum ( num 1 , num 2 ); 8 sum ( num 1 , num 2 , num 3);

7. 1. 4 函数模板重载 形 式 之 二 float sum ( float value. One

7. 1. 4 函数模板重载 形 式 之 二 float sum ( float value. One , float value. Two ) { return value. One + value. Two ; } template < class T > T sum ( T value. One , T value. Two , T value. Three ) { return value. One + value. Two + value. Three ; } 9

template < class T > class Freewill. Array { T *aptr; int array. Size;

template < class T > class Freewill. Array { T *aptr; int array. Size; void mem. Error(void); // allocation errors void sub. Error(void); // out of range public: Freewill. Array( ) { aptr = 0; array. Size = 0; } Freewill. Array( int ); Freewill. Array(const Freewill. Array &); ~Freewill. Array( ); int size( ) { return array. Size; } T &operator[ ](const int &); };

// Constructor for Freewill. Array class. template < class T > Freewill. Array <T>:

// Constructor for Freewill. Array class. template < class T > Freewill. Array <T>: : Freewill. Array( int s ) { array. Size = s; aptr = new T [s]; if (aptr == 0) mem. Error(); for (int count = 0; count < array. Size; count++) *(aptr + count) = 0; }

// Copy Constructor for Freewill. Array class. template <class T> Freewill. Array<T>: : Freewill.

// Copy Constructor for Freewill. Array class. template <class T> Freewill. Array<T>: : Freewill. Array ( const Freewill. Array &obj ) { array. Size = obj. array. Size; aptr = new T [array. Size]; if (aptr == 0) mem. Error(); for(int count = 0; count < array. Size; count++) *(aptr + count) = *(obj. aptr + count); }

// Destructor for Freewill. Array class. template <class T> Freewill. Array<T>: : ~Freewill. Array(

// Destructor for Freewill. Array class. template <class T> Freewill. Array<T>: : ~Freewill. Array( ) { if (array. Size > 0) delete [ ] aptr; } // mem. Error function. template <class T> void Freewill. Array<T>: : mem. Error( ) { cout << "ERROR: allocate memory. n"; exit(0); }

// sub. Error function. template <class T> void Freewill. Array<T>: : sub. Error(void) {

// sub. Error function. template <class T> void Freewill. Array<T>: : sub. Error(void) { cout << "ERROR: Subscript out of range. n"; exit(0); } // The argument is a subscript. template <class T> T &Freewill. Array<T>: : operator[ ](const int &sub) { if (sub < 0 || sub > array. Size) sub. Error(); return aptr[sub]; }

void main( ) { Freewill. Array <int> int. Table(10); Freewill. Array <float> float. Table(10);

void main( ) { Freewill. Array <int> int. Table(10); Freewill. Array <float> float. Table(10); int x; for (x = 0; x < 10; x++) { int. Table[x] = (x * 2); float. Table[x] = (x * 2. 14); } for (x = 0; x < 10; x++) cout << int. Table[x] << " "; cout << endl; for (x = 0; x < 10; x++) cout << float. Table[x] << " "; } 7 -4. cpp