CHAPTER2-35.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:1k
- //文件名:CHAPTER2-35.cpp
- #include<iostream.h>
- 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 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(int x) { cout<<"A error array ID number has been found.This value is "<<x<<endl; }
- }