类模板用于实现类所需数据的类型参数化
类模板在表示如数组、表、图等数据结构显得特别重要, 这些数据结构的表示和算法不受所包含的元素类型的影响
//模板类 类型参数化
templateclass A { public: A(T t){ this->t = t; } T &getT(){ cout << "this is t: " << t << endl; return t; } private: T t; }; //类模板做函数参数 要求具体的类 否则不知道如何分配内存 void useA(A &a){ a.getT(); } Main: A a(100),a1(20);//模板类是抽象的 ---》需要进行类具体化 a1.getT(); useA(a); //模板类 类型参数化 template class A{ public: A(T t){ this->t = t; cout << "this is A(T t) " << t << endl; } T &getT(){ cout << "this is t: " << t << endl; return t; } private: T t; }; //从模板类派生普通类时 需要具体化模板类,即父类的具体类型,内存大小(分配问题) class B :public A { public: B(int a,int b):A (a){ this->a = a; this->b = b; cout << "this is B(int a,int b) a:" << a<<" b:"< class C :public A { public: C(T c, T a) :A (a){ this->c = c; this->a = a; } void printC(){ cout << "this is printC a: " << a << " c: " << c << endl; } private: T a,c; }; int main() { B b1(13,14); b1.getT(); b1.getB(); C c(12, 17); c.printC(); system("pause"); return 0; }



