《C++语言与面向对象的设计》习题及参考解答(5)

2020-06-05 09:12

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 } #include }; class Student void main() { { char *Name; try{ int Age; Student(\ int length; } public: catch(int size) Student(char *name, int age) {

{ 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 enum errs{error0,error1}; break; double Divide(double test1, double test2) case error1: { cout<<\除数太大!\

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 cin>>x[i]; double sqrtX(double x) try{ { s += sqrtX(x[i]); if(x>=0) } return sqrt(x); catch(int){ else {throw 1;} cout<<\负数不能开平方!输入} 数据无效。\void main() } { } double x[10]; cout<<\数组中有效数据的平方根之和 double s=0; 为: \

} 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 { class Student cout<

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>IDnumber[i]; { IDnumber[IDlen]='\\0'; Student s1(\ int s1.print(); size=(strlen(Name)+1+sizeof(Age)+strlen(IDnumber})+1+sizeof(IDlen));

(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 class Vector } { templateint isEqual(T a,T b){return T x, y; a==b;} public: void main() Vector(){} { Vector(T a, T b){x=a;y=b;} int a=215; void print(){cout<<\ cout< v); char b='b', c='c'; Vector operator-(); cout<Vector Vector v1(2.5,12.5),v2(2.5,2.5); Vector::operator-() Vector v3(5,12),v4(5,12); { v1.print(); Vector temp; v2.print(); temp.x=-x;temp.y=-y; cout<int cout<::operator==(Vector v) } {

13.2 分析下面的程序的运行结果,说明队列的读写过程:#include const int MaxSize=20; void Queue::input(Type& x) template class Queue { { try{ Type data[MaxSize]; if((tail+1)%MaxSize==head) throw 1; int head,tail; tail=(tail+1)%MaxSize; public: data[tail]=x; Queue(){head=0;tail=0;} } void clear(){head=0;tail=0;} catch(int) void input(Type& x); { Type getout(); cout<<\ int empty()const {return head==tail;} } void printQueue()const; } void printData()const; template Type Queue::getout() }; { template Type temp;

50


《C++语言与面向对象的设计》习题及参考解答(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:回复买家好评模板

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: