毕业论文5000字英文文献翻译c++.docx
- 文档编号:16244157
- 上传时间:2023-07-12
- 格式:DOCX
- 页数:17
- 大小:28.32KB
毕业论文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 Dataabstractionisaprogramming Encapsulationisatermthatdescribesthetechniqueofcombininglower-levelelementstoformanew,higher-levelentity.Afunctionisoneformofencapsulation: Thedetailedactionsperformedbythefunctionareencapsulatedinthelargerentitythatisthefunctionitself.Encapsulatedelementshidethedetailsoftheirimplementationwemaycallafunctionbuthavenoaccesstothestatementsthatitexecutes.Inthesameway,aclassisanencapsulatedentity: Itrepresentsanaggregationofseveralmembers,andmost 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 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,thenchangestothememberdatacanhappeninonlyalimitednumberotoinitializethosemembers.Theuseroftheclasscouldinitializeorassigntothedatamembersbutitislesserror-pronefortheclasstodoso.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 毕业论文 5000 英文 文献 翻译 c+