1模板类继承普通类(常见)
cpp
#include<iostream>
using namespace std;
class AA
{
public:
int m_a;
AA(int a) :m_a(a) { cout << "调用了AA的构造函数\n"; }
void func1() { cout << "调用func1()函数 :m_a = " << m_a << endl; }
};
template <class T1,class T2>
class BB
{
public:
T1 m_x;
T2 m_y;
BB(const T1 x, const T2 y) :m_x(x), m_y(y) { cout << "调用了BB的构造函数,\n"; }
void func2() { cout << "调用了func2()函数:x = " << m_x << "y = "<<m_y<<endl; ; }
};
int main() {
BB<int, string> bb(8, "zhongge");
bb.func2();
return 0;
}
现在AA BB没有任何关系,如果把普通类作为基类,把模板类BB作为子类,继承AA。
cpp
#include<iostream>
using namespace std;\
class AA
{
public:
int m_a;
AA(int a) :m_a(a) { cout << "调用了AA的构造函数\n"; }
void func1() { cout << "调用func1()函数 :m_a = " << m_a << endl; }
};
template <class T1,class T2>
class BB:public AA
{
public:
T1 m_x;
T2 m_y;
BB(const T1 x, const T2 y) :m_x(x), m_y(y) { cout << "调用了BB的构造函数,\n"; }
void func2() { cout << "调用了func2()函数:x = " << m_x << "y = "<<m_y<<endl; ; }
};
int main() {
BB<int, string> bb(8, "zhongge");
bb.func2();
return 0;
}
让BB继承AA但是出现了错误:AA没有合适的默认的构造函数。
继承的时候派。生类如何构造基类:要在派生类BB构造函数的初始化列表中指明基类的构造函数。
cpp
#include<iostream>
using namespace std;\
class AA
{
public:
int m_a;
AA(int a) :m_a(a) { cout << "调用了AA的构造函数\n"; }
void func1() { cout << "调用func1()函数 :m_a = " << m_a << endl; }
};
template <class T1,class T2>
class BB:public AA
{
public:
T1 m_x;
T2 m_y;
BB(const T1 x, const T2 y,int a) :AA(a),m_x(x), m_y(y) { cout << "调用了BB的构造函数,\n"; }
void func2() { cout << "调用了func2()函数:x = " << m_x << " ,y = "<<m_y<<endl; ; }
};
int main() {
BB<int,string> bb(8, "zhongge",3);
bb.func2();
bb.func1();
return 0;
}
cpp
调用了AA的构造函数
调用了BB的构造函数,
调用了func2()函数:x = 8 ,y = zhongge
调用func1()函数 :m_a = 3
C:\Users\代伟业\Desktop\C++\初始化列表\ConsoleApplication1\x64\Debug\ConsoleApplication1.exe (进程 19556)已退出,代码为 0。
按任意键关闭此窗口. . .
2普通类继承模板类的实例版本
cpp
#include<iostream>
using namespace std;
template <class T1, class T2>
class BB
{
public:
T1 m_x;
T2 m_y;
BB(const T1 x, const T2 y) : m_x(x), m_y(y) { cout << "调用了BB的构造函数,\n"; }
void func2() { cout << "调用了func2()函数:x = " << m_x << " ,y = " << m_y << endl; ; }
};
class AA :public BB <int,string>
{
public:
int m_a;
AA(int a,int x,string y) :BB(x,y),m_a(a) { cout << "调用了AA的构造函数\n"; }
void func1() { cout << "调用func1()函数 :m_a = " << m_a << endl; }
};
int main() {
AA aa(3,8,"zhongge");
aa.func2();
aa.func1();
return 0;
}
cpp
调用了BB的构造函数,
调用了AA的构造函数
调用了func2()函数:x = 8 ,y = zhongge
调用func1()函数 :m_a = 3
C:\Users\代伟业\Desktop\C++\初始化列表\ConsoleApplication1\x64\Debug\ConsoleApplication1.exe (进程 7360)已退出,代码为 0。
按任意键关闭此窗口. . .
3普通类继承模板类
cpp
#include<iostream>
using namespace std;
template <class T1, class T2>
class BB
{
public:
T1 m_x;
T2 m_y;
BB(const T1 x, const T2 y) : m_x(x), m_y(y) { cout << "调用了BB的构造函数,\n"; }
void func2() { cout << "调用了func2()函数:x = " << m_x << " ,y = " << m_y << endl; ; }
};
template <class T1, class T2>
class AA :public BB <T1,T2>
{
public:
int m_a;
AA(int a,T1 x,T2 y) :BB<T1,T2>(x,y),m_a(a) { cout << "调用了AA的构造函数\n"; }
void func1() { cout << "调用func1()函数 :m_a = " << m_a << endl; }
};
int main() {
AA<int,string> aa(3,8,"zhongge");
aa.func2();
aa.func1();
return 0;
}
4模板类继承模板类。
cpp
#include<iostream>
using namespace std;
template <class T1, class T2>
class BB
{
public:
T1 m_x;
T2 m_y;
BB(const T1 x, const T2 y) : m_x(x), m_y(y) { cout << "调用了BB的构造函数,\n"; }
void func2() { cout << "调用了func2()函数:x = " << m_x << " ,y = " << m_y << endl; ; }
};
template <class T1, class T2>
class AA :public BB <T1,T2>
{
public:
int m_a;
AA(int a,T1 x,T2 y) :BB<T1,T2>(x,y),m_a(a) { cout << "调用了AA的构造函数\n"; }
void func1() { cout << "调用func1()函数 :m_a = " << m_a << endl; }
};
template <class T,class T1,class T2>
class CC:public BB<T1,T2>
{public:
T m_a;
CC(const T a, const T1 x,const T2 y) :BB<T1, T2>(x, y), m_a(a) { cout << "调用leCC的构造函数\n"; }
void func3() { cout << "调用func1()函数 :m_a = " << m_a << endl; }
};
int main() {
AA<int,string> aa(3,8,"zhongge");
aa.func2();
aa.func1();
CC <int,int,string> cc(3, 8, "我是一只傻傻鸟");
return 0;
}
5模板类继承模板参数给出的基类(不能是模板类)
cpp
#include<iostream>
using namespace std;
class AA
{
public:
AA(){ cout << "调用leAA的构造函数\n"; }
AA(int a) { cout << "调用leAA(int a)的构造函数"; }
};
class BB
{
public:
BB() { cout << "调用leBB的构造函数\n"; }
BB(int a) { cout << "调用leBB(int a)的构造函数"; }
};
template <class T>
class CC:public T
{public:
CC():T() { cout << "调用leCC的构造函数\n"; }
CC(int a):T(a) { cout << "调用leCC(int a)的构造函数"; }
};
int main() {
CC<AA> ca1;//AA作为基类
CC<BB> cb2;//BB作为基类
return 0;
}