类模板类外实现具体实现步骤

版权声明:所有博客均由作者本人原创,若要转载,请注明出处。谢谢 https://blog.csdn.net/LU_Leo/article/details/82711695
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
template <class T1,class T2>
class Person
{
	private:
	T1 m_name;
	T2 m_age;
public:
	template<class T1,class T2>
	friend ostream& operator<<(ostream& os, Person<T1,T2>& p);
	Person(T1 name, T2 age);
	void show();

};
template<class T1, class T2>
ostream& operator<<(ostream& os, Person<T1,T2>& p)
{
	os<< p.m_name << " is " << p.m_age << " years old" << endl;
	return os;
}
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>::show()
{
	cout << m_name << " is " << m_age << " years old" << endl;
}
void test01()
{
	Person<string,int> p("LiPing", 19);
	/*p.show();*/
	cout << p;
}
int main()
{
	test01();

	return 0;
}

猜你喜欢

转载自blog.csdn.net/LU_Leo/article/details/82711695