第十六周 程序阅读(3)

来源:互联网 发布:魔兽网络代理 编辑:程序博客网 时间:2024/06/05 10:47

问题及代码:

代码(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