且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

《C++语言基础》程序阅读——异常处理和命名空间

更新时间:2022-01-21 19:27:08

返回:贺老师课程教学链接

阅读下面的程序,写出输出结果

(1)

#include <iostream >
using namespace std;
int a[10]= {1,2, 3, 4, 5, 6, 7, 8, 9, 10};
int fun( int i);
int main()
{
    int i ,s=0;
    for( i=0; i<=10; i++)
    {
        try
        {
            s=s+fun(i);
        }
        catch(int)
        {
            cout<<"数组下标越界!"<<endl;
        }
    }
    cout<<"s="<<s<<endl;
    return 0;
}
int fun( int i)
{
    if(i>=10)
        throw i;
    return a[i];
}

(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;
}

(3)将(2)中的main函数换作如下形式,其余不变

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;

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

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

    return 0;
}

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

补充阅读

#include <iostream>
using namespace std;
void f();
class T
{
public:
    T( )
    {
        cout<<"constructor"<<endl;
        try
        {
            throw "exception";
        }
        catch(char*)
        {
            cout<<"exception"<<endl;
        }
        throw "exception";
    }
    ~T( )
    {
        cout<<"destructor";
    }
};
int main()
{
    cout<<"main function"<< endl;
    try
    {
        f( );
    }
    catch(char *)
    {
        cout<<"exception2"<<endl;
    }
    cout<<"main function"<<endl;
    return 0;
}
void f( )
{
    T t;
}