第十六周阅读程序:异常处理和命名空间

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

运行结果:


知识点总结:

try块是检查语句,throw用来当出现异常时抛出的一个异常信号,catch用来捕捉异常信息。

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

运行结果:



运行结果:

命名空间的作用是建立一些相互分隔的作用域,把一些全局实体分隔开来,以免产生命名冲突。

ob2.reset(100);因为100==upperbound,所以i==0,所以直接输出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,c,d处可以省去CounterNameSpace::,b处不可以省去CounterNameSpace::。

因为using CounterNameSpace::upperbound;声明了upperbound为命名空间成员,而lowerbound没有声明,所以不能省略。

在用using声明后,在其后程序中出现的upperbound,lowerbound就是隐含的指CounterNameSpace::upperbound,CounterNameSpace::lowerbound,所以c,d处可以省略。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值