//《C++程式設計——資料結構與程式設計方法》例15.8
//利用函數重載技術,求兩個整數、字元、浮點數或字元串中的較大值,需要編寫4個函數larger。
//而C++通過提供函數模闆,簡化了重載函資料的過程。
#include <iostream>
using namespace std;
template<class Type>//Type,模闆的形參,用于确定函數的形參,傳回值類型,還用于在函數中聲明變量。
Type larger(Type x,Type y);
int main()
{
cout<<"Line 1: Larger of 5 and 6 ="<<larger(5,6)<<endl;
//由于5和6是int類型,編譯器會自動用int取代Type,産生相應代碼,以下同理。
cout<<"Line 2: Larger of A and B ="<<larger('A','B')<<endl;
cout<<"Line 3: Larger of 5.6 and 3.2 ="<<larger(5.6,3.2)<<endl;
char str1[]="Hello";
char str2[]="Happy";
cout<<"Line 6: Larger of "<<str1<<" and "<<str2<<" = "<<larger(str1,str2)<<endl;
return 0;
}
template<class Type>
Type larger(Type x,Type y)
{
if(x>=y)
return x;