类模板与类的相互继承

  1 /* 类模板与类的相互继承 */
  2 
  3 #include<iostream>
  4 
  5 using namespace std;
  6 
  7 // 类继承与类模板:要求类模板实例化
  8 
  9 // 类模板继承于类模板:要求类模板实例化
 10 
 11 // 类模板继承于类:要求类模板实例化
 12 
 13 // 类模板如果不指定类型,编译器不会编译的
 14 
 15 template<class T>
 16 class templateclass
 17 {
 18 public:
 19     T t;
 20     
 21     void print()
 22     {    
 23         cout << t << endl;
 24     }
 25 };
 26 
 27 // 继承要求类模板实例化
 28 class newclass : public templateclass<double *>// 类继承于类模板需要指定类模板的类型
 29 {
 30 };
 31 
 32 
 33 template<class TX>
 34 class newtemplateclass : public templateclass<double>// double等已知类型 可以替换为 TX
 35 {
 36 public:
 37     TX tx;
 38     
 39     void printx()
 40     {    
 41         cout << tx << endl;
 42     }
 43 };
 44 
 45 
 46 template <class T>
 47 class Tclass
 48 {
 49 public:
 50     
 51 };
 52 
 53 class MyClass
 54 {
 55 public:
 56     MyClass()
 57     {
 58         cout << "MyClass()" << endl;
 59     }
 60 
 61     ~MyClass()
 62     {
 63         cout << "~MyClass()" << endl;
 64     }
 65 };
 66 
 67 
 68 class Tclass:public MyClass
 69 {
 70 public:
 71     Tclass()
 72     {
 73         cout << typeid(*this).name() << "Tclass()" << endl;
 74     }
 75 
 76     ~Tclass()
 77     {
 78         cout << "Tclass()" << endl;
 79     }
 80 };
 81 
 82 void main()
 83 {
 84     newclass *p = new newclass;
 85     //p->t = 12.9;
 86     p->t = new double(12.9)
 87     p->print();
 88 
 89     
 90     cin.get();
 91 }
 92 
 93 //--------------------------------------------------------------
 94 
 95 
 96 void main()
 97 {
 98     newtemplateclass<int> my1;
 99 
100     my1.t = 19.8;
101     my1.tx = 10;
102     my1.print();
103     my1.printx();
104 
105     
106     cin.get();
107 }
108 
109 //--------------------------------------------------------------
110 
111 
112 void main()
113 {
114     newtemplateclass<char*> my1;
115     my1.t = "ABC";
116     my1.tx = "XYZ";
117     my1.print();
118     my1.printx();
119 
120     
121 
122     
123     cin.get();
124 }
125 
126 //--------------------------------------------------------------
127 
128 
129 void main()
130 {
131     Tclass<double> *p = new Tclass<double>;
132     delete p;
133     
134     cin.get();
135 }

 

posted on 2015-06-12 08:48  Dragon-wuxl  阅读(240)  评论(0)    收藏  举报

导航