C++学习之路(九),C++primer plus 第九章 内存模型和名称空间--复习题

在这里插入图片描述
  距离上次学习又双叒叕过去三个月了,之前一直都想继续来着,只怪自己定力不够,自制力不行,虽然对这个很有兴趣,但是学习起来又是一直那么的枯燥,一直在偷懒,虽然网上都说这本书好,但是这本书有一个致命的缺点,就是太过于厚了,一本书差不多1000页,学习起是有点打击信心了,哎,不说了,继续。。。。

C++学习之路(九),C++primer plus 第九章 内存模型和名称空间--C++的编程模块-编程练习

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

a. homer 是函数的形参。
b. sercret 变量由两个文件黄离。
c. topsecret 变量由一个文件中的所有函数共享,但对于其他文件来说是隐藏的。
d. beencalled 记录包含它的函数被调用的次数。

答: a.自动变量;自动变量一般就是局部变量
b.外部变量;外部变量是在函数外部定义的变量,它的作用域 是从变量的定义处开始,到本程序文件的结尾
c.静态内部变量;static 用修饰符来在函数内部声明的变量
d.无链接性的静态变量。

2. using 声明和 using 编译指令之间有何区别?

答:using 声明可以单独使用名称空间中某个特写的名称,其作用域与 using 所在声明区相同。例如 using std::;
using 编译指令使用得名称空间中的所有名称可用。如果全剧终使用 using 编译指令,将使该 名称空间中的名称在全局可用。例如 using namespace std;
使用 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;
}

下面是不使用 using 声明和 using 编译指令的代码:

#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;
	}
	count << "Value = " << x << endl;
	
	return 0;
}

下面是使用 using 声明替换 using 编译指令的代码

#include <iostream>
using std :: cout;
using std :: cin;
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;
}

刚开始声明的时候只声明了前面两个,竟然把 using std :: endl; 这行代码给忘了。

5.在一个文件中调用 arerage(3, 6) 函数时,它返回两个 int 参数的 int 平均值,在同一个程序的另一个文件中调用时,它返回两个 int 参数的 double 平均值。应如何实现?

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 << ", " << endl;
}

答:我的答案是
10
4
随机数,因为 y 虽然声明了却没有赋值,系统应该会给一个随机值。
Other: 10,1
another: 10, -4

正确答案是:
10
4
0(实参会自定义初始化为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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值