类模板派生普通类
基本语法如下:
#include"iostream"
using namespace std;
template<typename T>
class A
{
public:
A(T a)
{
this->a = a;
}
T& Geta()
{
return a;
}
void Printa()
{
cout<<a<<endl;
}
private:
T a;
};
//子模版类派生时需要具体化模版类
//c++编译器需要根据父类类型分配子类空间
//从模板类派生普通类
class B:public A<int>
{
public:
B(int a,int b):A(a)
{
this->b = b;
}
void printb()
{
cout<<"b:"<<b<<endl;
}
private:
int b;
};
//类模板的派生
void main()
{
B b(10,20);
b.printb();
system("pause");
}
类模板派生模板类
基本语法如下:
#include"iostream"
using namespace std;
template<typename T>
class A
{
public:
A(T a)
{
this->a = a;
}
T& Geta()
{
return a;
}
void Printa()
{
cout<<a<<endl;
}
private:
T a;
};
//从模板类派生模版类
template<typename T>
class C:public A<int>
{
public:
C(T a,T b):A<T>(a)
{
this->b = b;
}
void printb()
{
cout<<"b:"<<b<<endl;
}
private:
T b;
};
//类模板的派生
void main()
{
C<int> b(10,20);
b.printb();
system("pause");
}