第十六周 程序阅读(3)

问题及代码:

代码(2)

#include <iostream>
using namespace  std;
namespace CounterNameSpace
{
int upperbound;
int lowerbound;

class counter
{
    int count;
public:
    counter(int n)
    {
        if (n <= upperbound )
        {
            count = n;
        }
        else
        {
            count = upperbound;
        }
    }

    void reset(int n)
    {
        if (n < upperbound)
        {
            count = n;
        }
    }

    int run()
    {
        if (count > lowerbound)
        {
            return count--;
        }
        else
            return lowerbound;
    }
};
}

int main()
{
    CounterNameSpace::upperbound = 100;
    CounterNameSpace::lowerbound = 0;
    CounterNameSpace::counter ob1(10);
int i;

    do
    {
        i = ob1.run();
        cout << i << " ";
    }
    while (i > CounterNameSpace::lowerbound);
    cout << endl;

    CounterNameSpace::counter ob2(20);
    do
    {
        i = ob2.run();
        cout << i << " ";
    }
    while (i > CounterNameSpace::lowerbound);
    cout << endl;

    ob2.reset(100);
    do
    {
        i = ob2.run();
        cout << i << " ";
    }
    while (i > CounterNameSpace::lowerbound);
    cout << endl;

    return 0;
}

/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:吴胜男
*完成日期:2015年06月21日
*版本号:v1.0
*
*问题描述:阅读下面的程序,写出输出结果(3)
请回答:
(a)(d)处:为什么可以省去CounterNameSpace::?
(b)(c)处:是否可以省去CounterNameSpace::?
*输入描述:
*程序输出:
*/
#include <iostream>
using namespace  std;
namespace CounterNameSpace
{
int upperbound;
int lowerbound;

class counter
{
    int count;
public:
    counter(int n)
    {
        if (n <= upperbound )
        {
            count = n;
        }
        else
        {
            count = upperbound;
        }
    }

    void reset(int n)
    {
        if (n < upperbound)
        {
            count = n;
        }
    }

    int run()
    {
        if (count > lowerbound)
        {
            return count--;
        }
        else
            return lowerbound;
    }
};
}

int main()
{
   <span style="color:#000099;"> </span><span style="color:#cc33cc;">using CounterNameSpace::upperbound;
    upperbound = 100;   //(a)</span>
    CounterNameSpace::lowerbound = 0;  //(b)
    CounterNameSpace::counter ob1(10);
    int i;
    do
    {
        i = ob1.run();
        cout << i<<" ";
    }
    while( i > CounterNameSpace::lowerbound);
    cout << endl;

    <span style="color:#cc33cc;">using namespace CounterNameSpace;
    counter ob2(20);</span>
    do
    {
        i = ob2.run();
        cout << i<<" ";
    }
    <span style="color:#ff0000;">while( i > CounterNameSpace::lowerbound);</span> //(c)
    cout << endl;

    ob2.reset(100);
    <span style="color:#cc33cc;">lowerbound = 90;</span>   //(d)
    do
    {
        i = ob2.run();
        cout << i <<" ";
    }
    while( i > lowerbound);

    return 0;
}
/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:吴胜男
*完成日期:2015年06月21日
*版本号:v1.0
*
*问题描述:阅读下面的程序,写出输出结果(3)
请回答:
(a)(d)处:为什么可以省去CounterNameSpace::?
(b)(c)处:是否可以省去CounterNameSpace::?
*输入描述:
*程序输出:
*/
#include <iostream>
using namespace  std;
namespace CounterNameSpace
{
int upperbound;
int lowerbound;

class counter
{
    int count;
public:
    counter(int n)
    {
        if (n <= upperbound )
        {
            count = n;
        }
        else
        {
            count = upperbound;
        }
    }

    void reset(int n)
    {
        if (n < upperbound)
        {
            count = n;
        }
    }

    int run()
    {
        if (count > lowerbound)
        {
            return count--;
        }
        else
            return lowerbound;
    }
};
}

int main()
{
    using CounterNameSpace::upperbound;
    upperbound = 100;   //(a)
    CounterNameSpace::lowerbound = 0;  //(b)
    CounterNameSpace::counter ob1(10);
    int i;
    do
    {
        i = ob1.run();
        cout << i<<" ";
    }
    while( i > CounterNameSpace::lowerbound);
    cout << endl;

    <span style="color:#cc33cc;">using namespace CounterNameSpace;</span>
    counter ob2(20);
    do
    {
        i = ob2.run();
        cout << i<<" ";
    }
   <span style="color:#ff0000;"> while( i > lowerbound);</span> //(c)
    cout << endl;

    ob2.reset(100);
    lowerbound = 90;   //(d)
    do
    {
        i = ob2.run();
        cout << i <<" ";
    }
    while( i > lowerbound);

    return 0;
}


运行结果:

知识点总结:

请回答: 
(a)(d)处:为什么可以省去CounterNameSpace::? 

(a)处:使用了命名空间成员的方法using CounterNameSpace::upperbound;在其后的upperbound就等价于CounterNameSpace::upperbound。

(d)处:是因为使用了using namespace CounterNameSpace;声明了在本作用域中要用到命名空间CounterNameSpace中的成员,在使用该命名空间的任何成员时都不必再用命名空间的限定。
(b)(c)处:是否可以省去CounterNameSpace::?

(b)处不可以。(c)处可以,理由同上。

学习心得:一点一点,扎扎实实的感觉真好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值