1.3.7类模板份文件编写
#include
using namespace std;
#include “person.hpp”
//#include
///*
//类模板的份文件编写
//*/
//template <class T1,class T2>
//class person
//{
//public:
// person(T1 name, T2 age);
// void showperson();
// T1 m_name;
// T2 m_age;
//};
//template<class T1,class T2>
//person<T1,T2>::person(T1 name, T2 age)
//{
// this->m_name = name;
// this->m_age = age;
//}
//
//template<class T1, class T2>
//void person<T1, T2>::showperson()
//{
// cout << m_name << endl;
//}
void test01()
{
person<string, int>p(“aa”, 11);
p.showperson();
}
int main()
{
test01();
system("pause");
return 0;
}
1.3.6类模板成员函数的类外实现
#include
using namespace std;
#include
template <class T1,class T2>
class person
{
public:
person(T1 name, T2 age);
//{
// this->m_name = name;
// this->m_age = age;
//}
void showperson();
//{
// cout << this->m_name << endl;
// cout << this->m_age << endl;
//}
T1 m_name;
T2 m_age;
};
//构造函数类外实现
template <class T1,class T2>
person<T1,T2>::person(T1 name, T2 age)
{
this->m_name = name;
this->m_age = age;
}
//成员函数的类外实现
template <class T1, class T2>
void person<T1, T2>::showperson()
{
cout << this->m_name << endl;
cout << this->m_age << endl;
}
void test()
{
person<string,int >p(“sss”, 20);
p.showperson();
}
int main()
{
test();
system("pause");
return 0;
}