1 // TemplateTest.cpp : 定义控制台应用程序的入口点。 2 3 4 #include "stdafx.h" 5 #include <iostream> 6 /*非模板定义类*/ 7 class NewTest 8 { 9 public: 10 NewTest(int t) 11 { 12 mOut = t; 13 } 14 15 virtual int action(int mIn) 16 { 17 mRes = mIn + mOut; 18 return (mRes); 19 } 20 static void go()//声明为static,只能用类名调用:: 21 { 22 std::cout<<"NewTest go ...."<<std::endl; 23 } 24 private: 25 int mOut; 26 int mRes; 27 }; 28 29 /*单参数列表*/ 30 template <typename T> 31 class Test 32 { 33 public: 34 Test(T t) 35 { 36 mOut = t; 37 } 38 39 virtual T action(T mIn) 40 { 41 mRes = mIn + mOut; 42 return (mRes); 43 } 44 virtual void go() 45 { 46 std::cout<<"Test go ...."<<std::endl; 47 } 48 private: 49 T mOut; 50 T mRes; 51 }; 52 /*多参数列表*/ 53 template <typename T,typename E> 54 class Travel 55 { 56 public: 57 Travel():mBeg(1) 58 { 59 } 60 virtual ~Travel() 61 { 62 } 63 virtual T Direction(E mIn) 64 { 65 mBeg++; 66 if(mBeg == 2) 67 return (mIn*2); 68 return mIn; 69 } 70 private: 71 E mBeg; 72 E mIn; 73 }; 74 /*多参数列表*/ 75 template<typename T,class E> 76 class MyTest 77 { 78 public: 79 MyTest():mInput(1) 80 { 81 } 82 virtual ~MyTest() 83 { 84 } 85 virtual void print(T mPut) 86 { 87 E::go(); 88 mInput++; 89 std::cout<<(mInput + mPut)<<std::endl; 90 } 91 private: 92 T mInput; 93 }; 94 95 int _tmain(int argc, _TCHAR* argv[]) 96 { 97 Test<int> test1(5); 98 std::cout<<test1.action(1)<<std::endl; 99 Test<double> test2(6.6); 100 std::cout<<test2.action(1.7)<<std::endl; 101 102 Travel<double,int> tra; 103 std::cout<<tra.Direction(6)<<std::endl; 104 105 MyTest<int,NewTest> mytest; 106 mytest.print(7); 107 return 0; 108 }
 
                
            
         
                    
                     
                    
                 
                    
                 
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号