毕业论文5000字英文文献翻译c++.docx
《毕业论文5000字英文文献翻译c++.docx》由会员分享,可在线阅读,更多相关《毕业论文5000字英文文献翻译c++.docx(17页珍藏版)》请在冰点文库上搜索。
毕业论文5000字英文文献翻译c++
英文翻译
英语原文:
.IntroducingClasses
Theonlyremainingfeatureweneedtounderstandbeforesolvingourbookstoreproblemishowtowriteadatastructuretorepresentourtransactiondata.InC++wedefineourowndatastructurebydefiningaclass.TheclassmechanismisoneofthemostimportantfeaturesinC++.Infact,aprimaryfocusofthedesignofC++istomakeitpossibletodefineclasstypesthatbehaveasnaturallyasthebuilt-intypesthemselves.Thelibrarytypesthatwe'veseenalready,suchasistreamandostream,arealldefinedasclassesthatis,theyarenotstrictlyspeakingpartofthelanguage.
Completeunderstandingoftheclassmechanismrequiresmasteringalotofinformation.Fortunately,itispossibletouseaclassthatsomeoneelsehaswrittenwithoutknowinghowtodefineaclassourselves.Inthissection,we'lldescribeasimpleclassthatwecanuseinsolvingourbookstoreproblem.We'llimplementthisclassinthesubsequentchaptersaswelearnmoreabouttypes,expressions,statements,andfunctionsallofwhichareusedindefiningclasses.
Touseaclassweneedtoknowthreethings:
Whatisitsname?
Whereisitdefined?
Whatoperationsdoesitsupport?
Forourbookstoreproblem,we'llassumethattheclassisnamedSales_itemandthatitisdefinedinaheadernamedSales_item.h.
TheSales_itemClass
ThepurposeoftheSales_itemclassistostoreanISBNandkeeptrackofthenumberofcopiessold,therevenue,andaveragesalespriceforthatbook.Howthesedataarestoredorcomputedisnotourconcern.Touseaclass,weneednotknowanythingabouthowitisimplemented.Instead,whatweneedtoknowiswhatoperationstheclassprovides.
Aswe'veseen,whenweuselibraryfacilitiessuchasIO,wemustincludetheassociatedheaders.Similarly,forourownclasses,wemustmakethedefinitionsassociatedwiththeclassavailabletothecompiler.Wedosoinmuchthesameway.Typically,weputtheclassdefinitionintoafile.Anyprogramthatwantstouseourclassmustincludethatfile.
Conventionally,classtypesarestoredinafilewithanamethat,likethenameofaprogramsourcefile,hastwoparts:
afilenameandafilesuffix.Usuallythefilenameisthesameastheclassdefinedintheheader.Thesuffixusuallyis.h,butsomeprogrammersuse.H,.hpp,or.hxx.Compilersusuallyaren'tpickyaboutheaderfilenames,butIDEssometimesare.We'llassumethatourclassisdefinedinafilenamedSales_item.h.
OperationsonSales_itemObjects
Everyclassdefinesatype.Thetypenameisthesameasthenameoftheclass.Hence,ourSales_itemclassdefinesatypenamed
Sales_item.Aswiththebuilt-intypes,wecandefineavariableofaclasstype.Whenwewrite"Sales_itemitem"wearesayingthatitemisanobjectoftypeSales_item.Weoftencontractthephrase"anobjectoftypeSales_item"to"aSales_itemobject"orevenmoresimplyto"aSales_item."
InadditiontobeingabletodefinevariablesoftypeSales_item,wecanperformthefollowingoperationsonSales_itemobjects:
Usetheadditionoperator,+,toaddtwoSales_items,
Usetheinputoperator,<Usetheoutputoperator,>>towriteaSales_itemobject,
Usetheassignmentoperator,=,toassignoneSales_itemobjecttoanother,
Callthesame_isbnfunctiontodetermineiftwoSales_itemsrefertothesamebook.
ClassesarecentraltomostC++programs:
Classesletusdefineourowntypesthatarecustomizedfortheproblemsweneedtosolve,resultinginapplicationsthatareeasiertowriteandunderstand.Well-designedclasstypescanbeaseasytouseasthebuilt-intypes.
Aclassdefinesdataandfunctionmembers:
Thedatamembersstorethestateassociatedwithobjectsoftheclasstype,andthefunctionsperformoperationsthatgivemeaningtothedata.Classesletusseparateimplementationandinterface.Theinterfacespecifiestheoperationsthattheclasssupports.Onlytheimplementoroftheclassneedknoworcareaboutthedetailsoftheimplementation.Thisseparationreducesthebookkeepingaspectsthatmakeprogrammingtediousanderror-prone.
Classtypesoftenarereferredtoasabstractdatatypes.Anabstractdatatypetreatsthedata(state)andoperationsonthatstateasasingleunit.Wecanthinkabstractlyaboutwhattheclassdoes,ratherthanalwayshavingtobeawareofhowtheclassoperates.Abstractdatatypesarefundamentaltobothobject-orientedandgenericprogramming.
Dataabstractionisaprogramming(anddesign)techniquethatreliesontheseparationofinterfaceandimplementation.Theclassdesignermustworryabouthowaclassisimplemented,butprogrammersthatusetheclassneednotknowaboutthesedetails.Instead,programmerswhouseatypeneedtoknowonlythetype'sinterface;theycanthinkabstractlyaboutwhatthetypedoesratherthanconcretelyabouthowthetypeworks.
Encapsulationisatermthatdescribesthetechniqueofcombininglower-levelelementstoformanew,higher-levelentity.Afunctionisoneformofencapsulation:
Thedetailedactionsperformedbythefunctionareencapsulatedinthelargerentitythatisthefunctionitself.Encapsulatedelementshidethedetailsoftheirimplementationwemaycallafunctionbuthavenoaccesstothestatementsthatitexecutes.Inthesameway,aclassisanencapsulatedentity:
Itrepresentsanaggregationofseveralmembers,andmost(well-designed)classtypeshidethemembersthatimplementthetype.
Ifwethinkaboutthelibraryvectortype,itisanexampleofbothdataabstractionandencapsulation.Itisabstractinthattouseit,wethinkaboutitsinterfaceabouttheoperationsthatitcanperform.Itisencapsulatedbecausewehavenoaccesstothedetailsofhowthetypeisrepresentatednortoanyofitsimplementationartifacts.Anarray,ontheotherhand,issimilarinconcepttoavectorbutisneitherabstractnorencapsulated.Wemanipulateanarraydirectlybyaccessingthememoryinwhichthearrayisstored.
Notalltypesneedtobeabstract.Thelibrarypairclassisagoodexampleofauseful,well-designedclassthatisconcreteratherthanabstract.Aconcreteclassisaclassthatexposes,ratherthanhides,itsimplementation.
Someclasses,suchaspair,reallyhavenoabstractinterface.Thepairtypeexiststobundletwodatamembersintoasingleobject.Thereisnoneedoradvantagetohidingthedatamembers.Hidingthemembersinaclasslikepairwouldonlycomplicatetheuseofthetype.
Evenso,suchtypesoftenhavememberfunctions.Inparticular,itisagoodideaforanyclassthathasdatamembersofbuilt-inorcompoundtypetodefineconstructor(s)toinitializethosemembers.Theuseroftheclasscouldinitializeorassigntothedatamembersbutitislesserror-pronefortheclasstodoso.
Programmerstendtothinkaboutthepeoplewhowillruntheirapplicationsas"users."Applicationsaredesignedforandevolveinresponsetofeedbackfromthosewhoultimately"use"theapplications.Classesarethoughtofinasimilarway:
Aclassdesignerdesignsandimplementsaclassfor"users"ofthatclass.Inthiscase,the"user"isaprogrammer,nottheultimateuseroftheapplication.
Authorsofsuccessfulapplicationsdoagoodjobofunderstandingandimplementingtheneedsoftheapplication'susers.Similarly,well-designed,usefulclassesaredesignedwithacloseattentiontotheneedsoftheusersoftheclass.
Inanotherway,thedivisionbetweenclassdesignerandclassuserreflectsthedivisionbetweenusersofanapplicationandthedesignersandimplementorsoftheapplication.Userscareonlyiftheapplicationmeetstheirneedsinacost-effectiveway.Similarly,usersofaclasscareonlyaboutitsinterface.Goodclassdesignersdefineaclassinterfacethatisintuitiveandeasytouse.Userscareabouttheimplementationonlyinsofarastheimplementationaffectstheiruseoftheclass.Iftheimplementationistoosloworputsburdensonusersoftheclass,thentheusersmustcare.Inwell-designedclasses,onlytheclassdesignerworriesabouttheimplementation.
Insimpleapplications,theuserofaclassandthedesigneroftheclassmightbeoneandthesameperson.Eveninsuchcases,itisusefultokeeptherolesdistinct.Whendesigningtheinterfacetoaclass,theclassdesignershouldthinkabouthoweasyitwillbetousetheclass.Whenusingtheclass,thedesignershouldn'tthinkabouthowtheclassworks.
Whenreferringtoa"user,"thecontextmakesitclearwhichkindofuserismeant.Ifwespeakof"usercode"orthe"user"oftheSales_itemclass,wemeanaprogrammerwhoisusingaclassinwritinganapplication.Ifwespeakofthe"user"ofthebookstoreapplication,wemeanthemanagerofthestorewhoisrunningtheapplication.
Dataabstractionandencapsulationprovidetwoimportantadvantages:
1.Classinternalsareprotectedfrominadvertentuser-levelerrors,whichmightcorruptthestateoftheobject.
2.Theclassimplementationmayevolveovertimeinresponsetochangingrequirementsorbugreportswithoutrequiringchangeinuser-levelcode.
Bydefiningdatamembersonlyintheprivatesectionoftheclass,theclassauthorisfreetomakechangesinthedata.Iftheimplementationchanges,onlytheclasscodeneedstobeexaminedtoseewhataffectthechangemayhave.Ifdataarepublic,thenanyfunctionthatdirectlyaccessesthedatamembersoftheoldrepresentationmightbebroken.Itwouldbenecessarytolocateandrewriteallthoseportionsofcodethatreliedontheoldpesentationbeforetheprogramcouldbeusedagain.
Similarly,iftheinternalstateoftheclassisprivate,thenchangestothememberdatacanhappeninonlyalimitednumbero