(一一三)第九章复习题

来源:互联网 发布:2015中国服务贸易数据 编辑:程序博客网 时间:2024/05/17 01:23

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

 

 

0 0
原创粉丝点击