《C++ Primer Plus(第六版)》(14)(第九章 内存模型和命名空间 复习题答案)

9.5复习题

1.

a自动存储,自动成为自动变量,不需要特别声明。

b外联的静态存储,在一个文件中定义为外部变量,另一个文件中使用extern来声明。

c内联的静态存储,加上static来声明,也可以使用一个未命名的名称空间来定义。

d无链接的静态存储,在函数中声明,要加上static。


2.

using声明,指定了特定的变量可用,在同一个代码块中不能再次声明同样名字的变量

using编译指令,使用整个命名空间,可以声明本地的同样名称的变量,会屏蔽外面的变量


3.

原来的:

#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.

修改前:

//
//  main.cpp
//  HelloWorld
//  关注微信公众号:传说之路,大家共同学习
//  Created by feiyin001 on 16/4/3.
//  Copyright (c) 2016年 FableGame. All rights reserved.
//

#include <iostream>
#include <cstring>
using namespace std;


int main(int argc, const char * argv[]) {
    
    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;
}
修改后:

//
//  main.cpp
//  HelloWorld
//  关注微信公众号:传说之路,大家共同学习
//  Created by feiyin001 on 16/4/3.
//  Copyright (c) 2016年 FableGame. All rights reserved.
//

#include <iostream>

int main(int argc, const char * argv[]) {
    
    using std::cin;
    using std::cout;
    using std::endl;
    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.在不同文件中,不同时被include,就没有问题的。如果都要被include了,就只能引入命名空间了。
Test1.h

//
//  Test1.h
//  HelloWorld
//
//  Created by feiyin001 on 16/12/17.
//  Copyright (c) 2016年 FableGame. All rights reserved.
//

#ifndef __HelloWorld__Test1__
#define __HelloWorld__Test1__

namespace Test1
{
    
    int ave(int a , int b)
    {
        return (a + b) /2;
    }
}


#endif /* defined(__HelloWorld__Test1__) */

Test2.h

//
//  Test1.h
//  HelloWorld
//
//  Created by feiyin001 on 16/12/17.
//  Copyright (c) 2016年 FableGame. All rights reserved.
//

#ifndef __HelloWorld__Test2__
#define __HelloWorld__Test2__
namespace Test2
{
    double ave(int a , int b)
    {
        return (a + b + 0.0) /2;
    }
}


#endif /* defined(__HelloWorld__Test2__) */

main.cpp

//
//  main.cpp
//  HelloWorld
//  关注微信公众号:传说之路,大家共同学习
//  Created by feiyin001 on 16/4/3.
//  Copyright (c) 2016年 FableGame. All rights reserved.
//

#include <iostream>
#include "Test1.h"
#include "Test2.h"

using namespace std;
int main(int argc, const char * argv[]) {
    
    cout << Test1::ave(1,2) << endl;
    cout << Test2::ave(1,2) << endl;
    return 0;
}

6.使用xcode是编译不过的,链接不了,最后发现visual studio居然可以,真是神奇了。

File1.cpp

#include<iostream>
using namespace std;

extern int x;

namespace
{
    int y = -4;
}


void another()
{
    cout << "another() " << x << ", " << y << endl;
}

File2.cpp

#include <iostream>
using namespace std;

void other();
void another();
int x = 10;
int y;
int main(int argc, const char * argv[]) {
    
    cout << x << endl;
    {
        int x = 4;
        cout << x << endl;
        cout << y << endl;
    }
    other();
    another();
    return 0;
}
void other()
{
    int y = 1;
    cout << "Ohter: " << 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(int argc, const char * argv[]) 
{
	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









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值