C. 将捕获到unexpected异常。 D. 将由abort()终止程序。 (6)下面几种关于单路捕获的说法不正确的是( ) A. 单路捕获只能捕获到一个throw抛出的异常。
B. 单路捕获只能捕获到同一类型的多个throw抛出的异常当中的一个。 C. 单路捕获能够捕获到强制类型转换的异常。
D. 单路捕获能够捕获到同一个类的不同公有派生类的异常。 答:(1)C (2) A (3) B (4) B (5) A (6) A 12. 3 分析下面程序的输出结果:(1)请给出当分别以参数\和\创建对象时下面程序的输出:
#include
{ if (size==0) length=strlen(name); cout<<\ Name=new char[length+1]; else strcpy(Name,name); cout<<\ Age= age; } if(length<=0 || length>10) throw 0; } if(age>30) throw 1;
当对象参数为\时程序输出: Age is too old
当对象参数为\时程序输出: Name is too long
(2)分析下列程序的输出:#include
try{ break; if(test2==0) throw error0; } if(test2>=1000) throw error1; } } return test1/test2; catch(errs er){ } switch(er) void main() { { case error0: cout<
cout<
46
}
程序输出:除数不能为0! 1.#INF 除数太大! 0. 001
12. 4 编写程序:(1)从键盘上输入10个数x[i],求其平方根的和,并对x[i]<0进行异常捕获。
{ 参考程序:#include
#include
} cout<<\请输入10个double型数据: \
for(int i=0;i<10;i++) (2)Student类定义如下:class Student IDlen); { void print() char *Name; { int Age; cout< 要求:给出具有异常处理的构造函数实现代码,当Student对象的最大存储长度超过24个字节时进行异常处理。在测试程序中定义一个Student对象,捕获对象初始化时存储长度超过24个字节的异常。 void print() 参考程序:#include #include 47 char[strlen(name)+1]; if(size>24) throw 1; strcpy(Name,name); } Age = age; catch(int) { IDnumber = new char[IDlen+1]; cout<<\学生对象总长度超过24 字节\ cout<<\\ } chars:\} for(int i=0; i (3)矩阵A、B如下: A B 10 20 30 5 20 60 50 40 0 10 10 50 0 60 50 50 0 10 70 10 0 70 10 0 试根据公式C[i,j]=A[i,j]/B[i,j]编写一个程序,计算C并对异常进行捕获。 } 参考程序:#include void main() catch(int) { { double cout<<\分母不能为0,请检查矩阵B中A[4][3]={10,20,30,50,40,0,0,60,50,70,10,0}; 是否有0元素。\ double } B[4][3]={5,20,60,10,10,50,50,0,10,70,10,0}; for(int i=0;i<4;i++) double C[4][3]; { try{ for(int j=0;j<3;j++) for(int i=0;i<4;i++) cout< Matrix(int r,int c){ 参考程序:#include class Matrix row=r; { col=c; double *ptr; ptr=new double[row*col]; int row,col; } public: double operator()(int i, int j){ 48 return ptr[i*col+j]; } void set(int i, int j, double value){ ptr[i*col+j]=value; } void print(){ for(int i=0;i temp.set(i,j,temp(i,j)+(*this)(i,k)*m(k,j)); } return temp; } void main() { Matrix a(2,3),b(2,3),c(3,2), d(2,3), cout<<(*this)(i,j)<<\ \ cout< Matrix operator+(Matrix m); Matrix operator-(Matrix m); Matrix operator*(Matrix m); }; Matrix Matrix::operator+(Matrix m){ Matrix temp(row,col); if(row!=m.row||col!=m.col) throw \此二矩阵不能相加!\ for(int i=0;i Matrix Matrix::operator-(Matrix m){ Matrix temp(row,col); if(row!=m.row||col!=m.col) throw \此二矩阵不能相减!\ for(int i=0;i Matrix Matrix::operator*(Matrix m){ Matrix temp(row,m.col); if(row!=m.col) throw \前矩阵行数不等于后矩阵列数,不能相乘!\ for(int i=0;i e(2,3),f(2,2); a.set(0,0,3.0); a.set(0,1,3.0); a.set(0,2,3.0); a.set(1,0,9.0); a.set(1,1,-7.0); a.set(1,2,4.0); b.set(0,0,11.0); b.set(0,1,-2.0); b.set(0,2,-3.0); b.set(1,0,1.0); b.set(1,1,5.0); b.set(1,2,1.0); c.set(0,0,1.0); c.set(0,1,2.0); c.set(1,0,3.0); c.set(1,1,4.0); c.set(2,0,5.0); c.set(2,1,6.0); a.print(); b.print(); c.print(); try{ d=a+b; d.print(); e=a-b; e.print(); f=a*b; f.print(); } catch(char* info){ cout< 49 } 习题13参考答案 13.1 设计一个全局函数模板int isEqual(T a, T b)和一个模板向量类Vector,然后编写测试程序,对int型、char型变量和Vector类的对象进行相等与否的比较。 return (x==v.x&&y==v.y); 参考程序:#include template 13.2 分析下面的程序的运行结果,说明队列的读写过程:#include 50