//cc38b_demo,21days_Cpp_异常_(2)txwtech20200121在异常中使用虚函数-多态
//--异常层次结构
//*异常的类-创建自己的异常类
//*异常派生-就是继承
//*异常中的数据:数据成员
//*按引用传递异常
//
//*->在异常中使用虚函数/可以使用多态,简化catch
//cc38b_demo,21days_Cpp_异常_(2)txwtech20200121在异常中使用虚函数-多态
//--异常层次结构
//*异常的类-创建自己的异常类
//*异常派生-就是继承
//*异常中的数据:数据成员
//*按引用传递异常
//
//*->在异常中使用虚函数/可以使用多态,简化catch
#include <iostream>
using namespace std;
const int DefaultSize = 10;
class Array//数组类,类似动态数组vector
{
public:
Array(int itsSize = DefaultSize);
~Array() { delete[] pType; }//删除[]数组指针
//运算符重载
//下标运算符重载
int& operator[](int offSet);//非-常函数
const int& operator[](int offSet) const;//常函数
//访问器,accessors
int GetitsSize() const { return itsSize; }
//做异常类
class xBoundary {};
class xSize
{
public:
xSize() {}
xSize(int size) :itsSize(size) {}
~xSize() {};
int GetSize() { return itsSize; }
////*->在异常中使用虚函数
virtual void PrintError()
{
cout << "下标发生错误: " << itsSize << endl;
}
//private:
protected:
int itsSize;
};
//class xZero {};
//class xNegative {};
//class xTooSmall {};
//class xTooBig {};
//通过继承实现异常的层次结构,好处是什么?得到更详细的异常信息
class xZero :public xSize
{
public:
xZero(int size) :xSize(size) {}
virtual void PrintError()
{
cout << "下标不能是0"<< endl;
}
};
class xNegative :public xSize
{
public:
xNegative(int size) :xSize(size) {}
virtual void PrintError()
{
cout << "下标不能是负数" <<xSize::itsSize<< endl;
}
};
class xTooSmall :public xSize
{
public:
xTooSmall(int size) :xSize(size) {}
virtual void PrintError()
{
cout << "下标不能小于10:当前是:" << xSize::itsSize << endl;
}
};
class xTooBig :public xSize
{
public:
xTooBig(int size) :xSize(size) {}
virtual void PrintError()
{
cout << "下标不能>3000" << xSize::itsSize << endl;
}//catch就可以简化了
};
private:
int *pType;
int itsSize;
};
int& Array::operator[](int offset)//非-常函数
{
int size = this->GetitsSize();
if (offset >= 0 && offset < size)
return pType[offset];
throw xBoundary(); //xBoundary后面记得加括号()
}
const int& Array::operator[](int offset) const//常函数
{
int size = this->GetitsSize();
if (offset >= 0 && offset < size)
return pType[offset];
//异常类,用着下标操作中
throw xBoundary(); //xBoundary后面记得加括号()
}
Array::Array(int size) :itsSize(size) //
{
if (size == 0)
throw xZero(size);
else if (size < 0)
throw xNegative(size);
else if (size > 3000)
throw xTooBig(size);
else if (size < 10)
throw xTooSmall(size);
pType = new int[size];//动态创建数组,放在pType指针
for (int i = 0; i < size; i++)
pType[i] = 0;
}
int main()
{
try
{
Array a;
Array intArray(20);
//Array b(-12);
//Array b(30000);
Array b(6);//6<10发生异常,太小
for (int j = 0; j < 100; j++)
{
intArray[j] = j;
cout << "intArray[" << j << "]okay..." << endl;
}
}
catch (Array::xBoundary)
{
cout << "下标越界了" << endl;
}
//简化有继承关系的,实现多态
catch (Array::xSize& exp)
{
exp.PrintError();
}
//catch (Array::xZero) //构造函数里面throw与catch里面的顺序需要一致
//{
// cout << "下标不能为0" << endl;
//}
//
//catch (Array::xNegative theException)//构造函数里面throw与catch里面的顺序需要一致
//{
// cout << "下标不能是负数: " << theException.GetSize() << endl;
//}
//
//catch (Array::xTooBig theException)//构造函数里面throw与catch里面的顺序需要一致
//{
// cout << "下标不能>3000:" << theException.GetSize() << endl;
//}
//
//catch (Array::xTooSmall theException)//构造函数里面throw与catch里面的顺序需要一致
//{
// cout << "下标不能<10,当前是:" << theException.GetSize() << endl;
//}
catch (...)//catch需要有顺序,...放到最后
{
cout << "发生未知异常" << endl;
}
cout << "Done." << endl;
getchar();
//system("pause");
return 0;
}