使用模板类,可以使用各种的数据成员对函数进行操作,
模板类的形式
tempalte
#include<iostream>
using namespace std;
template<class T>
class compare
{
public:
compare(T a, T b);
T min();
T max();
private:
T num1;
T num2;
};
template<class T>
compare<T>::compare(T a, T b)
{
num1 = a;
num2 = b;
}
template<class T>
T compare<T>::min()
{
if (num1 > num2)
return num2;
else
return num1;
}
template<class T>
T compare<T>::max()
{
if (num1 > num2)
return num1;
else
return num2;
}
int main()
{
compare<int> com1(20, 10);
cout << "the min of the number" << com1.min() << endl;
cout << "the max of the number" << com1.max() << endl;
compare < double> com2(15.6, 13.8);
cout << "the min of the min number" << com2.min() << endl;
cout << "the max of the number" << com2.max() << endl;
system("pause");
return 0;
}