(一一三)第九章复习题

简介:

1.对于下面的情况,应使用哪种存储方案?

ahomer是函数的形参。

bsecret变量由两个文件共享。

ctopsercret变量由一个文件中所有函数共享,但对于其他文件来说是隐藏的。

dbeencalled记录包含他的函数被调用的次数。

 

答:a。自动变量;b。外部变量;c。静态内部变量;d。无链接性的静态变量。

c补充:也可以在一个未命名的名称空间中定义

 

2using声明和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

 

 

目录
相关文章
|
4月前
|
C语言
第三章 C程序设计
第三章 C程序设计
22 0
|
编译器 C++
C++复习题
C++复习题
40 0
|
Java
java编程思想第四版第十一章习题
运行结果分析: 这个案例的重点是, 数组瘦受限制的, 集合是没有元素个数限制的。
201 0
|
设计模式 Java
java编程思想第四版第九章习题
输出结果: 调用基类构造方法的时候, 只是给子类的成员变量分配了一块内存空间, 并将内存空间的值设置为默认值0.
95 0
|
Java
java编程思想第四版第三章要点习题
输出结果: 这个结果需要特别说明一下, String是特殊的引用类型, 当他被直接赋值时,就是把这个值对应的引用位置赋值给String变量了, 所以, 两次结果都是true。 如果你用new String()赋值, 结果就不同了.
122 0
|
Web App开发 网络协议 关系型数据库
|
Perl Unix 算法
《编程珠玑(续)(修订版)》—第1章1.7节深入阅读
关于“动态统计”的第3节讨论了行计数和过程时间计数,以及用这两种计数搜集的统计数据。
1553 0