欢迎来到天天文库
浏览记录
ID:34816792
大小:233.22 KB
页数:63页
时间:2019-03-11
《chap15-polymorphism_et_virtual_func .pdf》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、15:Polymorphism&VirtualFunctionsPolymorphism(implementedinC++withvirtualfunctions)isthethirdessentialfeatureofanobject-orientedprogramminglanguage,afterdataabstractionandinheritance.659Itprovidesanotherdimensionofseparationofinterfacefromimplementation,todecouplewhatfromhow.
2、Polymorphismallowsimprovedcodeorganizationandreadabilityaswellasthecreationofextensibleprogramsthatcanbe“grown”notonlyduringtheoriginalcreationoftheproject,butalsowhennewfeaturesaredesired.Encapsulationcreatesnewdatatypesbycombiningcharacteristicsandbehaviors.Accesscontrolse
3、paratestheinterfacefromtheimplementationbymakingthedetailsprivate.Thiskindofmechanicalorganizationmakesreadysensetosomeonewithaproceduralprogrammingbackground.Butvirtualfunctionsdealwithdecouplingintermsoftypes.InChapter14,yousawhowinheritanceallowsthetreatmentofanobjectasit
4、sowntypeoritsbasetype.Thisabilityiscriticalbecauseitallowsmanytypes(derivedfromthesamebasetype)tobetreatedasiftheywereonetype,andasinglepieceofcodetoworkonallthosedifferenttypesequally.Thevirtualfunctionallowsonetypetoexpressitsdistinctionfromanother,similartype,aslongasthey
5、’rebothderivedfromthesamebasetype.Thisdistinctionisexpressedthroughdifferencesinbehaviorofthefunctionsthatyoucancallthroughthebaseclass.Inthischapter,you’lllearnaboutvirtualfunctions,startingfromthebasicswithsimpleexamplesthatstripawayeverythingbutthe“virtualness”oftheprogra
6、m.EvolutionofC++programmersCprogrammersseemtoacquireC++inthreesteps.First,assimplya“betterC,”becauseC++forcesyoutodeclareallfunctionsbeforeusingthemandismuchpickierabouthowvariablesareused.YoucanoftenfindtheerrorsinaCprogramsimplybycompilingitwithaC++compiler.660ThinkinginC+
7、+www.BruceEckel.comThesecondstepis“object-based”C++.Thismeansthatyoueasilyseethecodeorganizationbenefitsofgroupingadatastructuretogetherwiththefunctionsthatactuponit,thevalueofconstructorsanddestructors,andperhapssomesimpleinheritance.MostprogrammerswhohavebeenworkingwithCfo
8、rawhilequicklyseetheusefulnessofthisbecause,whenevertheycreatealibrary,this
此文档下载收益归作者所有