C++ Primer Plus第九章复习题

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

 答:

#include ciostream>
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
#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
#include <iostream>
using namespace std;
extern int x;
namespace
{
    int y = -4;
}
void another()
{
    cout << "another(): " << x << "," << y << endl;
}

答:

10
40
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

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值