第2次课堂练习题

一、判断题

1.重载函数可以带有默认值参数,但是要注意二义性。(T)

2.通过命名空间可以区分具有相同名字的函数(T)

3.通过命名空间可以区分具有相同名字的函数(T)

4.符号常量在定义时一定要初始化。(T)

二、单选题

2-1
命名空间应用于:(B)

A.在类外定义类的成员函数
B.避免各个不同函数、变量等的名称冲突
C.提高代码的执行速度
D.以上答案都正确

2-2
如果在函数中定义的局部变量与命名空间中的变量同名时,(B)被隐藏。

A.函数中的变量
B.命名空间中的变量
C.两个变量都
D.两个变量都不

2-3
如果程序中使用了using命令同时引用了多个命名空间,并且命名空间中存在相同的函数,将出现:(A)

A.编译错误
B.语法错误
C.逻辑错误
D.无法判定错误类型

2-4
要说明标识符是属于哪个命名空间时,需要在标识符和命名空间名字之间加上:(A)

A.::
B.->
C…
D.( )

2-5
如果默认参数的函数声明为“ void fun(int a,int b=1,char c=‘a’,float d=3.2);”,
则下面调用写法正确的是(B )。

A.fun();
B.fun(2,3);
C.fun(2, ,‘c’,3.14)
D.fun(int a=1);

三、填空题

1.(6分)A pointer allows us to pass potentially large amounts of data around at low cost: instead of copying the data we simply pass its address as a pointer value. The type of the pointer determines what can be done to the data through the pointer.

2.(8分)Like a pointer, a reference is an alias for an object, is usually implemented to hold a machine address of an object, and does not impose performance overhead compared to pointers, but it differs from a pointer in that:You access a reference with exactly the same syntax as the name of an object. A reference always refers to the object to which it was initialized There is no “null reference,” and we may assume that a reference refers to an object.

3.(6分)A reference is an alternative name for an object, an alias. The main use of references is for specifying arguments and return values for functions in general and for overloaded operators in particular.

四、程序填空题

5-1
使用动态内存1
分数 10
作者 张德慧
单位 西安邮电大学
填空使下列程序完整并能正确运行。

#include
using namespace std;

int main ( )
{
double* pDouble = NULL; // initialized with null
pDouble =
new double
; // Request memory for the variable

char* pChar =
NULL
; // initialized with null
pChar = new char[20]; // Request memory for the array

cin>>*pDouble >>pChar; // Store value at allocated address
cout << *pDouble<<endl << pChar<<endl ;

delete pDouble; // free up the memory.

delete [] pChar
; // free the memory pointed to by pChar

return 0;
}

五、函数题

6-1 面积计算器(函数重载)

分数 10
作者 何振峰
单位 福州大学
实现一个面积计算器,它能够计算矩形或长方体的面积。

函数接口定义:
int area(int x, int y);
int area(int x, int y, int z);
第一个函数计算长方形的面积,其中x和y是长和宽。第二个函数计算长方体的表面积,x,y和z是长,宽和高。

裁判测试程序样例:
#include
#include
using namespace std;
int area(int,int);
int area(int,int,int);
int main()
{
int i, repeat, c, x, y, z;
cin>>repeat;
for(i=0;i<repeat;i++){
cin>>c;
if(c2){
cin>>x>>y;
cout<<area(x,y)<<endl;
}
if(c
3){
cin>>x>>y>>z;
cout<<area(x,y,z)<<endl;
}
}
return 0;
}

/* 请在这里填写答案 */
输入样例:
2
2 1 2
3 2 3 4
输出样例:
2
52

int area(int x,int y){
    return x*y;
}
int area(int x,int y,int z){
    return (x*y+x*z+z*y)*2;
}

6-2 求最大值和最小值

分数 10
作者 张德慧
单位 西安邮电大学
本题要求实现一个函数f,可找出10个整数中最大值max和最小值min。

函数接口定义:
在主函数中将以下列形式调用该函数
f(a,10,max,min);
例如:其中a是数组名,max用来保存最大值,min用来保存最小值。

裁判测试程序样例:
#include
using namespace std;
/* 你提交的代码将被嵌入到这里 */

int main( )
{
int a[10];
int max,min,i;
for(i=0;i<10;i++){
cin>>a[i];
}
f(a,10,max,min);
cout<<"Max: "<<max<<endl;
cout<<"Min: "<<min<<endl;
return 0;
}
输入样例:
2 5 8 1 4 7 3 6 9 0
输出样例:
Max: 9
Min: 0

void f(int * a,int b,int &max,int &min)
{
    max = min = a[0];
    for(int i=1;i<10;i++)
    {
        if(max<a[i])
        {
            max = a[i];
        }
        if(min>a[i])
        {
            min = a[i];
        }
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值