#include <iostream> using namespace std; //1.类模板的概念 /* template <class T> class person { public: person(T id,T mage) { this->id=id; this->mage=mage; } void show() { cout<<"id: "<<id<<"mage: "<<mage<<endl; } T id; T mage; }; void test1() { //函数模板在调用的时候,可以自动类型推导。 //而类模板必须显示的指定类型 person<int> p(10,29); p.show(); } int main() { test1(); return 0; } */ //2.函数模板的案例 //对char类型和int类型数组进行排序 /* //打印函数 template <class T> void printarray(T *arr,int len) { for (int i=0;i<len;i++) { cout<<arr[i]<<" "; } cout<<endl; } //排序 template <class T> void mysort(T *arr,int len)//冒泡排序 { for (int i=0;i<len;i++) { for (int j=i+i;j<len;j++) { if (arr[i]<arr[j]) { int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } } int main() { //数组 int arr[]={2,3,4,6,7,9,11,34,56,22}; //数组长度 int len= sizeof(arr)/ sizeof(int); //排序前 cout<<"排序前...................."<<endl; printarray(arr,len); //排序 mysort(arr,len); //排序后 cout<<"排序后......................"<<endl; printarray(arr,len); //字符数组 char charr[]={'a','b','e','t','v','h'}; int len1= sizeof(charr)/ sizeof(char); cout<<"char类型数组排序前................"<<endl; printarray(charr,len1);//普通函数对于数组参数,不具有类型转换,加函数模板可解决问题。 mysort(charr,len1); cout<<"char类型数组排序后..................."<<endl; printarray(charr,len1); } */ //3.类模板派生普通类和类模板派生类模板 /* //类模板派生普通类 template <class T> class parent { public: parent(T a) { this->a=a; } void vocie() { cout<<"a= "<<a<<endl; } T a; }; class child1:public parent<int >//子类继承模板类需指定父类类型,因为编译器创建类对象,需要分配内存 { public: child1(int a,int b):parent(a) { this->b=b; } int b; void printb() { cout<<"b= "<<b<<endl; } }; //类模板派生模板类 template <class T> class child2:public parent<T> { public: child2(T a,T b):parent<T>(a) { this->b=b; } void printb() { cout<<"b= "<<b<<endl; } T b; }; int main() { parent<int> com1(10); com1.vocie(); child1 com2=child1(11,22); com2.vocie(); com2.printb(); child2<double > com3=child2<double >(12.1,23.2); com3.vocie(); com3.printb(); }; */ //4.类模板的类内实现 //5.类模板的类外实现 //basecase,类的类内实现 //template <class T> //class person //{ //public: // person(T id,T age) // { // this->id=id; // this->age=age; // } // // void show() // { // cout<<"id ="<<id<<"age= "<<age<<endl; // } // T id; // T age; //}; //case1 类模板在类的外部实现,.cpp和.h不分离编写 //类模板类外实现的时候,不要滥用友元 /* template <class T> class person;//类外声明 template <class T> void print(person<T>& p); //template <class T> ostream& operator<< (ostream& os,person<T>& p); template <class T> class person { public: //template<class T>//window下的编译器可以通过 // friend ostream& operator<<<T>(ostream& os,person<T>& p); friend void print<T>(person<T>& p);//linux 下的写法,在友元函数后加<T> person(T id,T age); void show(); private: T id; T age; }; template <class T> person<T>::person(T id, T age) { this->id=id; this->age=age; } template <class T> void person<T>::show() { cout<<"id= "<<id<<"age= "<<age<<endl; } //重载左移操作符 //template <class T> //ostream& ::operator<<(ostream& os,person<T>& p) //{ // os <<"id= "<<p.id<<"age= "<<p.age<<endl; // return os; // //} template <class T> void print(person<T>& p) { cout<<p.id<<" "<<p.age; } void test001() { person<int> p(10,12); // p.show(); // cout<<"--------------------------"<<endl; // // cout<<p; print(p); } int main() { test001(); } */ //case2 类模板类的外部实现,.cpp和.h分离编写 //类模板类不可以可分离编译问题原因(模板需要二次编译,而在可分离编写中,.cpp中的模板并没有实例,因此不能发生第二次编译,故在链接器链接含有模板的.cpp时候,报错, // 解决办法:将类模板的声明和实现写在一个文件中) //和模板实现机制 c++编译机制有关 //1.模板二次编译,第一次为模板编译,第二次编译发生在调用的时候生成具体函数 //2.c++中,文件独立编译.列如a.cpp文件在编译的时候,发现一个函数调用,在当前文件中找不到时,会在相应位置生成符号,以便链接的时候在其他文件找到对应的函数。 //6类模板中的static关键字 template<class T> class person { public: static int a; }; template<class T> int person<T>::a=0; int main() { person<int > p1,p2,p3; person<char > c1,c2,c3; p1.a=10; c1.a=100; cout<<p1.a<<p2.a<<p3.a<<endl; cout<<c1.a<<c2.a<<c3.a<<endl; //101010 //100100100 //结果说明,类模板中有static的时候,类模板实例化不同类型的类,其实例化类的不同对象共享类模板中static }
类模板学习
最新推荐文章于 2024-09-17 21:58:50 发布