#include<iostream>
#include<string>
using namespace std;
template<class T1, class T2>
class Person
{
public:
//成员函数类内声明
Person(T1 name, T2 age);
void ShowPerson();
T1 MyName;
T2 MyAge;
};
//构造函数类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
this->MyName = name;
this->MyAge = age;
}
//成员函数类外实现
template<class T1, class T2>
void Person<T1, T2>::ShowPerson()
{
cout << "姓名:" << this->MyName << "\t年龄:" << this->MyAge << endl;
}
void test()
{
Person<string, int> p("Tom",20);
p.ShowPerson();
}
int main()
{
test();
return 0;
}
7.类模板成员函数类外实现
最新推荐文章于 2024-05-13 09:55:25 发布