第16周-异常处理和命名空间-课后实践·阅读程序

 

/* 
* Copyright (c) 2014, 烟台大学计算机学院 
* All rights reserved. 
* 文件名称:test.cpp 
* 作    者:刘畅
* 完成日期:2015 年 6  月  19 日 
* 版 本 号:v1.0 
* 
* 问题描述:阅读下面的程序,写出输出结果

* 输入描述:NULL;
* 程序输出:输出结果;

 

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


运行结果:

当我把for循环中的10改为了11,它的输出结果是这样的

 

 

(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)

#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;

    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::? 

因为之前已经对相关命名作了声明。using namespace CounterNameSpace;
(b)(c)处:是否可以省去CounterNameSpace::?

可以。

知识点总结:(1)异常处理,(2)(3)命名空间。

学习心得:

对于命名空间还有不少疑虑,得仔细看看书理解了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值