zoukankan      html  css  js  c++  java
  • cc38b_demo_C++_异常_(2)txwtech在异常中使用虚函数-多态

    //cc38b_demo,21days_C++_异常_(2)txwtech20200121在异常中使用虚函数-多态

    //--异常层次结构
    //*异常的类-创建自己的异常类
    //*异常派生-就是继承
    //*异常中的数据:数据成员

    //*按引用传递异常


    //
    //*->在异常中使用虚函数/可以使用多态,简化catch

      1 //cc38b_demo,21days_Cpp_异常_(2)txwtech20200121在异常中使用虚函数-多态
      2 
      3 //--异常层次结构
      4 //*异常的类-创建自己的异常类
      5 //*异常派生-就是继承
      6 //*异常中的数据:数据成员
      7 
      8 //*按引用传递异常
      9 
     10 
     11 //
     12 //*->在异常中使用虚函数/可以使用多态,简化catch
     13 
     14 #include <iostream>
     15 using namespace std;
     16 const int DefaultSize = 10;
     17 class Array//数组类,类似动态数组vector
     18 {
     19 public:
     20     Array(int itsSize = DefaultSize);
     21     ~Array() { delete[] pType; }//删除[]数组指针
     22     //运算符重载
     23     //下标运算符重载
     24     int& operator[](int offSet);//非-常函数
     25     const int& operator[](int offSet) const;//常函数
     26     //访问器,accessors
     27     int GetitsSize() const { return itsSize; }
     28     //做异常类
     29     class xBoundary {};
     30     class xSize
     31     {
     32     public:
     33         xSize() {}
     34         xSize(int size) :itsSize(size) {}
     35         ~xSize() {};
     36         int GetSize() { return itsSize; }
     37 
     38         ////*->在异常中使用虚函数
     39 
     40         virtual void PrintError()
     41         {
     42             cout << "下标发生错误: " << itsSize << endl;
     43         }
     44     //private:
     45     protected:
     46         int itsSize;
     47 
     48     };
     49     //class xZero {};
     50     //class xNegative {};
     51     //class xTooSmall {};
     52     //class xTooBig {};
     53 
     54     //通过继承实现异常的层次结构,好处是什么?得到更详细的异常信息
     55 
     56     class xZero :public xSize
     57     {
     58     public:
     59         xZero(int size) :xSize(size) {}
     60         virtual void PrintError()
     61         {
     62             cout << "下标不能是0"<< endl;
     63         }
     64     };
     65     class xNegative :public xSize
     66     {
     67     public:
     68         xNegative(int size) :xSize(size) {}
     69         virtual void PrintError()
     70         {
     71             cout << "下标不能是负数" <<xSize::itsSize<< endl;
     72         }
     73     };
     74     class xTooSmall :public xSize
     75     {
     76     public:
     77         xTooSmall(int size) :xSize(size) {}
     78         virtual void PrintError()
     79         {
     80             cout << "下标不能小于10:当前是:" << xSize::itsSize << endl;
     81         }
     82     };
     83     class xTooBig :public xSize
     84     {
     85     public:
     86         xTooBig(int size) :xSize(size) {}
     87         virtual void PrintError()
     88         {
     89             cout << "下标不能>3000" << xSize::itsSize << endl;
     90         }//catch就可以简化了
     91     };
     92 private:
     93     int *pType;
     94     int itsSize;
     95 };
     96 int& Array::operator[](int offset)//非-常函数
     97 {
     98     int size = this->GetitsSize();
     99     if (offset >= 0 && offset < size)
    100         return pType[offset];
    101     throw xBoundary(); //xBoundary后面记得加括号()
    102 }
    103 const int& Array::operator[](int offset) const//常函数
    104 {
    105     int size = this->GetitsSize();
    106     if (offset >= 0 && offset < size)
    107         return pType[offset];
    108     //异常类,用着下标操作中
    109     throw xBoundary(); //xBoundary后面记得加括号()
    110 }
    111 
    112 Array::Array(int size) :itsSize(size) //
    113 {
    114     if (size == 0)
    115         throw xZero(size);
    116     else if (size < 0)
    117         throw xNegative(size);
    118     else if (size > 3000)
    119         throw xTooBig(size);
    120     else if (size < 10)
    121         throw xTooSmall(size);
    122     pType = new int[size];//动态创建数组,放在pType指针
    123     for (int i = 0; i < size; i++)
    124         pType[i] = 0;
    125 }
    126 
    127 
    128 
    129 int main()
    130 {
    131     
    132     try
    133     {
    134         Array a;
    135         Array intArray(20);
    136         //Array b(-12);
    137         //Array b(30000);
    138         Array b(6);//6<10发生异常,太小
    139         for (int j = 0; j < 100; j++)
    140         {
    141             intArray[j] = j;
    142             cout << "intArray[" << j << "]okay..." << endl;
    143         }
    144 
    145     }
    146     catch (Array::xBoundary)
    147     {
    148         cout << "下标越界了" << endl;
    149     }
    150     //简化有继承关系的,实现多态
    151     catch (Array::xSize& exp)
    152     {
    153         exp.PrintError();
    154     }
    155     //catch (Array::xZero)  //构造函数里面throw与catch里面的顺序需要一致
    156     //{
    157     //    cout << "下标不能为0" << endl;
    158     //}
    159     //
    160     //catch (Array::xNegative theException)//构造函数里面throw与catch里面的顺序需要一致
    161     //{
    162     //    cout << "下标不能是负数: " << theException.GetSize() << endl;
    163     //}
    164     //
    165     //catch (Array::xTooBig theException)//构造函数里面throw与catch里面的顺序需要一致
    166     //{
    167     //    cout << "下标不能>3000:" << theException.GetSize() << endl;
    168     //}
    169     //
    170     //catch (Array::xTooSmall theException)//构造函数里面throw与catch里面的顺序需要一致
    171     //{
    172     //    cout << "下标不能<10,当前是:" << theException.GetSize() << endl;
    173     //}
    174     catch (...)//catch需要有顺序,...放到最后
    175     {
    176         cout << "发生未知异常" << endl;
    177     }
    178     cout << "Done." << endl;
    179     getchar();
    180     //system("pause");
    181     return 0;
    182 }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    牛客小白月赛12 392B
    牛客392A 经典区间覆盖
    hihocoder contest95 1、3、4题目分析 2赛后补题
    hiho一下第234周《矩形计数》题目与解答
    Light oj 1306
    请访问我新的博客
    比特币“投资”记录-1
    如何清爽的使用网页版新浪微博
    Vuex/Vue 练手项目 在线汇率转换器
    重装macOS环境配置笔记
  • 原文地址:https://www.cnblogs.com/txwtech/p/12228312.html
Copyright © 2011-2022 走看看