BZ易风

导航

 

1.

template <class T1, class T2>

Person<T1, T2>::Person(T1 name, T2 age)

#define _CRT_SECURE_NO_WARNINGS
#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();
    T1 m_Name;
    T2 m_Age;
};
//类外实现成员函数
//先定泛型
template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)  //类型后要跟泛型然后再跟作用域
{
    this->m_Name = name;
    this->m_Age = age;
}
template<class T1, class T2>
void Person<T1, T2>::showPerson()
{
    cout << "姓名:" << this->m_Name << " 年龄: " << this->m_Age << endl;
}
void test01()
{
    Person<string, int> p1("大圣", 500);
    p1.showPerson();
}

int main()
{
    test01();
    system("Pause");
    return 0;
}

 

posted on 2021-08-24 17:08  BZ易风  阅读(128)  评论(0编辑  收藏  举报