C++常见英文面试笔试题.docx
《C++常见英文面试笔试题.docx》由会员分享,可在线阅读,更多相关《C++常见英文面试笔试题.docx(17页珍藏版)》请在冰豆网上搜索。
![C++常见英文面试笔试题.docx](https://file1.bdocx.com/fileroot1/2022-10/10/261a6873-5279-49db-b282-105b38fa2059/261a6873-5279-49db-b282-105b38fa20591.gif)
C++常见英文面试笔试题
C++-常见英文面试笔试题
C/C++Programminginterviewquestionsandanswers
BySatishShetty,July14th,2004
Whatisencapsulation?
?
Containingandhidinginformationaboutanobject,suchasinternaldatastructuresandcode.Encapsulationisolates(使隔离)theinternalcomplexityofanobject'soperationfromtherestoftheapplication.Forexample,aclientcomponentaskingfornetrevenue(收益)fromabusinessobjectneednotknowthedata'sorigin.
Whatisinheritance?
Inheritanceallowsoneclasstoreusethestateandbehaviorofanotherclass.Thederivedclassinheritsthepropertiesandmethodimplementationsofthebaseclassandextendsitbyoverridingmethodsandaddingadditionalpropertiesandmethods.
WhatisPolymorphism?
?
Polymorphismallowsaclienttotreatdifferent
tomembercopy(shallowcopy)
Whatarealltheimplicitmemberfunctionsoftheclass?
Orwhatareallthefunctionswhichcompilerimplementsforusifwedon'tdefineone.?
?
defaultctor
copyctor
assignmentoperator
defaultdestructor
addressoperator
Whatisconversionconstructor?
constructorwithasingleargumentmakesthatconstructorasconversionctoranditcanbeusedfortypeconversion.
forexample:
classBoo
{
public:
Boo(inti);
};
BooBooObject=10;//assigningint10Booobject
Whatisconversionoperator?
?
classcanhaveapublicmethodforspecificdatatypeconversions.
forexample:
classBoo
{
doublevalue;
public:
Boo(inti)
operatordouble()
{
returnvalue;
}
};
BooBooObject;
doublei=BooObject;//assigningobjecttovariableioftypedouble.nowconversionoperatorgetscalledtoassignthevalue.
Whatisdiffbetweenmalloc()/free()andnew/delete?
mallocallocatesmemoryforobjectinheapbutdoesn'tinvokeobject'sconstructortoinitiallizetheobject.
newallocatesmemoryandalsoinvokesconstructortoinitializetheobject.
malloc()andfree()donotsupportobjectsemantics
Doesnotconstructanddestructobjects
string*ptr=(string*)(malloc(sizeof(string)))
Arenotsafe
Doesnotcalculatethesizeoftheobjectsthatitconstruct
Returnsapointertovoid
int*p=(int*)(malloc(sizeof(int)));
int*p=newint;
Arenotextensible
newanddeletecanbeoverloadedinaclass
"delete"firstcallstheobject'sterminationroutine(i.e.itsdestructor)andthenreleasesthespacetheobjectoccupiedontheheapmemory.Ifanarrayofobjectswascreatedusingnew,thendeletemustbetoldthatitisdealingwithanarraybyprecedingthenamewithanempty[]:
-
Int_t*my_ints=newInt_t[10];
...
delete[]my_ints;
whatisthediffbetween"new"and"operatornew"?
"operatornew"workslikemalloc.
Whatisdifferencebetweentemplateandmacro?
?
Thereisnowayforthecompilertoverifythatthemacroparametersareofcompatibletypes.Themacroisexpandedwithoutanyspecialtypechecking.
Ifmacroparameterhasapost-incrementedvariable(likec++),theincrementisperformedtwotimes.
Becausemacrosareexpandedbythepreprocessor,compilererrormessageswillrefertotheexpandedmacro,ratherthanthemacrodefinitionitself.Also,themacrowillshowupinexpandedformduringdebugging.
forexample:
Macro:
#definemin(i,j)(ii:
j)
template:
template
Tmin(Ti,Tj)
{
returnii:
j;
}
WhatareC++storageclasses?
auto
register
static
extern
auto:
thedefault.Variablesareautomaticallycreatedandinitializedwhentheyaredefinedandaredestroyedattheendoftheblockcontainingtheirdefinition.Theyarenotvisibleoutsidethatblock
register:
atypeofautovariable.asuggestiontothecompilertouseaCPUregisterforperformance
static:
avariablethatisknownonlyinthefunctionthatcontainsitsdefinitionbutisneverdestroyedandretains=keepitsvaluebetweencallstothatfunction.Itexistsfromthetimetheprogrambeginsexecution
extern:
astaticvariablewhosedefinitionandplacementisdeterminedwhenallobjectandlibrarymodulesarecombined(linked)toformtheexecutablecodefile.Itcanbevisibleoutsidethefilewhereitisdefined.
WhatarestoragequalifiersinC++?
Theyare..
const
volatile
mutable
Constkeywordindicatesthatmemoryonceinitialized,shouldnotbealteredbyaprogram.
volatilekeywordindicatesthatthevalueinthememorylocationcanbealteredeventhoughnothingintheprogram
codemodifiesthecontents.forexampleifyouhaveapointertohardwarelocationthatcontainsthetime,wherehardwarechangesthevalueofthispointervariableandnottheprogram.Theintentofthiskeywordtoimprovetheoptimizationabilityofthecompiler.
mutablekeywordindicatesthatparticularmemberofastructureorclasscanbealteredevenifaparticularstructurevariable,class,orclassmemberfunctionisconstant.
struct