Skip to content

Latest commit

 

History

History
185 lines (170 loc) · 4.05 KB

复习题.md

File metadata and controls

185 lines (170 loc) · 4.05 KB

第 9 章 内存模型和名称空间 复习题

  1. 对于下面的情况,应使用哪种存储方案?
    a. homer是函数的形参
    b. secret变量由两个文件共享
    c. topsecret变量由一个文件中的所有函数共享,但对于其他文件来说是隐藏的
    d. beencalled记录包含它的函数被调用的次数
a. 函数形参直接使用自动变量,因为编译器会自动设置定义为自动变量。
b. 使用static变量定义为外部变量,在另一个文化中通过extern来声明。
c. 通过使用内部静态链接的方式存储。
d. 在函数的声明之前使用static,定义为静态变量。
  1. using声明和using编译指令之间有何区别?

    • using声明:仅名称空间中的特定的名称可用,通过作用域解析运算符进行。
    • using编译指令:使整个名称空间中的名称可用。
  2. 重新编写下面代码,使其不使用using声明和using编译指令

原代码:

#include <iostream>
using namespace std;
int main(void)
{
    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(void)
{
    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;
}
  1. 重新编写下面的代码,使之使用using声明,而不是using 编译指令。

原代码

#include <iostream>
using namespace std;
int main(void)
{
    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(void)
{
    //直接将所需要的对象使用using进行声明
    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;
}
  1. 在文件中调用average(3,6)函数时,它返回两个int参数的平均值,在同一个程序的另一个文件中调用时,它返回两个int参数的double平均值,应如何实现?
//return_int.cpp

//return_double.cpp
  1. 下面的程序由两个文件组成,该程序显示什么内容?
//file1.cpp
#include <iostream>
using namespace std;
void other();
void another();
int x = 10;
int y;
 
int main(void)
{
    cout<< x <<endl; //10
    {
        int x = 4; // 局部变量
        cout<< x <<endl;//4
        cout<< y <<endl; // 0
    }
    other(); //x=10,y =1
    another(); // x = 10,y = -4
    return 0;
}
void other()
{
    int y = 1;
    cout<< "other : " << x <<" , " << y << endl;//x=10,y =1
}

// file2.cpp
#include <iostream>
using namespace std;
extern int x; //使用外部引入的方式,引入file1.cpp中的变量x=10
namespace {
    int y = -4;
}
void another()
{
    cout << " another() : " << x << " , " << y << endl; // x = 10,y = -4
}
  1. 下面的代码将显示什么内容?
#include <iostream>
using namespace std;
void other();
namespace n1{
    int x =1;
}
namespace n2{
    int x = 2;
}
int main(void)
{
    using namespace n1;
    cout << x <<endl; //1
    {
        int x = 4;
        cout << x << " , " << n1::x << ","<< n2::x << endl; //4 1 2
    }
    using n2::x;
    cout << x << endl; //2
    other();
    return 0;
}
void other()
{
    using namespace n2;
    cout << x << endl;//2
    {
        int x = 4;
        cout << x << ", " << n1::x << ", " << n2::x << endl; // 4 1 2
    }
    using n2::x;
    cout << x << endl; //2
}