Abnormalities in the class hierarchy

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/dxd_123456/article/details/78125130

Using exception in the class hierarchy that is inherited in an abnormal use of this content can be illustrated by a case out of
the requirements of this case is this:
design an array class MyArray, overloaded [] operation,
initialize the array, the array the number of effective examination
1) index <0 thrown eNegative
2) index = 0 thrown Ezero
. 3) index> 1000 thrown eTooBig
. 4) index <10 thrown eTooSmall
. 5) or more class-based Esize parent, has parameters configured to achieve, and define the virtual void printErr () output error.

#include <stdio.h>

class MyArray
{
public:
    MyArray(int len);
    ~MyArray();
    int& operator[](int index);

    class eSize
    {
    public:
        eSize(int len)
        {
            size = len;
        }
        virtual void printErr() = 0;
    protected:
        int size;
    };

    class eNegative :public eSize
    {
    public:
        eNegative(int size) :eSize(size)
        {

        }
        virtual void printErr()
        {
            printf("抛出异常参数等于0\n");
        }
    };
    class  eZero :public eSize
    {
    public:
        eZero(int size) :eSize(size)
        {

        }
        virtual void printErr()
        {
            printf("抛出异常参数小于0\n");
        }
    };
    class eTooBig :public eSize
    {
    public:
        eTooBig(int size) :eSize(size)
        {

        }
        virtual void printErr()
        {
            printf("抛出异常参数大于1000\n");
        }
    };
    class eTooSmall :public eSize
    {
    public:
        eTooSmall(int size) :eSize(size)
        {
            this->size = size;
        }
        virtual void printErr()
        {
            printf("抛出异常参数小于10\n");
        }
    };

private:
    int len;
    int *m_p;
};

MyArray::MyArray(int len)
{
    if (len < 0)
        throw eNegative(len);
    if (len == 0)
        throw eZero(len);
    if (len >1000)
        throw eTooBig(len);
    if (len <10)
        throw eTooSmall(len);


    this->len = len;
    this->m_p = new int[this->len];
}

MyArray::~MyArray()
{
    if (m_p != NULL)
    {
        delete[]m_p;
        m_p = NULL;
    }
    len = 0;
}

int& MyArray::operator[](int index)
{
    return m_p[index];
}




int main()
{
    try
    {
        MyArray a(5);
        for (int i = 0; i < 10; i++)
            a[i] = i;

        printf("%d\n", a[2]);

    }
    catch (MyArray::eSize& e)
    {
        e.printErr();
    }
    catch (...)
    {
        printf("其他类型异常\n");
    }


    return 0;
}

In this case, we have created an array class that can perform various functions of the array, here is not to say that this is the contents of the previous courses, and in an array of initialization parameters must be checked use the content of this lesson, we need to exception processing parameters to obtain the results.
Here we create an exception in the array class, a part of the array of exception classes, this class is derived out of several sub-categories, sub-categories generated object is thrown depending on the circumstances, and we pick up abnormal results time, just use the parent class reference like to pick up, because printErr is virtual, e is the base class reference, polymorphism may occur, then the corresponding feedback abnormality, such a good indication of the case abnormalities in the class hierarchy.

Guess you like

Origin blog.csdn.net/dxd_123456/article/details/78125130