C++语言-函数缺省、重载及模板课案.pptx

闫治国 同济大学 地下建筑与工程系;函数(2) 1 函数参数缺省 2 函数重载 3 函数模板;C++对C的扩充: 带缺省参数的函数 C++通过在形参表列中对若干个形参指定缺省值,允许实参、形参个数不等 int fun(int a, int b, int c=100); 两种调用方式: fun(10,20,30); //a=10,b=20,c=30 fun(10,20); //a=10,b=20,c=100;#include iostream.h #include math.h double s(double x , double eps=1e-6) {int n=1; double w=0.0, t=1.0 ; while(fabs(t)=eps) {w+=t; t=t*x/(n++); } return w; }; 缺省参数的说明必须出现在函数调用之前;或在函数说明中给出缺省值。 函数说明与函数定义中的缺省值不一致时,以函数说明中为准。 缺省参数可以指定多个,但须放在形参表列中的最右端。;;int max(int a,int b,int c){cout“函数A”;} int max(int a,int b){cout“函数B”;} float max(float a??float b

文档评论(0)

1亿VIP精品文档

相关文档