- 异常的多态使用
- 提供基类异常类
- class BaseException
- 纯虚函数 virtual void printError() = 0;
- 子类空指针异常 和 越界异常 继承 BaseException
- 重写virtual void printError()
- 测试 利用父类引用指向子类对象
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//异常的基类
class BaseException
{
public:
virtual void printError() = 0;
};
//空指针异常
class NULLPointerException :public BaseException
{
public:
virtual void printError()
{
cout << "空指针异常" << endl;
}
};
//越界异常
class OutOfRangeException :public BaseException
{
public:
virtual void printError()
{
cout << "越界异常" << endl;
}
};
void doWork()
{
//throw NULLPointerException();
throw OutOfRangeException();
}
void test01()
{
try
{
doWork();
}
catch (BaseException & e)
{
e.printError();
}
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}