#include <iostream>
#include<string>
using namespace std;
//类模板
template<class T1, class T2>
class Person
{
private:
T1 name;
T2 age;
public:
Person(T1 name, T2 age)
{
this->name = name;
this->age = age;
}
void showPerson()
{
cout<<"姓名:"<<name<<", 年龄:"<<age<<endl;
}
};
int main(int argc, char *argv[])
{
Person<string, int> lucy("德玛西亚", 18);
lucy.showPerson();
Person<int,int> bob(100,200);
bob.showPerson();
Person<char, int> tom('A', 18);
tom.showPerson();
return 0;
}
类模板举例
最新推荐文章于 2022-05-06 10:25:47 发布