您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 创业/孵化 > 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
本文标题:C++-常见英文面试笔试题
链接地址:https://www.777doc.com/doc-7183429 .html