TemplatePPT教学课件

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Array( unsigned sz ); ~Array(); T & operator[]( unsigned i ); private: T * values; unsigned size; };
8
Array Class Template
template<class T> Array<T>::Array( unsigned sz ) { values = new T [sz];
return x >= 0 ? x : -x; }
2
double dabs(double x) {
return x >= 0 ? x : -x; }
Templates
Templates give us the means of defining a family of functions or classes that share the same functionality but which may differ with respect to the data type used internally. A class template is a framework for generating the source code for any number of related classes. A function template is a framework for generating related functions.
Templates
Sikang Hu Computer Science and Engineering
Department
1
Templates
Why use Templates?
int abs(int x) {
return x >= 0 ? x : -x; }
float fabs(float x) {
11
Define a Function Template
template <class T> return-type function-name(T param) template <typename T> return-type function-name(T param)
one parameter function. T is called template parameter.
Declare and define an object: template <class T> class MyClass{
T val; //…… } MyClass <int> x; MyClass <student> aStudent;
5
Simple Example
Multi parameters:
10
Function Templates
A function can be defined in terms of an unspecified type. The compiler generates separate versions of the function based on the type of the parameters passed in the function calls.
12
Function Template Instantiation
generic
Swap Function Template
...
Swap 2 integers
Swap 2 floats
Swap 2 rationals
. <iostream.h> template <class Param> void Swap( Param & x,
Swap
void main() {
int m = 10; int n = 20; Student S1( 1234 ); Student S2( 5678 ); cout << m<<" "<<n<<" "<<endl; Swap( m, n ); // call with integers cout << m<<" "<<n<<" "<<endl; cout << S1.getID()<<" "<<S2.getID()<<" "<<endl; Swap( S1, S2 ); // call with Students cout << S1.getID()<<" "<<S2.getID()<<" "<<endl; 15 }
Param & y ) {
Param temp; temp = x; x = y; y = temp; }
14
class Student { public:
Student( unsigned id = 0 ) {
idnum = id; } int getID() {return idnum;}; private: unsigned idnum; };
template <class T1, class T2> class Circle { //... private:
T1 x, y; T2 radius; }; //... Circle <int, long> c1; Circle <unsigned, float> c2;
6
Class Templates
size = sz; }
template<class T> T & Array<T>::operator[ ] ( unsigned i ) {
return values[i]; }
template<class T> Array<T>::~Array() { delete [] values; }
9
Array Class Template
void main() {
Array<int> dive(3); for (int i = 0; i < 3; i++)
cin >> dive[i]; for (i = 0; i < 3; i++) cout << dive[i] << endl; cout << endl; }
generic
Array Class Template
...
integer
float
FString
...
Array
Array
Array
7
Array Class Template
#include <iostream.h> template <class T> class Array { public:
3
Class Templates
template <class T> class class-name { …… } template <typename T> class class-name( …… }
T is a template parameter.
4
Class Templates
One parameter:
相关文档
最新文档