1、给出一个模板基类 michaelObj,它包含一个纯虚函数和一个成员函数。
#include <iostream>
template<typename T1,typename T2>
class michaelObj
{
public:
michaelObj(T1,T2);
~michaelObj(void);
public:
void showContent();
virtual void showVir() = 0;
public:
T1 m_ele1;
T2 m_ele2;
};
template<typename T1,typename T2>
michaelObj<T1,T2>::michaelObj(T1 p1,T2 p2)
{
m_ele1=p1;
m_ele2=p2;
}
template<typename T1,typename T2>
michaelObj<T1,T2>::~michaelObj(void)
{
}
template<typename T1,typename T2>
void michaelObj<T1, T2>::showContent()
{
std::cout<<"hello"<<std::endl;
}
2、继承类newObj,使用继承类型 int 实例化模板类michaelObj
.h文件定义:
#include "michaelobj.h"
class newObj :public michaelObj<int,int>
{
public:
newObj(int ,int );
~newObj(void);
public:
void showC2();
void showVir();
};
.cpp文件定义
newObj::newObj(int a,int b):michaelObj<int,int>(a,b)
{
}
newObj::~newObj(void)
{
}
void newObj::showC2()
{
std::cout<<__FUNCDNAME__<<std::endl;
}
void newObj::showVir()
{
std::cout<<m_ele1<<" and "<<m_ele2<<std::endl;
}
3、测试代码
int main()
{
newObj newone(13,14);
newone.showVir();
newone.showC2();
}