ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:18.23KB ,
资源ID:29204558      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/29204558.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(习题6及其解答第二版.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

习题6及其解答第二版.docx

1、习题6及其解答第二版第6章 运算符重载习题66.1 选择题1.在下列运算符中,不能重载的是( b )。(a) ! (b) sizeof (c) new (d) delete2.下列关于运算符重载的描述中,( d )是正确的。(a) 可以改变参与运算的操作数个数 (b) 可以改变运算符原来的优先级(c) 可以改变运算符原来的结合性 (d) 不能改变原运算符的语义3.下列函数中,不能重载运算符的函数是( b )。(a) 成员函数 (b) 构造函数 (c) 普通函数 (d) 友员函数4.不能用友员函数重载的是( a )。(a) = (b) = (c) = (d) +5. 下面关于类型转换的描述中,错

2、误的是( b )。(a) 构造函数可以把一种类类型对象转换成另一种类类型对象(b) 构造函数可以把一种类类型对象转换成基本类型对象(c) 类型转换函数可以把类类型对象转换为其他指定类型对象(d) 类型转换函数只能定义为一个类的成员函数,不能定义为类的友员函数6.2 阅读下列程序,写出执行结果 1.#include class T public : T() a = 0; b = 0; c = 0; T( int i , int j , int k ) a = i; b =j ; c = k; void get( int &i , int &j , int &k ) i = a; j = b; k

3、 = c; T operator * ( T obj );private: int a , b , c;T T:operator * ( T obj ) T tempobj; tempobj.a = a * obj.a; tempobj.b = b * obj.b; tempobj.c = c * obj.c; return tempobj;void main() T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3; int a , b , c; obj3 = obj1 * obj2; obj3.get( a, b, c ); cout ( obj1 * obj2 ):

4、t a = a t b = b t c = c t endl; ( obj2 * obj3 ).get( a, b, c ); cout ( obj2 * obj3 ): t a = a t b = b t c = c t endl;【答案】( obj1 * obj2 ): a = 5 b = 10 c = 15 ( obj2 * obj3 ): a = 25 b = 50 c = 752.#include class Vector public: Vector() Vector(int i,int j) x = i ; y = j ; friend Vector operator + ( V

5、ector v1, Vector v2 ) Vector tempVector ; tempVector.x = v1.x + v2.x ; tempVector.y = v1.y + v2.y ; return tempVector ; void display() cout ( x , y ) endl ; private: int x , y ;void main() Vector v1( 1, 2 ), v2( 3, 4 ), v3 ; cout v1 = ; v1.display() ; cout v2 = ; v2.display() ; v3 = v1 + v2 ; cout v

6、3 = v1 + v2 = ; v3.display() ; 【答案】v1 = ( 1, 2 ) v2 = ( 3, 4 ) v3 = v1 + v2 = ( 4, 6 )6.3 思考题 1 一个运算符重载函数被定义为成员函数或友员函数,从定义方式、解释方式和调用方式上有何区别?可能会出现什么问题?请用一个实例说明之。 2 类类型对象之间,类类型和基本类型对象之间用什么函数进行类型转换?归纳进行类型转换的构造函数和类型转换函数的定义形式、调用形式和调用时机。6.4 编程题1分别使用成员函数和友员函数编程序重载运算符“+”,使该运算符能实现两个字符串的连接。【解答】(1)使用成员函数#inclu

7、de class string public: string() *str = 0; string( char *pstr ) strcpy( str,pstr ); char *gets() return str; string operator+( string obj ); private: char str100;string string:operator+( string obj ) strcat( str,obj.str ); return str; /或return *this void main() string obj1( Visual ),obj2( C+ ),obj3;

8、 obj3 = obj1 + obj2; cout obj3.gets() endl;(2)使用友员函数#include #include class string public: string() *str= 0; string( char *pstr ) strcpy( str,pstr ); char *gets() return str; friend string operator+( string obj1,string obj2 ); private: char str100; string operator+( string obj1,string obj2 ) string

9、tempobj; strcat( tempobj.str,obj1.str ); strcat( tempobj.str,obj2.str ); return tempobj;void main() string obj1( Visual ),obj2( C+ ),obj3; obj3 = obj1 + obj2; cout obj3.gets() endl;2定义一个整数计算类Integer,实现短整数 +,-,*,/ 基本算术运算。要求可以进行数据范围检查(-3276832767,或自行设定),数据溢出时显示错误信息并中断程序运行。【解答】#include#includeclass Int

10、eger private: short a; public: Integer (short n=0) a=n; Integer operator +(Integer); Integer operator -(Integer); Integer operator *(Integer); Integer operator /(Integer); Integer operator =(Integer); void display() coutaendl;Integer Integer:operator+(Integer x) Integer temp; if(a+x.a32767) coutData

11、 overflow!endl; abort(); temp.a=a+x.a; return temp;Integer Integer:operator-(Integer x)Integer temp;if(a-x.a32767) coutData overflow!endl; abort();temp.a=a-x.a;return temp;Integer Integer:operator*(Integer x)Integer temp;if(a*x.a32767) coutData overflow!endl; abort();temp.a=a*x.a;return temp;Integer

12、 Integer:operator/(Integer x)Integer temp; if(a/x.a32767) coutData overflow!endl; abort(); temp.a=a/x.a; return temp; Integer Integer:operator=(Integer x) a=x.a; return *this; void main() Integer A(90),B(30),C; coutA=;A.display(); coutB=;B.display(); C=A+B; coutC=A+B=; C.display(); C=A-B; coutC=A-B=

13、; C.display(); C=A*B; coutC=A*B=; C.display(); C=A/B; coutC=A/B=; C.display();3定义一个实数计算类Real,实现单精度浮点数 +,-,*,/ 基本算术运算。要求可以进行数据范围(-3.410383.41038,或自行设定)检查,数据溢出时显示错误信息并中断程序运行。【解答】#include#includeclass Real private: double a; public: Real (double r=0)a=r; Real operator +(Real); Real operator -(Real); Re

14、al operator *(Real); Real operator /(Real); Real operator =(Real); void display() coutaendl;Real Real:operator+(Real x) Real temp; if(a+x.a1.7e308) coutData overflow!endl; abort(); temp.a=a+x.a; return temp;Real Real:operator-(Real x)Real temp;if(a-x.a1.7e308) coutData overflow!endl; abort();temp.a=

15、a-x.a;return temp;Real Real:operator*(Real x)Real temp;if(a*x.a1.7e308) coutData overflow!endl; abort();temp.a=a*x.a;return temp;Real Real:operator/(Real x)Real temp; if(a/x.a1.7e308) coutData overflow!endl; abort(); temp.a=a/x.a; return temp; Real Real:operator=(Real x) a=x.a; return *this; void ma

16、in() Real A(1.1),B(1.2),C; coutA=;A.display(); coutB=;B.display(); C=A+B; coutC=A+B=; C.display(); C=A-B; coutC=A-B=; C.display(); C=A*B; coutC=A*B=; C.display(); C=A/B; cout”、“”做向量的输入/输出操作。注意检测运算的合法性。【解答】#include#includeclass Vectorprivate: double *v; int len;public: Vector(int size); Vector(double

17、 *,int); Vector(); double &operator(int i); Vector & operator =(Vector &); friend Vector operator +(Vector &,Vector &); friend Vector operator -(Vector &,Vector &); friend double operator *(Vector &,Vector &); friend ostream & operator (istream &input,Vector &);Vector:Vector (int size) if(size=21474

18、83647)coutThe size of sizeis overflow!n;abort();v=new double size;for(int i=0;isize;i+) vi=0;len=size;Vector:Vector(double *C,int size) if(size=2147483647)coutThe size ofsizeis overflow!nendl; abort();v=new doublesize;len=size;for(int i=0;i=0 & ilen) return vi; else coutThe size ofiis overflow!n; ab

19、ort();Vector &Vector:operator =(Vector &C) if(len=C.len) for(int i=0;ilen;i+) vi=Ci; return *this; else coutOperator = fail!n; abort();Vector operator +(Vector &A,Vector &B) / 向量相加 int size=A.len ; double *T=new doublesize; if(size=B.len) for(int i=0;isize;i+) Ti=Ai+Bi; return Vector (T,size); else

20、coutOperator + fail!n; abort(); Vector operator -(Vector &A,Vector &B) /向量相减 int size=A.len ; double *T=new doublesize; if(size=B.len) for(int i=0;isize;i+) Ti=Ai-Bi; return Vector (T,size); else coutOperator - fail!n; abort(); double operator *(Vector &A,Vector &B) /向量相乘 int size=A.len ; double s=0

21、; if(size=B.len) for(int i=0;isize;i+) s+=Ai*Bi; return s; else coutOperator * fail!n; abort(); ostream & operator (ostream &output,Vector &A) /输出 output(; for(int i=0;iA.len-1;i+) outputAi,; outputAi(istream &input,Vector &A) /输入 for(int i=0;iAi;return input;void main() int k1,k2,k3;double t;coutk1

22、;Vector A(k1);coutA;coutk2;Vector B(k2);coutB;coutk3;Vector C(k3);coutA=Aendl;coutB=Bendl;C=A+B;coutA+B=A+B=Cendl;C=A-B;coutA-B=A-B=Cendl;t=A*B;coutA*B=A*B=tendl;5定义一个类nauticalmile_kilometer,它包含两个数据成员kilometer(千米)和meter(米)。还包含一个构造函数对数据成员初始化;成员函数print用于输出数据成员kilometer和meter的值;类型转换函数double()实现把千米和米转换为

23、海里(1海里=1.852千米)。编写main函数,测试类nauticalmile_kilometer。【解答】#include const double n = 1.852; / 定义海里与千米和米的转换系数(1海里=1.852千米)class nauticalmile_kilometer public: nauticalmile_kilometer( int km,double m ) kilometer = km; meter = m; void print() coutkilometer=kilometertmeter=meterendl; operator double();private: int kilometer;double meter;nauticalmile_kilometer:operator double() return ( meter / 1000 + double( kilometer ) / n; void main() nauticalmile_kilometer obj( 100,50 ); obj.print(); cout nauticalmile= double( obj ) endl;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1