![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Lecture note Data visualization - Chapter 14
Số trang: 26
Loại file: pptx
Dung lượng: 93.64 KB
Lượt xem: 4
Lượt tải: 0
Xem trước 3 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
This chapter presents the following content: Introduction to inheritance, inheritance in C++, IS-A relationship, polymorphism in inheritance, classes in inheritance, visibility rules, constructor and base class, adding members.
Nội dung trích xuất từ tài liệu:
Lecture note Data visualization - Chapter 14Lecture14RecapIntroductiontoInheritanceInheritanceinC++ISARelationshipPolymorphisminInheritanceClassesinInheritanceVisibilityRulesConstructorandBaseClassAddingMembersOverridingaMethodMethodscanbeoverridefromthebaseclassinthe derivedclassbysimplyprovidingaderivedclassmethod withthesamesignatureAderivedclassmethodmusthavethesameor compatiblereturntypeSometimesitisrequiredtoinvokethederivedclass methodtothebaseclassmethodtoaugmentabaseclass methodratherthandoingsomethingentirelydifferent. ThisisknownaspartialoverridingThescopeoperatorcanbeusedtocallabaseclass methodExampleclassWorkaholic:publicWorker{public:voiddowork(){Worker::doWork();//WorklikeaWorkerdrinkCoffee();//TakeabreakWorker::doWork();//WorklikeaWorkersomemoreStaticandDynamicBindingExampleCode 1Workerw; 2Workaholicwh; 3... 4w.doWork();wh.doWork();Theabovecodeillustratesthefactthatwecandeclare WorkerandWorkaholicobjectsinthesamescope becausethecompilercandeducewhichdoworkmethodto applywisaWorkerandwhisaWorkaholic,sotheExample1Worker*wptr;2cin>>x;3if(x!=0)4wptr=newWorkaholic();5else6wptr=newWorker();78……ExplanationofExampleIfxiszero,wecreateaWorkerobject;otherwise,we createaWorkaholicobjectRecallthat,asaWorkaholicISAWorker,aWorkaholic canbeaccessedbyapointertoaWorkerAnymethodthatwemightcallforWorkerwillhavea meaningforWorkaholicobjectsPublicinheritanceautomaticallydefinesatypeconversion fromapointertoaderivedclasstoapointertothebase classWecandeclarethatwptrisapointertothebaseclassContinued….Thedecisionaboutwhichdoworktousecanbemadeat compiletimeoratruntimeIfthedecisionismadeatcompiletime,workersdowork mustbeusedbecausethatisthetypeof*wptratcompile timeIfwptrisactuallypointingatWorkaholic,thisdecisionis wrong.Becausethetypeofobjectthatwptrisactually pointingatcanbedeterminedonlyastheprogramruns, thisdecisionmustbemadeatruntimeAruntimedecisiontoapplythemethodcorrespondingto theactualreferencedobjectiscalleddynamicbindingContinued….Howeveraruntimedecisionincurssomeruntime overheadbecauseitrequiresthattheprogrammaintain extrainformationandthatthecompilergeneratecodeto performthetestThisoverheadwasoncethoughttobesignificantAlthoughotherlanguagessuchasSmalltalkand ObjectiveCusedynamicbindingbydefault,C++does notVirtualFunctionAvirtualfunctionusesdynamicbindingifacompiletime bindingdecisionisimpossibletomakeAnonvirtualfunctionwillalwaysusestaticbindingThedefaultisthatfunctionsarenonvirtual.Thiscondition isunfortunatebecausewenowknowthattheoverheadis relativelyminorAsaresult,anonvirtualfunctionshouldbeusedonly whenthefunctionisinvariantovertheinheritance hierarchyContinued….Virtualnessisinherited,soitcanbeindicatedinthebase classThusifthebaseclassdeclaresthatafunctionisvirtualthe decisioncanbemadeatruntime;otherwise,itismadeat compiletimeForexample Intheexceptionclass,thewhatmethodisvirtual.The derivedclassesrequirenofurtheractiontohavedynamic bindingapplyforwhatmethodcallsContinued….InlastexamplecodeofWorkclass,doWorkiscalledifit isdeclaredasvirtualIfdoworkisnotvirtualintheWorkerclass,butislater madevirtualinWorkaholic,thenaccessesthrough pointersandreferencestoWorkerwillstillusestatic bindingTomakearuntimedecision,thekeywordvirtualhaveto placeatthestartofthedoworkdeclarationintheWorker classinterfaceas classWorkerSummarizingTopicStaticbindingisusedbydefaultDynamicbindingisusedforvirtualfunctionsifthe bindingcannotberesolvedatcompiletimeHowever,aruntimedecisionisneededonlywhenan objectisaccessedthroughapointerorreferencetoabase classTypesofMemberFunctionThreetypesofmemberfunction Nonvirtualfunction Virtualfunction PurevirtualfunctionNonvirtualFunctionOverloadingisresolvedatcompiletimeToensureconsistencywhenpointerstoobjectsareusedWegenerallyuseanonvirtualmethodonlywhenthe functionisinvariantovertheinheritancehierarchyConstructorsarealwaysnonvirtualVirtualFunctionOverloadingisresolvedatruntimeThebaseclassprovidesadefaultimplementationthat maybeoverriddenbythederivedclassesDestructorsshouldbevirtualPureVirtualFunctionOverloadingisresolvedatruntimeThebaseclassprovidesnoimplementationandisabstractTheabsenceofadefaultrequireseitherthatthederived classesprovideanimplementationorthatthederived classesthemselvesbeabstractAbstractMethodAnabstractmethodisdeclaredinthebaseclassItalwaysdefinedinthederivedclassItsays(inthebaseclass),whatallclassobjectsinthe hierarchycandoandmusteventuallyimplementItdoesnotprovideadefaultimplementation,soeach derivedclassmustprovideitsownimplementationAbstractClassAclassthathasatleastoneabstractmethodiscalledan abstractclassThebehaviorofanabstractclassisnotcompletely defined,that’swhyabstractclassescanneverbe instantiatedWhenaderivedclassfailstooverrideanabstractmethod withanimplementation,themethodremainsabstractin thederivedclass ...
Nội dung trích xuất từ tài liệu:
Lecture note Data visualization - Chapter 14Lecture14RecapIntroductiontoInheritanceInheritanceinC++ISARelationshipPolymorphisminInheritanceClassesinInheritanceVisibilityRulesConstructorandBaseClassAddingMembersOverridingaMethodMethodscanbeoverridefromthebaseclassinthe derivedclassbysimplyprovidingaderivedclassmethod withthesamesignatureAderivedclassmethodmusthavethesameor compatiblereturntypeSometimesitisrequiredtoinvokethederivedclass methodtothebaseclassmethodtoaugmentabaseclass methodratherthandoingsomethingentirelydifferent. ThisisknownaspartialoverridingThescopeoperatorcanbeusedtocallabaseclass methodExampleclassWorkaholic:publicWorker{public:voiddowork(){Worker::doWork();//WorklikeaWorkerdrinkCoffee();//TakeabreakWorker::doWork();//WorklikeaWorkersomemoreStaticandDynamicBindingExampleCode 1Workerw; 2Workaholicwh; 3... 4w.doWork();wh.doWork();Theabovecodeillustratesthefactthatwecandeclare WorkerandWorkaholicobjectsinthesamescope becausethecompilercandeducewhichdoworkmethodto applywisaWorkerandwhisaWorkaholic,sotheExample1Worker*wptr;2cin>>x;3if(x!=0)4wptr=newWorkaholic();5else6wptr=newWorker();78……ExplanationofExampleIfxiszero,wecreateaWorkerobject;otherwise,we createaWorkaholicobjectRecallthat,asaWorkaholicISAWorker,aWorkaholic canbeaccessedbyapointertoaWorkerAnymethodthatwemightcallforWorkerwillhavea meaningforWorkaholicobjectsPublicinheritanceautomaticallydefinesatypeconversion fromapointertoaderivedclasstoapointertothebase classWecandeclarethatwptrisapointertothebaseclassContinued….Thedecisionaboutwhichdoworktousecanbemadeat compiletimeoratruntimeIfthedecisionismadeatcompiletime,workersdowork mustbeusedbecausethatisthetypeof*wptratcompile timeIfwptrisactuallypointingatWorkaholic,thisdecisionis wrong.Becausethetypeofobjectthatwptrisactually pointingatcanbedeterminedonlyastheprogramruns, thisdecisionmustbemadeatruntimeAruntimedecisiontoapplythemethodcorrespondingto theactualreferencedobjectiscalleddynamicbindingContinued….Howeveraruntimedecisionincurssomeruntime overheadbecauseitrequiresthattheprogrammaintain extrainformationandthatthecompilergeneratecodeto performthetestThisoverheadwasoncethoughttobesignificantAlthoughotherlanguagessuchasSmalltalkand ObjectiveCusedynamicbindingbydefault,C++does notVirtualFunctionAvirtualfunctionusesdynamicbindingifacompiletime bindingdecisionisimpossibletomakeAnonvirtualfunctionwillalwaysusestaticbindingThedefaultisthatfunctionsarenonvirtual.Thiscondition isunfortunatebecausewenowknowthattheoverheadis relativelyminorAsaresult,anonvirtualfunctionshouldbeusedonly whenthefunctionisinvariantovertheinheritance hierarchyContinued….Virtualnessisinherited,soitcanbeindicatedinthebase classThusifthebaseclassdeclaresthatafunctionisvirtualthe decisioncanbemadeatruntime;otherwise,itismadeat compiletimeForexample Intheexceptionclass,thewhatmethodisvirtual.The derivedclassesrequirenofurtheractiontohavedynamic bindingapplyforwhatmethodcallsContinued….InlastexamplecodeofWorkclass,doWorkiscalledifit isdeclaredasvirtualIfdoworkisnotvirtualintheWorkerclass,butislater madevirtualinWorkaholic,thenaccessesthrough pointersandreferencestoWorkerwillstillusestatic bindingTomakearuntimedecision,thekeywordvirtualhaveto placeatthestartofthedoworkdeclarationintheWorker classinterfaceas classWorkerSummarizingTopicStaticbindingisusedbydefaultDynamicbindingisusedforvirtualfunctionsifthe bindingcannotberesolvedatcompiletimeHowever,aruntimedecisionisneededonlywhenan objectisaccessedthroughapointerorreferencetoabase classTypesofMemberFunctionThreetypesofmemberfunction Nonvirtualfunction Virtualfunction PurevirtualfunctionNonvirtualFunctionOverloadingisresolvedatcompiletimeToensureconsistencywhenpointerstoobjectsareusedWegenerallyuseanonvirtualmethodonlywhenthe functionisinvariantovertheinheritancehierarchyConstructorsarealwaysnonvirtualVirtualFunctionOverloadingisresolvedatruntimeThebaseclassprovidesadefaultimplementationthat maybeoverriddenbythederivedclassesDestructorsshouldbevirtualPureVirtualFunctionOverloadingisresolvedatruntimeThebaseclassprovidesnoimplementationandisabstractTheabsenceofadefaultrequireseitherthatthederived classesprovideanimplementationorthatthederived classesthemselvesbeabstractAbstractMethodAnabstractmethodisdeclaredinthebaseclassItalwaysdefinedinthederivedclassItsays(inthebaseclass),whatallclassobjectsinthe hierarchycandoandmusteventuallyimplementItdoesnotprovideadefaultimplementation,soeach derivedclassmustprovideitsownimplementationAbstractClassAclassthathasatleastoneabstractmethodiscalledan abstractclassThebehaviorofanabstractclassisnotcompletely defined,that’swhyabstractclassescanneverbe instantiatedWhenaderivedclassfailstooverrideanabstractmethod withanimplementation,themethodremainsabstractin thederivedclass ...
Tìm kiếm theo từ khóa liên quan:
Data visualization Lecture note Data visualization Data structures Data Visualization with C Data visualization with matlab Effective graphical displayTài liệu liên quan:
-
Trực quan hóa dữ liệu: Vai trò & thử thách
10 trang 37 0 0 -
Ebook Eloquent JavaScript - A modern introduction to programming: Part 1
199 trang 36 0 0 -
Ebook Management support systems: Part 2 - Dr. Kamlesh Lakhwani
136 trang 32 0 0 -
Lecture Introduction to computing systems (2/e): Chapter 19 - Yale N. Patt, Sanjay J. Patel
28 trang 31 0 0 -
Ebook Introduction to algorithms (3rd edition)
1313 trang 30 0 0 -
Lecture Data Structures: Lesson 38
71 trang 28 0 0 -
Lecture Data structures and algorithms: Chapter 1 - Introduction
41 trang 28 0 0 -
Ebook Introduction to algorithms (Second Edition)
429 trang 27 0 0 -
335 trang 26 0 0
-
Lecture Data Structures: Lesson 32
16 trang 25 0 0