毕业论文5000字英文文献翻译文档格式.docx
《毕业论文5000字英文文献翻译文档格式.docx》由会员分享,可在线阅读,更多相关《毕业论文5000字英文文献翻译文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
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,<
<
toreadaSales_itemobject,
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;
theycanthinkabstractlyaboutwhatthetypedoesratherthanconcret