第11章运算符重载习题解答_第1页
第11章运算符重载习题解答_第2页
第11章运算符重载习题解答_第3页
第11章运算符重载习题解答_第4页
第11章运算符重载习题解答_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

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

3、. fun operator +=(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)

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

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

6、mple s2) return sample(S1n+s2n);void main() Sample S1(2),s2(5),s3; cout”S1:” ;S1disp(); cout”s2:” ;s2disp(); s3=S1+S2; cout”s3:” ; s3disp(); S3=S2+S1; cout”s3:” ;s3disp();程序执行结果如下:从中看到,执行 sl+s2 和 s2+sl 的结果是相同的,从而满足运算符“+”的交换性。所以本题答案为:友元函数。4在 c+中,运算符的重载有两种实现方法,一种是通过成员函数来实现,另一种则通过 (4) 来实现。答案:友元5. 当用成员函

7、数重载双目运算符时,运算符的左操作数必定是: (5) 答案:对象三程序分析题三程序分析题1 以下程序的执行结果是 。#includeclass 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-)+; Sdisp();解:该程序中,类 Sample 的重载”一”运算符的成员函数返回 Sample 对象的私有数据成员 n 的引用。在 main()函数中执行(s-

8、)+;语句时,先调用重载成员函数,使 S 对象的 n 值减 1,这时返回的是 S 对象的 n 的引用,再执行+运算,也就是对 s 对象的 n值执行+运算,所以 s 对象的 11 值保持不变。程序的执行结果为:n=lO。2以下程序的执行结果是 :#includeclass Sampleprivate: int x;public: Sample()x=0; void disp()cout”x=”xendl; void 0perator+()x+=10;void main() Sample obj; objdisp(); obj+; cout“执行。bj+之后”endl ; objdisp();解:

9、在类 Sample 中设计运算符重载成员函数,使运算符“+”作为于 Sample 类对象时,该对象的 x 增 10。本题答案为:x=0执行 obj+之后x=103 阅读下面的程序,在空白处填写出程序运行后的结果。#includeclass complex float real,imag,public: complex(float r,float i)real=r;imag=i; complex()real=O; imag=O; void print(); friend complex operator+(complex a,complex b); friend complex operator

10、 一(complex a,complex b); friend complex operator(complex a,complex b); friend complex operator(complex a,complex b);void complex:print() coutO)cout”+” ; if(imag!:0) coutimag”i”endl;complex operator+(complex a,complex b) complex temp; tempreal=areal+breal; tempimag=aimag+bimag; return temp;complex op

11、erator-(complex a,complex b) complex temp ; tempreal=areal-breal; tempimag=aimag-bimag; return temp;complex operator *(complex a,complex b) complex temp; tempreal:areal*breal-aimag *bimag; tempimag=areal *bimag+aimag *breal; return temp;complex operator(complex a,complex b) complex temp; float tt; t

12、t=l(brealbreal+bimag。bimag); tempreal=(areal*breal+aimag*bimag)*tt; tempimag=(breal*aimag-areal*bimag)*tt; return temp;void main() complex c1(23,46),c2(36,28),c3; c1print(); c2print(); c3=c1+c2; c3print(); c3=c1-c2; c3print(); c3=c1*c2 ; c3print(); c3=c1c2; c3print();程序运行结果为:23+46i36+28i (1) (2) (3)

13、 (4) 解:(1)、(2)、(3)和(4)分别是 2.3+46i 和 3.6+2.8i 两个复数相加、相减、相乘和相除的结果。本题答案为:(1)5.9+7.4i (2)-1.3+1.8i (3)-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 date int mo ,rda, ryr;public: date(int m,int d,int y)mo=m;da=d;yr=y;

14、date() void disp()coutmo ”da“”yrdysdtmo-1) day-=dysdtmo-1; if(+dtmo=13) dtmo=1; dtyr+; dtda=day; return dt; void main() date dl(2,10,2003),d2; d2=dl+365; d2disp();解:类 date 含有 mo、da 和),r 3 个私有数据成员,分别存放一个日期的月份、日号和年份。其成员函数有:两个构造函数,disp0 成员函数用于显示日期, “+”运算符重载为计算指定日期加上指定天数后的日期。本程序输出 2003 年 2 月 10 日加上 365

15、天后的日期,结果为:2102004。2 说明以下类 Words 的功能,并给出程序执行结果。#include#includeclass Words char *str; public: Words(char *s) str=new charstrlen(s)+1; strcpy(str,s); void disp()coutstr=A& stri=a&stri=z) / 4写字母 return char(stri-32); else / 其他字符 return stri; void main() int i; char *s=”Hello” ; Words word(s); w

16、orddisp(); for(i=0;istrlen(s);i+) coutwordi; coutendl:解:Words 类含有一个私有数据,其成员函数有:一个构造函数,disp()函数用于输出str,下标运算符重载函数用于将指定下标的字母大小写互换,其他字符时不变。本程序的输出结果如下: Hell() heLL()3 有以下两个程序,分析它们的执行结果有什么不同。程序 l: #include class Point int x,y; public: Point()x=y=0; Point(int i,int j)x=i;y=j; Point operator+(Point); void d

17、isp() ( cout”(”x” , ”y”)”x+=Px; this-y+=py; return *this;void main() Point pl(2,3),p2(3,4),p3; cout”pl:” ;p1disp(); cout”p2:” ;p2disp(); p3=pl+p2; cout”执行 p3=pl+p2 后”endl; cout”pl:” ,p1disp(); cout”p2:” ;p2disp(); cout”p3:” ;p3disp();程序 2:#includeclass Point int x,Y;public: Point()x=y=O; Point(int i

18、,int j)x=i,y=j; Point operator+(Point); void disp f)cout ”(”x ” , ”y”)”endl; Point Point:operator+(Point P) Point s; sx=x+px; sy=y+py; return s;void main() Point pl(2,3),p2(3,4),p3; cout”pl:” ;p1disp(); cout”p2:” ;p2disp(); p3=pl+p2; cout”执行 p3=pl+p2 后”endl; cout”pl:” ;p1disp(); cout”p2:” ;p2disp();

19、 coutx+=px;this-y 十一 py;语句,修改 p l 对象的 x 和 y 成员值,执行return*this;语句,将 pl 对象赋给 p3。所以 p1 和 p3 两个对象的 x、Y 值相同,即p3=pl+p2 等价于 p1=p1+p2,p3:pl,其运行结果如下: pl:(2,3) p2:(3,4)执行 p3=pl+p2 后 P1:(5,7) p2:(3,4) P3:(5,7)对于程序 2,执行运算符重载函数,Point s;语句定义一个对象,sx=x+px;sy=y+py;语句用于修改 s 对象的 x、Y 值,retill s;语句返回该对象,赋给 p3,而 pl 和 p2

20、对象不改变。其运行结果如下: pl:(2,3) p2:(3,4)执行 p3=pl+p2 后 pl:(2,3) p2:(3,4) p3:(5,7)五完善程序题五完善程序题1.本程序调用构造函数实现字符串对象的初始化。调用重载运算符”+”把两个字符串拼接,并通过重载运算符“”来实现字符串的比较运算。#include#includeclass stringchar *str;public:string(char *s=0)if(_(14)_)str=new charstrlen(s)+1; strcpy(_(15)_);else str=0;friend string operator+(strin

21、g &,string &);int operator(string &);void show() if(str)coutstr(string &s)if(strcmp(_(18)_)0)return 1;else return 0;void main(void)string s1(southeast university),s2(mechanical department);string s3; s3=s1+s2;s1.show(); s2.show(); s3.show();couts2)n;答案:(14) s (15) str,s (16) s1.str (

22、17) return t (18) str,s.str六上机题六上机题(一)改错题(一)改错题(二)编程题(二)编程题1 定义一个计数器类 Counter,对其重载运算符“+” 。解:计数器类 Counter 含有一个私有整型数据成员 n, “+”运算符重载为使对象的 n值增 l。程序如下:#includeclass Counter int n; public: Counter()n=O; / 默认构造函数 Counter(int i)n=i; / 构造函数 Counter operator+(Counter c) / 运算符重载函数 Counter temp; tempn=n+cn; ret

23、urn temp; void disp() cout”n=”rlendl; void main() Counter cl(5),c2(10),c3; c3=c1+c2; c1disp(); c2disp(); c3disp();2 C+在运行期间不会自动检查数组是否越界。设计一个类能够检查数组是否越界。解:设计一个类 Words,下标运算符重载为:判断指定的下标是否越界,越界时显示相应的错误信息,未越界时返回该下标的字符。程序如下:#include#incudeclass Words int len; / str 所指字符串的长度 char *str; / 字符串指针public: Words

24、(char *s) / 构造函数 str=new charstrlen(s)+1; strcpy(str,s); len=strlen(s); char operator(int n) if(nlen-1) cout”数组下标越界” ; return ; / 返回一个特殊字符 else return*(str+n); void disp()coutstrendl;void main() Words word(”Goodbye”); worddisp(); cout”位置 0:” ; coutword0endl; / 显示G cout”位置 15:” ; coutword15endl; / 越界

25、3 设计一个日期类 Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。解:在 Date 类中设计如下重载运算符函数: Date operator+(int days):返回某日期加上天数得到的日期 Date operator(int days):返回某日期减去天数得到的日期int operator-(Date&b):返回两日期相差的天数在实现这些重载运算符函数时调用以下私有成员函数:leap(int):判断指定的年份是否为闰年dton(Date &):将指定日期转换成从 0 年 O 月 O 日起的天数.ntod

26、(int):将指定的 0 年 O 月 O 日起的天数转换成对应的日期程序如下:#includeint day_tab212=31,28,31,30,3l,30,3l,3l,30,31,30,31), 31,29,31,30,31,30,31,31,30,31,30,31; / day_tab 二维数组存放各月天数,第一行对应非闰年,第二行对应闰年class Date int year,month,day; / 年,月,日 int leap(int); / 判断是否为闰年 int dton(Date&); Date ntod(int);public: Date() Date(int Y,

27、int m,int d) year=y;month=m;day=d; void setday(int d)day=d; void setmonth(int m)month=m; void setyear(int y)year=y; int getday()return day; int getmonth()return month; int getyear()return year; Date operator+(int days) / +运算符重载函数 static Date date ; int number=dton(*this)+days; date=ntod(number); ret

28、urn date; Date operator-(int days) / 一运算符重载函数 Static Date date; int number=dton( *this); number-=days; date=ntod(number); return date; int operator-(data &b) / 一运算符重载函数 int days=dton(*this)-dton(b)-1; return days; void disp() / 输出日期 coutyear” ”month” ”dayendl:int Date:leap(int year) if(year4=0 &

29、amp; year100!=0 | year400=0)/ 是闰年 return 1; else / 不是闰年 return 0;int Date:dton(Date)&d) / 求从公元 0 年 0 月 0 日到 d 日期的天数 int y,m,days=0; for (y=1;y=dyear;y+) if(1eap(y)days+=366; / 闰年时加 366 天 else days+=365; / 非闰年时加 365 天 for(m=0;mdmonth-1;m+) if(leap(dyear)days+=day_tab1m; else days+=day_tab0m; days

30、+=dday; return days;Date Date:ntod(int n) / 将从公元 0 年 0 月 0 日的天数转换成日期 int y=l;m=l;d, rest=n,lp; while(1) if(leap(y) / 闰年 if(rest=366) break; else rest-=366; else / IP 闰年 if(restday;tab1m-1)rest-=day_tab1m-i; else break; else / 非闰年 if(restday_tab0m-1)rest-=day_tab0m-1; else break; m+: d=rest; return D

31、ate(y,m,d);void mein() Date now(2003,10,1),then(2005,6,5); cout”now:” ;nowdisp(); cout”then:” ;thendisp(); cout”相差天数:”(thennow)endl; Date dl=now+1000,d2=now-i000; cout”now+1000:” ;dldisp(); cout”now1000:” ;d2disp();本程序的执行结果如下: now:2003101 then:200565 相差天数:612 now+1000:200 6627 now 一 1000:20011144 定义

32、如下集合类的成员函数,并用数据进行测试。class Set int *elem; / 存放集合元素的指针 int count ; / 存放集合中的元素个数public: Set(); Set(int s,int n); int find(int x)const; /判断 X 是否在集合中 Set operator+(const Set); /集合的并集Set operator-(const Set); /集合的差集 Set operator*(const Set);/集合的交集 void disp(); /输出集合元素解:该类的运算符重载均采用成员函数方式实现。程序如下:#includecon

33、st int N=100; /集合中最多元素个数class Set int * elem; /存放集合元素的指针 int count; /存放集合中的元素个数public: Set(); Set(int S,int n); int find(int x)const; /判断 X 是否在集合中 Set operator+(const Set);/集合的并集 Set operator-(const Set); /集合的差集 Set operator*(const Set); /集合的交集 void disp(); /输出集合元素Set:Set() /默认构造函数 elem=NULL; count=

34、O;Set:Set(int s,int n) / 初始化构造函数 int i; elem=new intn; for(i=0; in; i+) *(elem+i)=si; count=n;int Set:find(int x)const int i; for(i=O;icount;i+) if(*(elem+i)=x) return 1;return 0;Set Set:operator+(const set &s) int aN,i,k; for(i=0;icount;i+) / 先将当前集合元素放入 a 数组中 ai=elemi; k=count; for(i=0;iscount;

35、i+) / 将集合 s 中不在当前集合中的元素放入a 数组中 if(find(selemI) continue; else if(k=N-1)break; ak+=selemi; return Set(a,k); / 调用构造函数产生一个无名对象返回Set Set:operator-(const Sets) / 集合的差集 int aN,i,k=0; for(i=0;icount;i+) / 将当前集合中不在 s 集合中的元素放入 a 数组中 if(!sfind(elemi) ak+=elemi; return Set(a,k); / 调用构造函数产生一个无名对象返回Set Set:opera

36、tor*(const Set &s) / 集合的交集 int aN,k=0,i; for(i=0;icount;i+) / 扫描当前集合中所有元素 if(sfind(elemi) ak+=elemi; / 将属于两个集合的元素放在 a 数组中 return Set(a,k); / 调用构造函数产生一个无名对象返回void Set:disp() / 输出集合元素 int i; for(i=0;icount;i+) cout*(elem+i)” ” ; coutendl:void main() int a=1,4,2,7,8; int b=2,3,1,6,7,; Set s1(a,5),s2(b,5),s3;cout”集合 S1:” ;s1disp(); cout”集合 s2:” ; s2disp(); S3=s1+S2; cout”并集:” ;s3disp(); S3=s1*S2; cout”交集:” ;S3disp(); S3=s1-s2; cout”差集:” ;s3disp()j本程序的执行结果如下:集合 S1:1 4 2 7 8集合 S2:2 3 1 6 7并集:1 4 2

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论