008模板类派生时需要具体化模板类C编译器需要知道父类的数据类型具体是什么样子

#include<iostream>
using namespace std;
//A编程模板 类 
//模板类  类型参数化

//类模板的定义
//类模板的使用
//类模板 做函数参数

//模板类 
template <class T>
class A
{
public:
	A(T a,T c)
	{
		this->a = a;
		this->c = c;
	}
	void printA()
	{
		cout << "a=" << a << endl;
		cout << "c=" << c << endl;
	}
protected:
	T a;
	T c;
};
//从模板类 派生了 普通类
// 模板类派生时, 需要具体化模板类. C++编译器需要知道 父类的数据类型具体是什么样子的
//=====> 要调用父类的构造函数,要知道父类所占的内存大小是多少 只有数据类型固定下来,才知道如何分配内存 

class B:public A<int>
{
public:
	B(int aaa=10, int ccc=0,int b=0):A<int>(aaa,ccc)//需要把数据类型具体化
	{
		this->b = b;
	}

	void printB()
	{
		cout << "b=" << b << endl;
		cout << "a=" << a << endl;
		cout << "c=" << c << endl;

	}
private:
	int b;
};
int main(void)
{
	B b1(1, 2, 3);
	b1.printB();
	cout << "________________________" << endl;
	b1.printA();//子类可以调用父类的成员函数

	system("pause");
	return 0;
}

/*
 *
 * b=3
a=1
c=2
________________________
a=1
c=2
请按任意键继续. . .
当进行修改的时候,注意在重新生成的时候要选择--“生成”--“重新编译”
这是因为类模板二次编译的问题
SunRise于东北电力大学第二教学楼1121
 */

猜你喜欢

转载自blog.csdn.net/baixiaolong1993/article/details/89482587