用C++解决问题第十版Chapter 11.docx
- 文档编号:29657312
- 上传时间:2023-07-25
- 格式:DOCX
- 页数:17
- 大小:18.40KB
用C++解决问题第十版Chapter 11.docx
《用C++解决问题第十版Chapter 11.docx》由会员分享,可在线阅读,更多相关《用C++解决问题第十版Chapter 11.docx(17页珍藏版)》请在冰豆网上搜索。
用C++解决问题第十版Chapter11
TRUE/FALSE
1.Friendfunctionsaremembersoftheclass.
ANSWER:
FALSE
2.Alloperatorscanbeoverloaded.
ANSWER:
FALSE
3.Ifyouhavemutatorsandaccessors,youshouldnothavefriendfunctionsalso
ANSWER:
FALSE
4.Friendfunctionsmaydirectlymodifyoraccesstheprivatedatamembers.
ANSWER:
TRUE
5.ThefollowingisaproperlydeclaredoverloadedinsertionoperatorformyClass.
ostream&operator<<(ostream&out,constmyClass&obj);
ANSWER:
TRUE
6.Functionsthatareconstantmemberfunctionsmaycalltheclassmutatorfunctions.
ANSWER:
FALSE
7.Functionsthatareconstantmemberfunctionsmaycallconstantclassaccessorfunctions.
ANSWER:
TRUE
8.Youcannotcreatenewoperators(suchasthequote).
ANSWER:
TRUE
9.Operatorsmustbefriendsoftheclass.
ANSWER:
FALSE
10.Youmaynotchangetheprecedenceofoperatorsbyoverloadingthem
ANSWER:
TRUE
ShortAnswer
1.Ifagiventaskbeingperformedbyafunctioninvolvesmorethanoneobject,thenthatfunctionshouldnormallybea__________function.
ANSWER:
friend
2.Ifagiventaskbeingperformedbyafunctioninvolvesoneobject,thenthatfunctionshouldnormallybea__________function.
ANSWER:
member
3.A_________functionisnotamemberoftheclass,buthasaccesstotheprivatemembersoftheclass.
ANSWER:
friend
4.Anoverloadedextractionorinsertionoperatorshouldreturn___________
ANSWER:
areferencetothestream
5.Afriendfunctionneedstobepassedanobjectoftheclass.Ifthefriendonlyneedstoaccesstheobject,butnotchangeitsdatamembers,thentheobjectshouldbepassedas_______________
ANSWER:
aconstantreference
6.Anoperatorthatexpectsonlyoneparameteriscalleda________operator
ANSWER:
unary
7.Anoperatorthatexpectstwoparametersiscalleda________operator.
ANSWER:
binary
8.Inordertodoautomatictypeconversionforyourclass,youwouldwrite_________
ANSWER:
overloadedfunctionsoroverloadedconstructors
9.Puttingthekeywordconstafterthefunctiondeclarationguarantees__________________________
ANSWER:
Thatthefunctionwillnotchangethecallingobject.
10.Puttingthekeywordconstinfrontofapassbyreferenceparameterguarantees___________________
ANSWER:
thatthefunctionwillnotmodifythatparameter.
11.WritethefunctiondeclarationforadestructorforaclassnamedmyClass
ANSWER:
~myClass();
12.WritethefunctiondeclarationforacopyconstructorforaclassnamedmyClass
ANSWER:
myClass(constmyClass&object_name);
13.WritethefunctiondeclarationforanassignmentoperatorforaclassnamedmyClass
ANSWER:
voidoperator=(constmyClass&object_name);or
myClass&operator=(constmyClass&object_name);
14.Boththecopyconstructorandtheassignmentoperatorshouldmake___________
ANSWER:
acompleteandindependentcopyofthedynamicvariables
15.Theassignmentoperatormustbea_________oftheclass.
ANSWER:
member
MultipleChoice
1.Howmanymembers(dataandfunctions)doesthefollowingclasshave?
classRational
{
public:
Rational();
Rational(intnumer,intdenom);
Rational(intwhole);
intgetNumerator();
intgetDenominator();
friendvoiddisplay(ostream&out,constRational&value);
private:
intnumerator;
intdenominator;
};
a.2
b.6
c.5
d.7
e.8
ANSWER:
D
2.Whocanaccessprivatedatainaclass?
a.membersoftheclass
b.friendsoftheclass
c.everyone
d.BandC
e.noone
ANSWER:
D
3.Giventhefollowingclass,whichisthecorrectfunctionheaderforthedisplayfunction?
classRational
{
public:
Rational();
Rational(intnumer,intdenom);
Rational(intwhole);
intgetNumerator();
intgetDenominator();
friendvoiddisplay(ostream&out,constRational&value);
private:
intnumerator;
intdenominator;
};
a.friendvoiddisplay(ostream&out,constRational&value)
b.voiddisplay(ostream&out,constRational&value)
c.voidRational:
:
display(ostream&out,constRational&value)
d.friendvoidRational:
:
display(ostream&out,constRational&value)
ANSWER:
B
4.Operatorscanbeoverloadedas
a.friendsofaclass
b.membersofaclass
c.non-friends,non-membersofaclass
d.Alloftheabove
ANSWER:
D
5.Ifwehaveafullselectionofaccessorandmutatorfunctions,whywouldwehavefriendfunctions?
a.Youshouldnothavethem
b.Moreefficientaccesstotheprivatedatamembers.
c.Thefriendfunctionmustcalltheaccessorormutatorfunctionsanyway.
d.noneoftheabove
ANSWER:
B
6.Sinceaccessorfunctionsinaclassdonotmodifyormutatethedatamembersoftheobject,thefunctionshouldhavethe__________modifier.
a.reference
b.friend
c.const
d.private
ANSWER:
C
7.Whyshouldyougenerallypassanobjectoftheclasstoafriendfunctionasareferenceparameter?
a.Ifthefriendfunctionchangesthevaluesofthedatamember(s).
b.Ifthefriendfunctionwillnotchangethevaluesofthedatamember(s).
c.Itismoreefficienttopasstheobjectbyreference.
d.AandB
e.AandC
ANSWER:
E
8.Ifcisacharactervariablethatcontainsadigit,whatdoesthefollowingfunctionreturn?
intdigit_to_int(charc)
{
return(int(c)–int('0'));
}
a.TheASCIIvalueofc
b.Thecharactervalueofc
c.Theintegerequivalentofthedigitstoredinc
d.noneoftheabove
ANSWER:
C
9.Whatiswrongwiththefollowingmemberfunctiondefinitiongiventheclassbelow?
classRational
{
public:
Rational();
Rational(intnumer,intdenom);
Rational(intwhole);
intgetNumerator()const;
intgetDenominator()const;
friendvoiddisplay(ostream&out,constRational&value);
private:
intnumerator;
intdenominator;
};
intRational:
:
getNumerator()const
{
numerator=0;
returnnumerator;
}
a.Youcannotsetthenumeratortozero
b.Thefunctionmaynotmodifynumerator,butitcanmodifydenominator
c.Thefunctionmaynotmodifyanyoftheprivatedatamembers
d.nothing
e.AandB
f.AandC
ANSWER:
F
10.Giventhefollowingclass,whatissyntacticallywrongwiththeimplementationofthedisplayfunction?
classRational
{
public:
Rational();
Rational(intnumer,intdenom);
Rational(intwhole);
intgetNumerator();
intgetDenominator();
friendvoiddisplay(ostream&out,constRational&value);
private:
intnumerator;
intdenominator;
};
voiddisplay(ostream&out,constRational&value)
{
out< } a.nothing b.valuemustbenotbepassbyreference c.Thegetfunctionsarenotconstfunctions d.outshouldbepassbyvalue ANSWER: C 11.Tooverloadfunctionswithsymbolicnames(like+-/<<),youmustusethekeyword_______beforethesymbolicname. a.const b.operator c.reference d.void ANSWER: B 12.Inthefollowingcodefragment,whichisthecallingobjectfortheless-thanoperator? strings1,s2; if(s1 a.s1 b.s2 c.< d.none ANSWER: A 13.Giventhefollowingclassdeclaration, classRational { public: Rational(); Rational(intnumer,intdenom); intgetNumerator()const; intgetDenominator()const; friendvoiddisplay(ostream&out,constRational&value); friendbooloperator(constRational&left,constRational&right); private: intnumerator; intdenominator; }; whatmustweaddtotheclassinorderforthefollowingcodetocompile? RationalmyRational(2,3); if(3 a.Weneedanother b.Weneedanother c.Weneedaconstructorthatexpectsarationnumber d.Weneedaconstructorthatexpectsaninteger e.AorD f.BorD ANSWER: F 14.Whenoverloadinganoperator,whichofthefollowingistrue? a.Oneoftheargumentsmustbeanobjectoftheclass b.Theoperatorcanbeafriendoramemberoftheclass. c.Theoperatordoesnothavetobeafriendoramemberoftheclass d.Alloftheabove e.Noneoftheabove ANSWER: D 15.Whatiswrongwiththefollowingoverloadedextractionoperatordeclaration? istream&operator>>(istream&in,constmyClass&object); a.Objectshouldnotbeapassbyreferenceparameter b.Objectshouldnotbeaconstparameter c.Youcannotputthe&onthereturntype d.nothing ANSWER: B 16.Whichofthefollowingwouldbeanappropriatefunctiondeclarationtoaddtworationalnumbers? a.voidfriendoperator+(constRational&left,constRational&right); b.voidoperatator+(constRational&left,constRational&right); c.friendRationaloperator+(constRational&left,constRational&right); d.Rationaloperator+(constRational&left,constRational&right); ANSWER: D 17.Howmanyparametersarethereinabinaryoperatorimplementedasafriend? a.0 b.1 c.2 d.asmanyasyouneed ANSWER: C 18.Howmanyparametersarethereinaunaryoperatorimplementedasafriend? a.0 b.1 c.2 d.asmanyasyouneed ANSWER: B 19.Giventhefollowingfunctiondeclaration, friendvoiddisplay(constmyClass&object); whichisthecorrectheaderforthedefinitionofthefunction? a.voidmyClass: : display(constmyClass&object) b.voiddisplay(constmyClass&object) c.friendvoiddisplay(constmyClass&object); d.friendvoiddisplay(constmyClass&object) ANSWER: B 20.WhichofthefollowingfunctiondeclarationswouldbecorrecttooverloadthemultiplyoperatorfortheRationalnumbersclass? a.friendRationaloperatortimes(constRational&left,constRational&right); b.Rationaloperatortimes(constRational&left,constRational&right); c.friendRationaloperator*(constRational&left,constRational&right); d.Rationaloperator*(constRational&left,constRational&right); ANSWER: C 21.Whyaretheextractionandinsertionoperatorsalwaysimplementedasfriendsoftheclassratherthanasmembersoftheclass? a.Becausethefirstparametermustbethestreamobject. b.Theydon't,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 用C+解决问题第十版Chapter 11 C+ 解决问题 第十 Chapter