类模板成员函数的类外实现(8)

学习目标;

能够掌握类模板中的成员函数类外实现

注意事项:作用域、参数列表

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 template<class T1, class T2>
 6 class Person
 7 {
 8 public:
 9 
10     Person(T1 name, T2 age);
11     /*{
12         this->m_Name = name;
13         this->m_Age = age;
14     }*/
15 
16     void showPerson(void);
17         /*{
18             cout << "姓名:" << this->m_Name << endl;
19             cout << "年龄::" << this->m_Age << endl;
20         }*/
21 
22     T1 m_Name;
23     T2 m_Age;
24 };
25 
26 //构造函数的类外实现
27 template<class T1,class T2>            
28 Person<T1,T2>::Person(T1 name, T2 age) //为了体现类模板,使用Person<T1,T2>参数列表,还要加上作用域 ::
29 {
30     this->m_Name = name;
31     this->m_Age = age;
32 }
33 
34 //成员函数类外实现
35 template<class T1,class T2>
36 void Person<T1,T2>::showPerson() //为了体现类模板,使用Person<T1,T2>参数列表,还要加上作用域 ::
37 {
38     cout << "姓名:" << this->m_Name << endl;
39     cout << "年龄::" << this->m_Age << endl;
40 }
41 
42 void test_01(void)
43 {
44     Person<string,int> p1("Tom",20);
45     p1.showPerson();
46 }
47 
48 int main(void)
49 {
50     test_01();
51 
52     system("pause");
53     return 0;
54 }

 

posted @ 2020-04-27 11:24  坦率  阅读(588)  评论(0)    收藏  举报