}
float Distance::fDist(Point p1,Point p2) { }
double x,y; x=p1.X -p2.X ; y=p1.Y -p2.Y ;
return float(sqrt(x*x+y*y)); Distance d;
cout<<\; cout< A.设置参数默认值时,应当全部设置 B.设置参数默认值后,调用函数不能再对参数赋值 C.设置参数默认值时,应当从右向左设置 D.只能在函数定义时设置参数默认值 9.74 10.13 思考题 1. 利用重载求两个整数、三个整数和四个整数的最小 值。 #include %using namespace std; 第3章 函数重载与内联函数 1.为什么要进行函数重载? 解:当函数实现的是同一类功能,只是部分细节不同(如参数的个数或参数类型不同)时,利用函数重载机制可以将这些函数取成相同的名字,从而使程序易于阅读和理解,方便记忆和使用。 2.什么是内联函数? 解:内联函数是一个函数,它与一般函数的区别是在使用时可以像宏一样展开,所以没有函数调用的开销。因此,使用内联函数可以提高系统的执行效率。 3.函数重载的意义主要在于( A )。 A.使用方便,提高可读性 执行效率 C.减少存储空间开销 程序可靠性 4.下面关于重载函数的说法中正确的是( C )。 A.重载函数一定具有不同的返回值类型 D.提高B.提高 int Min(int x1,int x2); int Min(int x1,int x2,int x3); int Min(int x1,int x2,int x3,int x4); int main() { } int Min(int x1,int x2) { } int Min(int x1,int x2,int x3) return (x1 cout<<\< { B.重载函数形参个数一定不同 C.重载函数一定有不同的形参列表 int y; y=x1 D.重载函数名可以不同 } 5.一个函数功能不太复杂,但要求被频繁调用,选用( A )。 A.内联函数 D.嵌套函数 B.重载函数 C.递归函数 int Min(int x1,int x2,int x3,int x4) { } int y1,y2; y1=x1 6.将函数声明为内联函数的关键字是( C )。 A.register B.static C.inline D.extern B.开关语句 C.赋值语句 7.在内联函数内允许使用的是( C )。 A.循环语句 D.以上都允许 8.在C++中,下列关于参数默认值的描述中正确的是( C )。 2.利用重载计算长方形、正方形、圆、梯形的面积和体积。 #include \ 5 / 15 using namespace std; float Area(float x,float y); float Area(float x); float Area(float r,const double pi); float Area(float a,float b,float h); float Volumn(float a,float b,float h); float Volumn(float a); float Volumn(float r,const float pi); float Volumn(float a,float b,float a1,float b1,float h); int main() { } float Area(float x,float y) { } float Area(float x) { return x*x; return x*y; float x,y,z,x1,y1; const float PI=3.14; cout<<\计算面积---------\< cout<<\长方形的面积是:\<>x; cout<<\正方形的面积是:\<>x; cout<<\圆的面积是:\<>x>>y>>z; cout<<\梯形的面积是:\<>x>>y>>z; cout<<\长方形的体积是:\< cout<<\正方形的体积是:\< cout<<\圆的体积是:\< cout<<\输入上底边长a、b,下底边长a1、b1,以及高\< cout<<\梯形的体积是:\< } float Area(float r,const double pi) { } float Area(float a,float b,float h) { } float Volumn(float a,float b,float h) { } float Volumn(float a) { } float Volumn(float r,const float pi) { } float Volumn(float a,float b,float a1,float b1,float h) { } return h*(a*b+(a+a1)*(b+b1)+a1*b1)/6.0; return 4*pi*r*r*r/3; return a*a*a; return a*b*h; return (a+b)*h/2; return pi*r*r; 3.利用重载实现对10个整数和10个实数的排序。 #include %using namespace std; void sort(int a[],int n); void sort(float a[],int n); int main() { 6 / 15 const int n=10; int i,a[n]; float b[n]; cout<<\输入\< cin>>a[i]; sort(a,n); cout<<\排序结果为:\< cout< cout<<\输入\< cin>>b[i]; sort(b,n); cout<<\排序结果为:\< } void sort(int a[],int n) { } void sort(float a[],int n) { } int i,j; float t; for (i=0;i for (j=i+1;j
相关推荐: