资源描述
Click to edit Master title style,Click to edit Master text styles,Second Level,Third Level,Fourth Level,Fifth Level,*,BIT,Templates,Sikang,Hu,Computer Science and Engineering Department,Templates,Why use Templates?,int abs(int x),return x=0?x:-x;,float,fabs(float,x),return x=0?x:-x;,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.,template,class class-name ,template,class class-name(,T,is a template parameter.,Class Templates,Class Templates,Declare and define an object:,template,class,MyClass,T,val,;,/,MyClass,x;,MyClass,aStudent,;,One parameter:,Simple Example,template,class Circle,/.,private:,T1 x,y;,T2 radius;,;,/.,Circle c1;,Circle c2;,Multi parameters:,Class Templates,Array,Class Template,integer,Array,float,Array,FString,Array,.,.,generic,Array Class Template,#include,template,class Array,public:,Array(unsigned,sz,);,Array();,T,private:,T*values;,unsigned size;,;,Array Class Template,template Array:Array(unsigned,sz,),values=new T,sz,;,size=,sz,;,template T&Array:operator (unsigned i),return valuesi;,template Array:Array(),delete values;,Array Class Template,void main(),Array dive(3);,for(int i=0;i divei;,for(i=0;i 3;i+),cout divei,endl,;,cout,endl,;,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.,Define a Function Template,template,return-type function-name(,T,param,),template,return-type function-name(,T,param,),one parameter function.,T,is called template parameter.,Function Template Instantiation,Swap,Function Template,Swap,2 integers,Swap,2 floats,Swap,2,rationals,.,.,generic,Swap,#,include,template,void Swap(,Param,&x,Param,&y),Param,temp;,temp=x;,x=y;,y=temp;,class Student,public:,Student(unsigned id=0),idnum,=id;,int,getID,()return,idnum,;,private:,unsigned,idnum,;,;,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,;,Multiple parameter function template,template,void ArrayInput(T1 array,T2 count),for(T2 j=0;j count;j+),cout arrayj;,float temperature3;,int station4;,ArrayInput(temperature,3);,ArrayInput(station,4);,Example:Table Lookup,#,include,template,long,indexOf,(T,searchVal,const T*table,unsigned size),for(unsigned i=0;i size;i+),if(,searchVal,=tablei),return i;,return-1;,search an value in the table,Example:Table Lookup,void main(),const unsigned,iCount,=10;,const unsigned,fCount,=5;,int,iTableiCount,=0,10,20,30,40,50,60,70,80,90;,float,fTablefCount,=1.1,2.2,3.3,4.4,5.5;,cout,indexOf,(20,iTable,iCount,),endl,;,/2,cout,indexOf,(2.2,fTable,fCount,),endl,;,/1,const unsigned,sCount,=5;,char*,namessCount,=John,Mary,Sue,Dan,Bob;,cout,indexOf,(Dan,names,sCount,),endl,;,/3,Note,When using the function,indexOf,defined before,it could cause a problem.,void main(),Student sTable5=10000,11111,20000,22222,30000;,Student s(22222);,cout,indexOf,(s,sTable,5),endl,;,Example:student,class Student,public:,Student(long,idVal,),id=,idVal,;,int operator=(const Student&s2)const,return id=s2.id;,private:,long id;,/student ID number,;,Derived and Templates,Naturally,it is often useful to derive one template class,from another.If members of a base class depend on a,template parameter of a derived class,the base itself must,be parameterized.,templateclass vector,/*/,templateclass,Vec,:public vector,/*/,Member Templates,A class or a class template can have members that are,themselves templates.,templateclass complex,Scalar re,im,;,public:,templatecomplex(const complex&c),:re(c.real),im(c.imag,),/More code here,;,complex,cf,;,complex,cd,=,cf,;,/ok:uses float to double conversion,class Quad,/no conversion to int,;,complex,cq,;,complex,ci,=,cq,;,/error:no Quad to int conversion,Member Templates,A member template cannot be virtual.,class Shape,/.,templatevirtual,bool,F1(const T,templatevirtual,bool,F2(const T,/error:member function template cannot be virtual,;,
展开阅读全文