C++类模板

 1 //类模板
 2 //语法:template<typename T>
 3 //
 4 #include <iostream>
 5 #include <string>
 6 #include<fstream>
 7 using namespace std;
 8 
 9 //类模板
10 template<class NameType,class AgeType>
11 class Person
12 {
13 public:
14     Person(NameType name,AgeType age)
15     {
16         this->m_Name = name;
17         this->m_Age = age;
18     }
19 
20     void showPerson()
21     {
22         cout << "name: " <<this->m_Name << " age: " <<this->m_Age << endl;
23     }
24     NameType m_Name;
25     AgeType m_Age;
26 
27 
28 };
29 void test01()
30 {
31     Person<string, int> p1("张三", 18);
32     p1.showPerson();
33 }
34 int main()
35 {
36 
37     test01();
38 
39 
40     system("pause");
41 
42     return 0;
43 
44 }

 

posted on 2021-08-12 10:50  Bytezero!  阅读(62)  评论(0编辑  收藏  举报