CHAPTER2-36.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:1k
- //文件名:CHAPTER2-36.cpp
- #include<iostream.h>
- class Err
- {
- int err_number;
- public:
- Err(int x):err_number(x) {cout<<"A error array ID number has been found."<<endl;}
- int get_err_number(){return err_number;}
- };
- template<class T>
- class Array
- {
- int i;
- T *ar;
- public:
- Array(int c):i(c){ar=new T[c];}
- void init(int n,T x){ar[n]=x;}
- T& operator[](int n) {if(n>=i)throw Err(n);return ar[n];}
- };
- void main()
- {
- Array<int> array(5);
- cout<<"Please input every element's value:"<<endl;
- try{
- for(int i=0;i<5;i++) { cout<<"No."<<i+1<<':';cin>>array[i];}
- cout<<array[5]; //测试数组越界从而导致异常处理的产生
- }
- catch(Err& ex) {cout<<"This value is "<<ex.get_err_number()<<endl;}
- }