C++面向对象程序设计双语教程(第3版)-参考答案【ch05】 Operator Overloading.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《C++面向对象程序设计双语教程(第3版)-参考答案【ch05】 Operator Overloading.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计双语教程(第3版)-参考答案【ch05】 Operator Overloading.docx(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、7 17 712 3 4 /( 7( /( /(operatorChapter 5 Operator Over Ioad i ng1. Mark the fol lowing staterments as true (T) or fa I se (F) and give reasons.C+ + a I Iows operators to be over Ioaded.C+ a I Iows new operators to be created.Any operator can be over Ioaded.How an operator works on bas i c types can
2、not be changed byover Ioad i ng.(5) The associativity of over Ioad i ng.(6) The precedence of an over Ioad i ng.(7) Over Ioaded ass i gnment funct i ons.(8) Over Ioaded operatoran operator can be changed byoperator cannot be changed byoperatoroperatoroperators must be dec Iared as non-membermember f
3、unct i ons for b i nary operatorsimp I icitly use th i s to obta i n access to the I eft operand of the operator.(9) A unary operator can be over Ioaded as a non-member funct i on.(10) A b i nary operator can be over Ioaded as a non -member funct i on tak i ng one argument.(11) A convers i on operat
4、or converts an object of one c I ass i nto an object of another cI ass.(12) The + (or -)operators can be over I oaded for both pref i x and postf i x i ncrement (or decrement).以下是对每个状态的标记和理由:(1) C+允许操作符重载。一真(T)理由:C+是一种面向对象的编程语言,它提供了强大的操作符重载机制,允许程序员 修改已有的操作符的行为。(2) C+允许创建新的操作符。一假(F)理由:C+不允许程序员创建新的操作符
5、。操作符是由语言本身定义和实现的,程序员 只能重载己有的操作符。(3)任何操作员都可能过载。一真(T理由:在C+中,几乎所有的操作符都可以重 载,包括算术操作符、比较操作符、逻辑操作符等。(4)操作符对基本类型的工作方式不能因操作符重载而改变。一假(F)理由:通过操作符重载,可以改操作符对基本类型的工作方式。例如,可以重载加法操 作符,使其能够连接两个字符串。(5)运算符的关联性可以通过运算符重载来改变。一真(T)理由:C+允许通过操作符重载来改变运算符的关联性。例如,可以通过重载操作 符来实现类似于字符串拼接的效果,将两个字符串连接在一起。#重载操作符用于从用户输入时间def _rshift
6、_(self, other):time_str = input(、请输入时间(格式:hh:mm:ss): time str. split(self, hour = int(time list0)self, minute = int(time_list1)self, second = int (time_list2)#重载操作符用于输出时间到控制台def _Ishift(self, other):print (f 现 在 的 时 间self, hour:02d:self, minute:02d:self, second:02d使用这个修改后的类,你可以这样输入和输出时间:pythontime 二
7、 Time ()time None#输入时间time 操作符,以实现从用户输入时间。 然后使用方法重载了操作符,以实现将时间输出到控制台。9. Design a Percent cI ass that represents the percentage of 10%, 80% and etc. Over I oad operators and to imp I ement the output and i nput of Percent objects; Over Ioad operators = and to compare the re I at i onsh i p of two Per
8、cent objects.、 cpp ttinclude class Percentage public:Percentage(int value) : m value (value) friend std:ostream& operator(std:istream& is, Percentagefe percentage);friend bool operator= (const Percentagefe pl, const Percentagefe p2); friend bool operator(const Percentage& pl, const Percentagefe p2);
9、private:int m_value; /存储百分比的整数值);std:ostream& operator(std:ostream& os, const Percentagefc percentage) os percentage. m_value (std:istreamfe is, Percentagefe percentage) is percentage. m_value;is. ignored, %); 忽略输入中的百分号字符 return is;)bool operator=(const Percentagefe pl, const Percentagefe p2) return
10、 pl. m_value = p2. m_value;)bool operator (const Percentagefc pl, const Percentage& p2) return pl. m value pl;std:coutstd:coutpl: p2: pl std:endl; p2 std:endl;if (pl =p2)std:cout else if (plstd:cout else std:cout pl is p2) pl is pl isequal to p2 std:endl;less than p2 std:endl;greater than p2 std:end
11、l;return 0; (6)假(False)。操作符的重载可以改变操作符的优先级。例如,重载的操作符可以优 先级高于原始操作符。(7)假(False)。过载的分配操作符可以声明为成员函数或非成员函数。通常,推荐将 其声明为成员函数,以便操作符能够访问类的私有成员。(8)真(True)。二进制运算符的成员函数隐式地使用它来获取对左操作数的访问权,而 右操作数作为函数的参数传递给该函数。(9)真(True)。一元运算符可以作为非成员函数重载,特别是当一元运算符作用于类对 象时,非成员函数重载提供了更灵活的选项。(10)假(F):二进制运算符一般作为成员函数或全局函数来定义,并且通常需要两个参数。
12、(11)真(T):转换运算符可以定义将一个类的对象转换为另一个类的对象,这样可以在类 型之间进行隐式转换。(12)真(T):前缀和后缀增量(或递减)运算符可以被重载,使其可以根据需要在前缀或 后缀位置使用。例如,+i和i+都可以使用重载的增量运算符。2. Given the fol lowing program.chssRecta咽.private,;:;二工 * ., . . . , , . , , - - . . , . , . , . , ,. .public:Rectangle(int 1 = 0, int w = 0):leng1h(l), width(w) . f * . _ -
13、, * * * * * , . . * . a , * . . . . , , .t. - coutvvconstructing a Rectangle object.n; , . , , , * . . , .:J . . *. .* *. f*, *. * * . .,,, . , * . : .:;- :.,.:;*三Ecw/feum三.二e:二工* 三学三:了:intmainO * ,t * * *,*, - *.*?*,* *Rectangle red 1(5,6), rect2(7,8), rectS;rect3 - recti + rect2;coutvv”leiigth=vre
14、ct3 .getLengthO” widthMrect3. getWidth()endl;.二三;.;;:三:三千二.:二三;二二二二二二.二.:.;一,.::十标811t匕门的=对”营旗晨晤加 W width=wrect3 gctWidtiiOendl;、:;.,itteimO;;二.一二二二二 * . *. . . . * . . , , . ,.一 : * , , , * . .J1 s:匚.V- . . 一. . * ,; 1. , : . , : 4 . . . . , * , , ., . . , .Define over Ioad i ng operators =,+ and +
15、 and write out the output of the program.略。3. Write a program that def i nes a Date cI ass to implement the fol lowing operat i ons:)z )z !/ 1712 3 4/( /( /(X z(Pr i nt a date;Increment the date by one month;Compare two dates by re I at i on operators=, =, and !=;Over Ioad i nsert i on () and extrac
16、t i on () operators for easyi nput and output. (The Date cI ass requ i res the user to i nput/output a date in the form: dd/mm/yy)cppttinclude ttinclude class Date private:int day;int month;int year;public:Date(int d, int m, int y) : day(d), month(m), year (y) void printDate () std:cout day month ye
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ch05 C+面向对象程序设计双语教程第3版-参考答案 【ch05】 Operator Overloading C+ 面向 对象 程序设计 双语 教程 参考答案
![提示](https://www.taowenge.com/images/bang_tan.gif)
链接地址:https://www.taowenge.com/p-97918862.html
限制150内