您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > (十二C)第10.3_类模板
2020/2/131第10章模版10.3类模版2020/2/132类模板用于实现类所需数据的类型参数化类模板在表示数据结构如数组、表、图等显得特别重要,这些数据结构的表示和算法不受所包含的元素类型的影响10.3类模板2020/2/133类模板由模板说明和类说明构成template类型形式参数表类声明例如templatetypenameTypeclassTClass{//TClass的成员函数private:TypeDateMember;//…};类属参数必须至少在类说明中出现一次10.3.1类模板与模板类2020/2/134templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};templatetypenameTArrayT::Array(ints){if(s1)size=s;elsesize=1;element=newT[size];}templatetypenameTArrayT::~Array(){delete[]element;}templatetypenameTconstT&ArrayT::Entry(intindex)const{returnelement[index];}templatetypenameTvoidArrayT::Enter(intindex,constT&value){element[index]=value;}一个数组类模板2020/2/135templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};templatetypenameTArrayT::Array(ints){if(s1)size=s;elsesize=1;element=newT[size];}templatetypenameTArrayT::~Array(){delete[]element;}templatetypenameTconstT&ArrayT::Entry(intindex)const{returnelement[index];}templatetypenameTvoidArrayT::Enter(intindex,constT&value){element[index]=value;}一个数组类模板templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};2020/2/136templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};templatetypenameTArrayT::Array(ints){if(s1)size=s;elsesize=1;element=newT[size];}templatetypenameTArrayT::~Array(){delete[]element;}templatetypenameTconstT&ArrayT::Entry(intindex)const{returnelement[index];}templatetypenameTvoidArrayT::Enter(intindex,constT&value){element[index]=value;}一个数组类模板templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};数据成员是T类型指针2020/2/137templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};templatetypenameTArrayT::Array(ints){if(s1)size=s;elsesize=1;element=newT[size];}templatetypenameTArrayT::~Array(){delete[]element;}templatetypenameTconstT&ArrayT::Entry(intindex)const{returnelement[index];}templatetypenameTvoidArrayT::Enter(intindex,constT&value){element[index]=value;}一个数组类模板templatetypenameTArrayT::Array(ints){if(s1)size=s;elsesize=1;element=newT[size];}templatetypenameTArrayT::~Array(){delete[]element;}templatetypenameTconstT&ArrayT::Entry(intindex)const{returnelement[index];}templatetypenameTvoidArrayT::Enter(intindex,constT&value){element[index]=value;}类模板的成员函数是函数模板2020/2/138templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};templatetypenameTArrayT::Array(ints){if(s1)size=s;elsesize=1;element=newT[size];}templatetypenameTArrayT::~Array(){delete[]element;}templatetypenameTconstT&ArrayT::Entry(intindex)const{returnelement[index];}templatetypenameTvoidArrayT::Enter(intindex,constT&value){element[index]=value;}一个数组类模板#includeiostream.h#includeArray.hvoidmain(){ArrayintIntAry(5);inti;for(i=0;i5;i++)IntAry.Enter(i,i);coutIntegerArray:\n;for(i=0;i5;i++)coutIntAry.Entry(i)'\t';coutendl;ArraydoubleDouAry(5);for(i=0;i5;i++)DouAry.Enter(i,(i+1)*0.35);coutDoubleArray:\n;for(i=0;i5;i++)coutDouAry.Entry(i)'\t';coutendl;}2020/2/139templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};templatetypenameTArrayT::Array(ints){if(s1)size=s;elsesize=1;element=newT[size];}templatetypenameTArrayT::~Array(){delete[]element;}templatetypenameTconstT&ArrayT::Entry(intindex)const{returnelement[index];}templatetypenameTvoidArrayT::Enter(intindex,constT&value){element[index]=value;}一个数组类模板#includeiostream.h#includeArray.hvoidmain(){ArrayintIntAry(5);inti;for(i=0;i5;i++)IntAry.Enter(i,i);coutIntegerArray:\n;for(i=0;i5;i++)coutIntAry.Entry(i)'\t';coutendl;ArraydoubleDouAry(5);for(i=0;i5;i++)DouAry.Enter(i,(i+1)*0.35);coutDoubleArray:\n;for(i=0;i5;i++)coutDouAry.Entry(i)'\t';coutendl;}2020/2/1310templatetypenameTclassArray{public:Array(ints);virtual~Array();virtualconstT&Entry(intindex)const;virtualvoidEnter(intindex,constT&value);protected:intsize;T*element;};templatetypenameTArrayT::Array(ints){if(s1)size=s;elsesize=1;element=newT[size];}templatetypenameTArrayT::~Array(){delete[]element;}templatetypenameTconstT&ArrayT::Entry(intindex)const{returnelement[index];}templatetypenameTvoidArrayT::Enter(intindex,constT&value){element[index
本文标题:(十二C)第10.3_类模板
链接地址:https://www.777doc.com/doc-3710959 .html