正在加载图片...
第一节模板的概念 #includesiostream.h> #<.h> template <class T> eI max(Ix,I y return x>y?x:y;) void main( { int il=10,i2=20; float fl=30.5,f2=13.9 double dl=48.255,d2=36358; char cl='A’,c2=a cout<< the max of il, i2 is: <<max(i1, i2)<<endl; cout <<the max of fl, f2 is: <<max(f1, f2)<<endl cout <<the max of dl, d2 is: <<max(dl, d2)<<endl; cout <<the max of cl, c2 is: " <<max(cl, c2)<<endl;第一节 模板的概念 二、模板的概念 ➢例【6.1】用函数模板实现不同类型的二个数最大值 的计算。 #include<iostream.h> #include<string.h> template <class T> T max(T x,T y) {return x>y?x:y;} void main() {int i1=10,i2=20; float f1=30.5,f2=13.9; double d1=48.255,d2=36.358; char c1='A',c2='a'; cout <<"the max of i1,i2 is:"<<max(i1,i2)<<endl; cout <<"the max of f1,f2 is:"<<max(f1,f2)<<endl; cout <<"the max of d1,d2 is:"<<max(d1,d2)<<endl; cout <<"the max of c1,c2 is:"<<max(c1,c2)<<endl; }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有