毕业论文5000字英文文献翻译c++.docx

上传人:b****6 文档编号:16244157 上传时间:2023-07-12 格式:DOCX 页数:17 大小:28.32KB
下载 相关 举报
毕业论文5000字英文文献翻译c++.docx_第1页
第1页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第2页
第2页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第3页
第3页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第4页
第4页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第5页
第5页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第6页
第6页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第7页
第7页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第8页
第8页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第9页
第9页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第10页
第10页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第11页
第11页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第12页
第12页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第13页
第13页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第14页
第14页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第15页
第15页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第16页
第16页 / 共17页
毕业论文5000字英文文献翻译c++.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

毕业论文5000字英文文献翻译c++.docx

《毕业论文5000字英文文献翻译c++.docx》由会员分享,可在线阅读,更多相关《毕业论文5000字英文文献翻译c++.docx(17页珍藏版)》请在冰点文库上搜索。

毕业论文5000字英文文献翻译c++.docx

毕业论文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.Anabstractdatatypetreatsthedataandoperationsonthatstateasasingleunit.Wecanthinkabstractlyaboutwhattheclassdoes,ratherthanalwayshavingtobeawareofhowtheclassoperates.Abstractdatatypesarefundamentaltobothobject-orientedandgenericprogramming.

Dataabstractionisaprogrammingtechniquethatreliesontheseparationofinterfaceandimplementation.Theclassdesignermustworryabouthowaclassisimplemented,butprogrammersthatusetheclassneednotknowaboutthesedetails.Instead,programmerswhouseatypeneedtoknowonlythetype'sinterface;theycanthinkabstractlyaboutwhatthetypedoesratherthanconcretelyabouthowthetypeworks.

Encapsulationisatermthatdescribesthetechniqueofcombininglower-levelelementstoformanew,higher-levelentity.Afunctionisoneformofencapsulation:

Thedetailedactionsperformedbythefunctionareencapsulatedinthelargerentitythatisthefunctionitself.Encapsulatedelementshidethedetailsoftheirimplementationwemaycallafunctionbuthavenoaccesstothestatementsthatitexecutes.Inthesameway,aclassisanencapsulatedentity:

Itrepresentsanaggregationofseveralmembers,andmostclasstypeshidethemembersthatimplementthetype.

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-inorcompoundtypetodefineconstructortoinitializethosemembers.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

展开阅读全文
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2