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