1.源代码
#include <tchar.h>
#include <iostream>
using namespace std;
template<typename T>
T qjh_max(T a, T b)
{
return a > b ? a : b;
}
template<typename T2>
class C_qjh_max
{
public:
C_qjh_max(T2 a, T2 b)
{
m_a = a;
m_b = b;
}
T2 Get_C_qjh_max()
{
return m_a > m_b ? m_a : m_b;
}
private:
T2 m_a;
T2 m_b;
};
int _tmain(int argc, _TCHAR* argv[])
{
cout << "1.使用函数模板" << endl;
int nResult = qjh_max(10, 25);
cout << "nResult = " << nResult << endl;
char cResult = qjh_max('d', 'e');
cout << "cResult = " << cResult << endl << endl;
cout << "2.使用类模板" << endl;
C_qjh_max<int>ljg_max(10, 25);
int nResult2 = ljg_max.Get_C_qjh_max();
cout << "nResult2 = " << nResult2 << endl;
C_qjh_max<char>ljg2_max('d', 'e');
char cResult2 = ljg2_max.Get_C_qjh_max();
cout << "cResult2 = " << cResult2 << endl << endl;
return 0;
}
2.运行结果
