运算符重载练习题

上传人:m**** 文档编号:512673673 上传时间:2023-10-28 格式:DOCX 页数:22 大小:44.35KB
返回 下载 相关 举报
运算符重载练习题_第1页
第1页 / 共22页
运算符重载练习题_第2页
第2页 / 共22页
运算符重载练习题_第3页
第3页 / 共22页
运算符重载练习题_第4页
第4页 / 共22页
运算符重载练习题_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《运算符重载练习题》由会员分享,可在线阅读,更多相关《运算符重载练习题(22页珍藏版)》请在金锄头文库上搜索。

1、运算符重载.单项选择题1 .下列运算符中,运算符在C+4不能重载。 A . ?: B . + C . D . obj2 被C+编译器解释为 。A . operator(objl , obj2) B . (obj1 , obj2)C . obj2 . operator : (obj1) D . objl . operator(obj2)解:重载为友元函数的运算符的调用形式如下:operator(,参数2)等价于:参数1本题答案为A是正确的。5 .现需要对list类对象使用的逻辑运算符“二=”重载,以下函数声明A 、list & list : operator=(const list &a);B

2、、list list : operator=(const list &a);C 、bool & list : operator=(const list &a)D 、bool list : operator=(const list &a)6 .以下类中分别说明了 “+=”和“ +”运算符重载函数的原型。如果主函数中有定义:funm,c,d; ,那么,执行语句c=m+;时,编译器把 m+解释为:(33)A) c.operator+(m); B) m=operator+(m);C) m.operator+(m); D) operator+(m); class funpublic:fun operat

3、or +=(fun);friend fun operator +(fun &,int);;答案:D7 .在第33题中,当执行语句 d+=m;时,C+编译器对语句作如下解释:(34)A. d=operator+=(m);B. m=operator+=(d);C. d.operator+=(m);D. m.operator+=(d);答案:C8 .设有以下类定义,其中说明了 “+”运算符重载函数的原型。这是一个友元函数,当类外有语句a=b+c;访问这个友元函数时,C+编译器把对语句 a=b+c;解释为:operator +(b,c)其中:(35)A. a,b,c都必须是 Com的对象 B. a,b

4、都必须是 Com的对象C. a,c都必须是Com的对象 D. b,c都必须是Com的对象class Com. .friend Com operator +();;答案:B.填空题1.利用成员函数对双目运算符重载,其左操作数为,右操作数为。解:将双目运算符重载为类的成员函数时,由于 this指针在每次非静态成员函数操作对 象时都作为第一个隐式参数传递给对象,因此它充当了双目运算符的左操作数,而该成员函数的形参则表示双目运算符的右操作数。本题答案为: this指针成员函数参数。2. 运算符重载仍然保持其原来的优先级、 和。解:运算符重载不能改变操作数的个数、运算符的优先级、运算符的结合性和运算符的

5、语法结构。本题答案为:结合性语法结构。3. 为了满足运算符“ +”的可交换性,必须将其重载为 。解:以友元函数方式重载运算符可满足运算符“+”的可交换性,因为两个操作数都作为参数,例如,有以下程序: #include class Sample int n ; public :Sample()Sample(int i)n=i; friend Sample operator+(Sample, Sample);void disp() coutn= nendl ; Sample operator+(sample s1 , Sample s2) return sample(S1 . n+s2 . n);

6、void main()Sample S1(2), s2(5) , s3;cout S1:; S1. disp();cout s2 :; s2 . disp();s3=S1+S2 ;cout s3:; s3 . disp();S3=S2+S1 ;cout s3:; s3. disp();程序执行结果如下:从中看到,执行sl+s2和s2+sl的结果是相同的,从而满足运算符“+”的交换性。所以本题答案为:友元函数。4 .在C+中,运算符的重载有两种实现方法,一种是通过成员函数来实现,另一种则通过(4)来实现。答案:友元5 .当用成员函数重载双目运算符时,运算符的左操作数必定是:答案:对象三.程序分析

7、题1 .以下程序的执行结果是 。#include class Sample int n ; public : Sample() Sample(int m)n=m ;) int &operator-(int) n-;return n ; void disp() cout rl= nendl;) void main() Sample s(10);(S-)+;S . disp(); 解:该程序中,类Sample的重载“ 一”运算符的成员函数返回Sample对象的私有数据成员n的引用。在main()函数中执行(s-)+ ;语句时,先调用重载成员函数,使 S对象的 n值减1,这时返回的是 S对象的n的引

8、用,再执行+运算,也就是对 s对象的n值执行 +运算,所以s对象的11值保持不变。程序白执行结果为:n=lO。2 .以下程序的执行结果是 : #include class Sample private : int x ; public : Sample()x=0 ; void disp()cout x= xendl ;void 0perator+()x+=10; void main() Sample obj ; obj . disp(); obj+ ; cout “执行。bj+ 之后”endl ; obj . disp();解:在类Sample中设计运算符重载成员函数,使运算符“+”作为于Sa

9、mple类对象时,该对象的x 增 10。本题答案为:x=0执行 obj+ 之后x=103 阅读下面的程序,在空白处填写出程序运行后的结果。#include class complexfloat real , imag,public :complex(float r , float i)real=r ; imag=i ; complex()real=O ; imag=O; void print() ;一 (complex a (complex a (complex a, complex b) complex b) complex b)friend complex operator+(comple

10、x a, complex b) ;friend complex operator friend complex operator friend complex operator void complex : : print()coutO)cout ”+”;if(imag! : 0) coutimag” i ” endl ;complex operator+(complex acomplex temp ;temp real=a real+b temp imag=a imag+breturn temp ;complex operator-(complex acomplex b)real ;imag

11、;complex b)complex temp ;temp real=a real-b real ;temp imag=a imag-b imag;return temp ;complex operator *(complex a , complex b)complex temp ;temp real : a real*b real-a imag *b imag; temp . imag=a. real *b . imag+a. imag *b . real ; return temp ;complex operator / (complex a , complex b) complex te

12、mp ; float tt ;tt=l /(b . real b. real+b . imag。b. imag); temp. real=(a .real*b. real+a . imag*b.imag)*tt;temp. imag=(b .real*a. imag-a . real*b .imag)*tt;return temp ;void main() complex c1(2. 3, 4. 6), c2(3 . 6, 2. 8), c3;c1. print();c2. print();c3=c1+c2;c3. print();c3=c1-c2;c3. print();c3=c1*c2;c

13、3. print();c3=c1/ c2;c3. print();程序运行结果为:2. 3+4. 6i3. 6+2. 8i4. )解:(1)、(2)、(3)和(4)分别是2.3+4 . 6i和3.6+2.8i两个复数相加、相减、相乘和相 除的结果。本题答案为:(1)5.9+7.4i(2)-1.3+1.8i-4.6+23i;(4)A.1.01731+0.486538i。四.简答题1 说明以下类date 的功能,并给出程序执行结果。#includestatic int dys=31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);c1ass dateint mo ,rda, ryr;public :date(int m , int d , int y)mo=m ; da=d

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号