1、类模板从类模板派生
/*******************类模板从类模板派生**********/
#include <iostream>
using namespace std;
template <class T1,class T2>
class A{
T1 v1;
T2 v2;
public:
A(){cout<<"A!"<<endl;}
};
template <class T1,class T2>
class B:public A<T2,T1>{
T1 v3;T2 v4;
public:B(){cout<<"B!"<<endl;}
};
template <class T>
class C:public B<T,T>{
T v5;
public: C(){cout<<"C!"<<endl;}
};
int main()
{
B<int,double >obj1;
C<int> obj2;
return 0;
}
2、类模板由模板类派生
//类模板由模板类派生
#include <iostream>
using namespace std;
template <class T1,class T2>
class A{
T1 v1;
T2 v2;
public:
A(){cout<<"A!"<<endl;}
};
template <class T>
class B:public A<int,double >{ // A<int,double >是模板类
T v;
public:
B(){cout<<"B!"<<endl;}
};
int main()
{
B<char> obj1;
return 0;
}
3、类模板从普通类派生
#include <iostream>
using namespace std;
class A{
int v1;
public:
A(){cout<<"A!"<<endl;}
};
template <class T>
class B:public A{//所以从B实例化的类都以A为基类
T v;
public:
B(){cout<<"B!"<<endl;}
};
int main()
{
B<char> obj1;
return 0;
}
4、普通类从模板类派生
//普通类从模板类派生
#include <iostream>
using namespace std;
template <class T>
class A{
T v1;
int n;
public:
A(){cout<<"A!"<<endl;}
};
class B:public A<int> {
double v;
public:
B(){cout<<"B!"<<endl;}
};
int main()
{
B obj1;
return 0;
}