当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++ Primer Plus复习题及答案

C++ Primer Plus复习题及答案

2018年10月05日  | 移动技术网IT编程  | 我要评论
1. 对于下面的情况,应使用哪种存储方案?  a. homer是函数的形参。  b. secret变量由两个文件共享。  c. topsecret变量由一个文件中的所有

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

a. homer是函数的形参。 

b. secret变量由两个文件共享。 

c. topsecret变量由一个文件中的所有函数共享,但对于其他文件来说是隐藏的。 

d. beencalled记录包含它的函数被调用的次数。 

答: 

a.homer将自动成为自动变量。 

b.应该在一个文件中将secret定义为外部变量,并在第二个文件中使用extern来声明它。 

c.可以在外部定义前加上关键字static,将topsecret定义为一个有内部链接的静态变量。也可在一个未命名的名称空间中进行定义 

d.应在函数中的声明前加上关键字static,将beencalled定义为一个本地静态变量。

2. 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 mian() {

    double x;

    std::cout << "enter value: ";

    while (!(std::cin >> x)) {

        std::cout << "bad input. please enter a numeber: ";

        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 calus: ";

    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() {

    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.在一个文件中调用average(3,6)函数时,它返回两个int参数的int平均值,在同一个程序的另一个文件中调用时,它返回两个int参数的double平均值。应如何实现? 

答:可以在每个文件中包含单独的静态函数定义。或者每个文件都在未命名的名称空间中定义一个合适的average()函数。

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 

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;

}

答: 

4, 1, 2 

4, 1, 2 

2

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网