c++英语文献.docx
《c++英语文献.docx》由会员分享,可在线阅读,更多相关《c++英语文献.docx(11页珍藏版)》请在冰豆网上搜索。
c++英语文献
Object-OrientationandC++
C++isjustoneofmanyprogramminglanguagesinusetoday.Whyaretheresomanylanguages?
Whydonewonesappeareveryfewyears?
Programminglanguageshaveevolvedtohelpprogrammerseasethetransitionfromdesigntoimplementation.
Thefirstprogramminglanguageswereverydependentontheunderlyingmachinearchitecture.Writingprogramsatthislevelofdetailisverycumbersome.Justashardwareengineerslearnedhowtobuildcomputersystemsoutofothercomponents,languagedesignersalsorealizedthatprogramscouldbewrittenatamuchhigherlevel,therebyshieldingtheprogrammerfromthedetailsoftheunderlyingmachine.
Whyaretheresuchalargenumberofhigh-levelprogramminglanguages?
Therearelanguagesforaccessinglargeinventorydatabases,formattingfinancialreports,controllingrobotsonthefactoryfloor,processinglists,controllingsatellitesinrealtime,simulatinganuclearreactor,predictingchangingatmosphericconditions,playingchess,anddrawingcircuitboards.Eachoftheseproblemsrequiresdifferentsetsofdatastructuresandalgorithms.Programminglanguagesaretoolstohelpussolveproblems.However,thereisnotoneprogramminglanguagethatisbestforeverytypeofproblem.Newprogramminglanguagesareoftendevelopedtoprovidebettertoolsforsolvingaparticularclassofproblems.Otherlanguagesareintendedtobeusefulforavarietyofproblemdomainsandaremoregeneralpurpose.
Eachprogramminglanguageimpartsaparticularprogrammingstyleordesignphilosophyonitsprogrammers.Withthemultitudeofprogramminglanguagesavailabletoday,anumberofsuchdesignphilosophieshaveemerged.Thesedesignphilosophies,calledprogrammingparadigms,helpustothinkaboutproblemsandformulatesolutions.
SoftwareDesignthroughParadigms
Whendesigningsmallcomputerprogramsorlargesoftwaresystems,weoftenhaveamentalmodeloftheproblemwearetryingtosolve.Howdowedeviseamentalmodelofasoftwaresystem?
Programmingparadigmsoffermanydifferentwaysofdesigningandthinkingaboutsoftwaresystems.Aparadigmcanbethoughtofasamentalmodelorasaframeworkfordesigninganddescribingasoftwaresystem'sstructure.Themodelhelpsusthinkaboutandformulatesolutions.
Wecanusethementalmodelofaparadigmindependentlyfromtheprogramminglanguagechosenforimplementation.However,whenthechosenlanguageprovidesconstructsandmechanismsthataresimilartothosethatarefoundintheparadigm,theimplementationwillbemorestraightforward.Usually,thereareseverallanguagesthatbelongtoaparadigm.Forthisreason,aprogrammingparadigmisalsoconsideredaclassoflanguages.
Alanguagedoesnothavetofitintojustoneparadigm.Moreoften,languagesprovidefeaturesorcharacteristicsfromseveralparadigms.Hybridlanguages,suchasC++,combinecharacteristicsfromtwoormoreparadigms.C++includescharacteristicsfromtheimperativeandproceduralparadigms--justlikeitspredecessorlanguage,C--andtheobject-orientedparadigm.
Theimperativeparadigmischaracterizedbyanabstractmodelofacomputerwithalargememorystore.ThisistheclassicvonNeumannmodelofcomputerarchitecture.Computations,whichconsistofasequenceofcommands,arestoredasencodingwithinthestore.Commandsenablethemachinetofindsolutionsusingassignmenttomodifythestore,variablestoreadthestore,arithmeticandlogictoevaluateexpressions,andconditionalbranchingtocontroltheflowofexecution.
Theproceduralparadigmincludestheimperativeparadigm,butextendsitwithanabstractionmechanismforgeneralizingcommandsandexpressionsintoprocedures.Parameters,whichareessentiallyaliasesforaportionofthestore,werealsointroducedbythisparadigm.Otherfeaturesincludeiteration,recursion,andselection.Mostmainstreamsprogrammingtodayisdoneinaprocedurallanguage.
Theproceduralparadigmwasthefirstparadigmtointroducethenotionofabstractionintoprogramdesign.Thepurposeofabstractioninprogrammingistoseparatebehaviorfromimplementation.Proceduresareaformofabstraction.Theprocedureperformssometaskorfunction.Otherpartsoftheprogramcalltheprocedure,knowingthatitwillperformthetaskcorrectlyandefficiently,butwithoutknowingexactlyhowtheprocedureisimplemented.
DATAABSTRACTIONisconcernedwithseparatingthebehaviorofadataobjectfromitsrepresentationorimplementation.Forexample,astackcontainstheoperationsPush,Pop,andIsEmpty.Astackobjectprovidesuserswiththeseoperations,butdoesnotrevealhowthestackisactuallyimplemented.Thestackcouldbeimplementedusinganarrayoralist.Usersofthestackobjectdonotcarehowthestackisimplemented,onlythatitperformstheaboveoperationscorrectlyandefficiently.Becausetheunderlyingimplementationofthedataobjectishiddenfromitsusers,theimplementationcaneasilybechangedwithoutaffectingtheprogramsthatuseit.
Whenwedesignalgorithms,weoftenneedaparticulardatatypetouseinordertocarryoutthealgorithm'soperations.Thedesignofanalgorithmiseasierifwesimplyspecifythedatatypesofthevariables,withoutworryingabouthowtheactualdatatypeisimplemented.Wedescribethedatatypebyitspropertiesandoperationsandassumethatwhateverimplementationischosen,theoperationswillworkcorrectlyandefficiently.TypesdefinedinthiswayarecalledABSTRACTDATATYPES(ADTs).
Theuseofabstractdatatypesmakesthedesignofthealgorithmmoregeneral,andallowsustoconcentrateonthealgorithmathandwithoutgettingboggeddowninimplementationdetails.Afterthealgorithmshavebeendesigned,theactualdatatypeswillneedtobeimplemented,alongwiththealgorithms.Recently,procedurallanguageshavebeenextendedtosupportthedefinitionofnewdatatypesandprovidefacilitiesfordataabstraction.
Theobject-orientedparadigmretainsmuchofthecharacteristicsoftheproceduralparadigm,sinceproceduresarestilltheprimaryformforcomposingcomputations.However,ratherthanoperateonabstractvalues,programsintheobject-orientedparadigmoperateonobjects.Anobjectisverysimilartoanabstractdatatypeandcontainsdataaswellasprocedures.
Therearethreeprimarycharacteristicsoftheobject-orientedparadigm.Wehavealreadydescribedthefirst,ENCAPSULATION,themechanismforenforcingdataabstraction.ThesecondcharacteristicisINHERITANCE.Inheritanceallowsnewobjectstobecreatedfromexisting,moregeneralones.Thenewobjectbecomesaspecializedversionofthegeneralobject.Newobjectsneedonlyprovidethemethodsordatathatdifferbecauseofthespecialization.Whenanobjectiscreated(orderived)fromanotherobject,itissaidtoinheritthemethodsanddataoftheparentobject,andincludesanynewrepresentationsandneworrevisedmethodsaddedtoit.
Thethirdandfinalcharacteristicofobject-orientedprogrammingisPOLYMORPHISM.Polymorphismallowsmanydifferenttypesofobjectstoperformthesameoperationbyrespondingtothesamemessage.Forexample,wemayhaveacollectionofobjectswhichcanallperformasortoperation.However,wedonotknowwhattypesofobjectswillbecreateduntilrun-time.Object-orientedlanguagescontainmechanismsforensuringthateachsortmessageissenttotherightobject.
Encapsulation,inheritance,andpolymorphismareconsideredthefundamentalcharacteristicsofobject-orientedprogrammingandallobject-orientedlanguagesmustprovidethesecharacteristicsinsomeway.Notsurprisingly,languagessupportthesecharacteristicsinverydifferentways.Smalltalk,C++,Objective-C,andLispwithCLOS(theCommonLispObjectSystem)areallexamplesofobject-orientedlanguages,andeachprovidessupportforencapsulation,inheritance,andpolymorphism.
Constructinganobject-orientedprograminvolvesdeterminingtheobjectsthatareneededtosolvetheproblem.Theobjectsarethenusedtoconstructcomputationsthatdefinethebehaviorofthesoftwaresystem.Messagepassingisthefundamentalinteractionmechanismamongobjects.Messages(fromotherobjectsorprograms)aresenttoobjectstoinformthemtoperformoneoftheiroperations.
Objectsareresponsibleformaintainingthestateoftheirdata.Onlytheobjectmaymodifyitsinternaldata.Objectsmaythemselvesbeimplementedviaothersub-objects.Implementinganobjectinvolvesarecursiveprocessofbreakingitintosub-objectsuntilatsomeleveltheobjectsandmethodsdefinedonthemareprimitives.Atthispoint,themethodsanddataconsistofelementsthatcanbeimplementedusingthebasicconstructsprovidedbytheprogramminglanguage.
Oneofthemostimportantaspectsoftheobject-orientedparadigmishowitchangesourwayofthinkingaboutsoftwaresystems.Systemsarethoughtofasconsistingofindividualentitiesthatareresponsibleforcarryingouttheirownoperations.Eachobjectisconceivedandimplementedasself-contained.Suchamodelfacilitatessoftwaredesign(andlaterimplementation)becauseobjectsoftenmodelconceptualreal-worldentities.Designingsystemsusingtheobject-orientedparadigmresultsinsoftwaresystemsthatbehaveandappearmoreliketheirreal-lifecounterparts.
TheObject-OrientedCharacteristicsofC++
C++extendsCwithafacilityfordefiningnewdatatypes.AclassislikeaCstruct,butcontainsdataaswellasmethods.Inaddition,C++providesdifferentlevelsofaccesstothemembersofaclassinordertocontrolhowthemembersofaclasscanbemanipulatedfromoutsidetheclass.
Recallthattheimportanceofdataabstractionistohidetheimplementationdetails