C++-常见英文面试笔试题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

C/C++ProgramminginterviewquestionsandanswersBySatishShetty,July14th,2004Whatisencapsulation??Containingandhidinginformationaboutanobject,suchasinternaldatastructuresandcode.Encapsulationisolates(使隔离)theinternalcomplexityofanobject'soperationfromtherestoftheapplication.Forexample,aclientcomponentaskingfornetrevenue(收益)fromabusinessobjectneednotknowthedata'sorigin.Whatisinheritance?Inheritanceallowsoneclasstoreusethestateandbehaviorofanotherclass.Thederivedclassinheritsthepropertiesandmethodimplementationsofthebaseclassandextendsitbyoverridingmethodsandaddingadditionalpropertiesandmethods.WhatisPolymorphism??Polymorphismallowsaclienttotreatdifferentobjectsinthesamewayeveniftheywerecreatedfromdifferentclassesandexhibit(展现)differentbehaviors.Youcanuseimplementation(实现)inheritancetoachievepolymorphisminlanguagessuchasC++andJava.Baseclassobject'spointercaninvoke(调用)methodsinderivedclassobjects.YoucanalsoachievepolymorphisminC++byfunctionoverloadingandoperatoroverloading.Whatisconstructororctor?Constructorcreatesanobjectandinitializesit.Italsocreatesvtable变量列表?forvirtualfunctions.Itisdifferentfromothermethodsinaclass.Whatisdestructor?Destructorusuallydeletesanyextraresourcesallocatedbytheobject.Whatisdefaultconstructor?Constructorwithnoargumentsoralltheargumentshasdefaultvalues.Whatiscopyconstructor?Constructorwhichinitializestheit'sobjectmembervariables(byshallowcopying)withanotherobjectofthesameclass.Ifyoudon'timplementoneinyourclassthencompilerimplementsoneforyou.forexample:BooObj1(10);//callingBooconstructorBooObj2(Obj1);//callingboocopyconstructorBooObj2=Obj1;//callingboocopyconstructorWhenarecopyconstructorscalled?Copyconstructorsarecalledinfollowingcases:a)whenafunctionreturnsanobjectofthatclassbyvalueb)whentheobjectofthatclassispassedbyvalueasanargumenttoafunctionc)whenyouconstructanobjectbasedonanotherobjectofthesameclassd)WhencompilergeneratesatemporaryobjectWhatisassignmentoperator?Defaultassignmentoperatorhandlesassigningoneobjecttoanotherofthesameclass.Membertomembercopy(shallowcopy)Whatarealltheimplicitmemberfunctionsoftheclass?Orwhatareallthefunctionswhichcompilerimplementsforusifwedon'tdefineone.??defaultctorcopyctorassignmentoperatordefaultdestructoraddressoperatorWhatisconversionconstructor?constructorwithasingleargumentmakesthatconstructorasconversionctoranditcanbeusedfortypeconversion.forexample:classBoo{public:Boo(inti);};BooBooObject=10;//assigningint10BooobjectWhatisconversionoperator??classcanhaveapublicmethodforspecificdatatypeconversions.forexample:classBoo{doublevalue;public:Boo(inti)operatordouble(){returnvalue;}};BooBooObject;doublei=BooObject;//assigningobjecttovariableioftypedouble.nowconversionoperatorgetscalledtoassignthevalue.Whatisdiffbetweenmalloc()/free()andnew/delete?mallocallocatesmemoryforobjectinheapbutdoesn'tinvokeobject'sconstructortoinitiallizetheobject.newallocatesmemoryandalsoinvokesconstructortoinitializetheobject.malloc()andfree()donotsupportobjectsemanticsDoesnotconstructanddestructobjectsstring*ptr=(string*)(malloc(sizeof(string)))ArenotsafeDoesnotcalculatethesizeoftheobjectsthatitconstructReturnsapointertovoidint*p=(int*)(malloc(sizeof(int)));int*p=newint;Arenotextensiblenewanddeletecanbeoverloadedinaclassdeletefirstcallstheobject'sterminationroutine(i.e.itsdestructor)andthenreleasesthespacetheobjectoccupiedontheheapmemory.Ifanarrayofobjectswascreatedusingnew,thendeletemustbetoldthatitisdealingwithanarraybyprecedingthenamewithanempty[]:-Int_t*my_ints=newInt_t[10];...delete[]my_ints;whatisthediffbetweennewandoperatornew?operatornewworkslikemalloc.Whatisdifferencebetweentemplateandmacro??Thereisnowayforthecompilertoverifythatthemacroparametersareofcompatibletypes.Themacroisexpandedwithoutanyspecialtypechecking.Ifmacroparameterhasapost-incrementedvariable(likec++),theincrementisperformedtwotimes.Becausemacrosareexpandedbythepreprocessor,compilererrormessageswillrefertotheexpandedmacro,ratherthanthemacrodefinitionitself.Also,themacrowillshowupinexpandedformduringdebugging.forexample:Macro:#definemin(i,j)(ij?i:j)template:templateclassTTmin(Ti,Tj){returnij?i:j;}WhatareC++storageclasses?autoregisterstaticexternauto:thedefault.Variablesareautomaticallycreatedandinitializedwhentheyaredefinedandaredestroyedattheendoftheblockcontainingtheirdefinition.Theyarenotvisibleoutsidethatblockregister:atypeofautovariable.asuggestiontothecompilertouseaCPUregisterforperformancestatic:avariablethatisknownonlyinthefunctionthatcontainsitsdefinitionbutisneverdestroyedandretains=keepitsvaluebetweencallstothatfunction.Itexistsfromthetimetheprogrambeginsexecutionextern:astaticvariablewhosedefinitionandplacementisdeterminedwhenallobjectandlibrarymodulesarecombined(linked)toformtheexecutablecodefile.Itcanbevisibleoutsidethefilewhereitisdefined.WhatarestoragequalifiersinC++?Theyare..constvolatilemutableConstkeywordindicatesthatmemoryonceinitialized,shouldnotbealteredbyaprogram.volatilekeywordindicatesthatthevalueinthememorylocationcanbealteredeventhoughnothingintheprogramcodemodifiestheconten

1 / 14
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功