(一一三)第九章复习题
来源:互联网 发布:2015中国服务贸易数据 编辑:程序博客网 时间:2024/05/17 01:23
1.对于下面的情况,应使用哪种存储方案?
a。homer是函数的形参。
b。secret变量由两个文件共享。
c。topsercret变量由一个文件中所有函数共享,但对于其他文件来说是隐藏的。
d。beencalled记录包含他的函数被调用的次数。
答:a。自动变量;b。外部变量;c。静态内部变量;d。无链接性的静态变量。
c补充:也可以在一个未命名的名称空间中定义
2。using声明和using编译指令有何区别?
答:
①using声明只使用名称空间中的一个名称,using编译指令使用名称空间中的所有名称;
②using声明在遇见同名内部变量(但外部变量不会发生这种情况)时,可能导致名称冲突(使用外部时,同名外部会被隐藏);但using编译指令不会,他会隐藏外部,或者被内部隐藏。
补充③using声明其作用域与using声明所在的声明区域相同(我知道,但我觉得没必要强调,就相当于声明了一个变量一样)。using编译指令就像在一个包含using声明和名称空间本身的最小声明区域中声明了这些名称一样。
3。重新编写下面的代码,使其不使用using声明和using编译指令。
#include<iostream>
using namespace std;
int main()
{
double x;
cout << "Enter value: ";
while (! (cin>>x) )
{
cout << "Bad input. Please enter a number: ";
cin.clear();
while (cin.get() != '\n' )
continue;
}
cout << "Value = " << x << endl;
return 0;
}
答:修改为:
#include<iostream>
int main()
{
double x;
std::cout << "Enter value: ";
while (! (std::cin>>x) )
{
std::cout << "Bad input. Please enter a number: ";
std::cin.clear();
while (std::cin.get() != '\n' )
continue;
}
std::cout << "Value = " << x << std::endl;
return 0;
}
4。重新编写下面的代码,使之使用using声明,而不是using编译指令。
#include<iostream>
using namespace std;
int main()
{
double x;
cout << "Enter value: ";
while (! (cin>>x) )
{
cout << "Bad input. Please enter a number: ";
cin.clear();
while (cin.get() != '\n' )
continue;
}
cout << "Value = " << x << endl;
return 0;
}
答:修改为:
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
double x;
cout << "Enter value: ";
while (! (cin>>x) )
{
cout << "Bad input. Please enter a number: ";
cin.clear();
while (cin.get() != '\n' )
continue;
}
cout << "Value = " << x << endl;
return 0;
}
5。在一个文件中调用average(3,6)函数时,它返回两个int参数平均值,在同一个程序的另一个文件中调用时,它返回两个int参数的double平均值。应如何实现?
答:需要将两个函数的链接性变为内部。具体方式为:
第一个文件,使用函数原型:static int average(int a,int b);然后在该文件中加入函数定义;
第二个文件,使用函数原型:static double average(int a,int b); 然后写对应的函数定义,并加入到该文件之中。
补充:也可以在未命名的名称空间中包含定义
6.下面的程序由两个文件组成,该程序显示什么内容?
//file1.cpp
#include<iostream>
using namespace std;
void other();
void another();
int x = 10;
int y;
int main()
{
cout <<x <<endl;
{
int x = 4;
cout << x << endl;
cout << y << endl;
}
other();
another();
return 0;
}
void other()
{
int y = 1;
cout << "Other: " << x << ", " << y << endl;
}
//file2.cpp
#include<iostream>
using namespace std;
extern int x;
namespace
{
int y = -4;
}
void another()
{
cout << "another() " << x << ", " << y << endl;
}
答:显示为:
10
4
0
Other: 10, 1
another(): 10, -4
7。下面的代码将显示什么内容?
#include <iostream>
using namespace std;
void other();
namespace n1
{
int x = 1;
}
namespace n2
{
int x = 2;
}
int main()
{
using namespace n1;
cout << x << endl;
{
int x = 4;
cout << x << ", " << n1::x << ", " << n2::x << endl;
}
using n2::x;
cout << x << endl;
other();
return 0;
}
void other()
{
using namespace n2;
cout << x << endl;
{
int x = 4;
cout << x << ", " << n1::x << ", " << n2::x << endl;
}
using n2::x;
cout << x <<endl;
}
显示:
1
4, 1, 2
2
2
4, 1, 2
2
- (一一三)第九章复习题
- 第九章 复习题
- (一一四)第九章编程练习
- 数据结构复习题(三)
- Java复习题集(三)
- 复习题三
- 计算机网络自顶向下方法之一一一第九章
- 《C++ Primer Plus(第六版)》(14)(第九章 内存模型和命名空间 复习题答案)
- (六十三)第四章复习题
- (七十九)第五章复习题
- (九十)第六章复习题
- (一四四)第十三章复习题
- 黑马程序员一一Java基础语法(三)
- AngualrJS的学习记录(三)一一AngularJS指令小结
- (一〇〇)第七章复习题
- (一〇七)第八章复习题
- (一二一)第十章复习题
- (一二五)第十一章复习题
- 【iOS开发】值对象--NSValue,NSNumber
- Andorid 实现抢购倒计时能(天时分秒)
- 数据结构.字符串.模式匹配算法
- sed命令n,N,d,D,p,P,h,H,g,G,x解析
- r语言入门系列3
- (一一三)第九章复习题
- ScrollView
- (一一四)第九章编程练习
- 重新出发
- java包用法(课堂)
- view爆炸的烟花效果
- Python数值问题的一些问题
- 图片加载到内存优化
- LayoutTransition 和 SizeChange