#include<iostream>
#include<string>
using namespace std;
//类模板对象做函数参数
template<class T1,class T2>
class person
{
public:
person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
void showperson()
{
cout << "name= " << this->m_Name << " age= " << this->m_Age << endl;
}
T1 m_Name;
T2 m_Age;
};
//1、指定传入类型 使用比较广泛
void printperson1(person<string, int>&p)
{
p.showperson();
}
void test01()
{
person<string, int>p1("Tom", 19);
printperson1(p1);
}
//参数模板化
template<class T1,class T2>
void printperson2(person<T1, T2>&p)
{
p.showperson();
cout << "T1的类型为: " << typeid(T1).name() << endl;
cout << "T2的类型为: " << typeid(T2).name() << endl;
}
void test02()
{
person<string, int>p2("Jerry", 20);
printperson2(p2);
}
//3、整个类模板化
template<class T>
void printperson3(T & p)
{
p.showperson();
cout << "T的类型为: " << typeid(T).name() << endl;
}
void test03()
{
person<string, int>p3("Ann", 20);
printperson3(p3);
}
int main()
{
test01();
test02();
test03();
system("pause");//按任意键继续
return 0;//关闭程序
}