类模板的使用
#include <iostream> #include <conio.h> #include <string> #define N 3 using namespace std; template <class numtype> class Swap{ public : Swap(numtype a,numtype b){x=a;y=b;} numtype ___(){ temp=x; x=y; y=temp; return x; } //test numtype ____(){ return x; } //test private : numtype x,y; numtype temp; }; template <class numtypeeee> class Seh_arr{ public: Seh_arr(numtypeeee a[N]){ for(int i = 0; i < N; ++i){ *(x+i)=*(a+i); } } numtypeeee seh(){ cout << "input the Number of the element you want:"; cin >> T; return x[T-1]; } private: numtypeeee x[N]; int T; }; int main(void){ Swap<int > wap1(3,7); cout<<wap1.___( )<<" "<<wap1.____( )<<"--change int."<<endl; Swap<double > wap2(45.78,93.6); cout<<wap2.___( )<<" "<<wap2.____( )<<"--change float."<<endl; Swap<char> wap3('a','A'); cout<<wap3.___( )<<" "<<wap3.____( )<<"--change char."<<endl; Swap<string> wap4("nice", "good"); cout<<wap4.___( )<<" "<<wap4.____( )<<"--change char."<<endl; int a[N]={1,2,3}; Seh_arr<int>_testInt(a); cout << _testInt.seh() << "--search int."<< endl; float b[N]={1.1, 2.2, 3.3}; Seh_arr<float>_testFlo(b); cout << _testFlo.seh() << "--search float." << endl; char c[N]={'a', 'b', 'c'}; Seh_arr<char>_testCha(c); cout << _testCha.seh() << "--search char." << endl; string d[N]={"my", "name", "is"}; Seh_arr<string>_testStr(d); cout << _testStr.seh() << "--search string." << endl; getch(); return 0; }