📄 408.cpp
字号:
/*
408.cpp Written By S.Y.Feng
DEMO implement one interface of a group of functions
by using function(return a Larger) template
*/
#include <iostream.h>
#include <string.h>
#include <cstring.h>
char * Larger(char * x,char * y);
string Larger(string x,string y);
template <class Type> Type Larger(Type a,Type b);
main(void)
{
int i1 = 10 ,i2 = 20 ;
cout << i1 << " and " << i2
<<" The Larger is " << Larger(i1,i2) << endl;
float f1 = 11.1, f2 = 2.2 ;
cout << f1 << " and " << f2
<<" The Larger is " << Larger(f1,f2) << endl;
char c1 = 'X', c2 = 'Y' ;
cout << c1 << " and " << c2
<<" The Larger is " << Larger(c1,c2) << endl;
string s1="Wang", s2="Lee";
cout << s1 << " and " << s2
<<" The Larger is " << Larger(s1,s2) << endl;
char * s3="AAA", * s4="bbb";
cout << s3 << " and " << s4
<<" The Larger is " << Larger(s3,s4) << endl;
return 0;
}
template <class Type>
Type Larger(Type a,Type b)
{
if (a>b) return a;
else return b;
}
char * Larger(char * x,char * y)
{
if (strcmp(x,y)>0) return x;
else return y ;
}
string Larger(string x,string y)
{
if (x>y) return x;
else return y ;
}
/*上面程序运行的结果如下:
10 and 20 The Larger is 20
11.1 and 2.2 The Larger is 11.1
X and Y The Larger is Y
Wang and Lee The Larger is Wang
AAA and bbb The Larger is bbb
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -