欢迎来到天天文库
浏览记录
ID:42323860
大小:317.51 KB
页数:66页
时间:2019-09-12
《继承是面向对象语言的重要特征之一继承是把已有类作为》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、CHAPTER10Inheritance继承IntroductionAkeyfeatureofanobject-orientedlanguageininheritance.Inheritanceistheabilitytodefinenewclassesusingexistingclassesasabasis.Thenewclassinheritstheattributesandbehaviorsoftheclassesonwhichitisbased,anditcanalsohaveattributesandbehaviorsthatarespeci
2、fictoit.继承是面向对象语言的重要特征之一。继承是把已有类作为基类定义新的类。这个新的类继承了基类的属性和操作,而且还可以具有自己特定的属性和操作。Threerelationshipsarecommonlyusedinobject-orienteddesign:is-arelationshipsHierarchyrelationship.Itspecifiesthatoneabstractionisaspecializationofanother.has-arelationshipsContainrelationship.Someobjectis
3、partofanother.uses-arelationshipsOneobjectusesanotherobjectinsomeway.10.1OBJECT-ORIENTEDDESIGNUSINGINHERITANCEis-arelationshipsAB1B2C1C2C3BaseclassofB1andB2DerivedclassofABaseclassofC1,C2andC3DerivedclassofABaseclassofC3DerivedclassofB1andB2MultipleinheritanceDerivedclassofB1ha
4、s-arelationshipsclassA{…BDMi…FM};classB{…DM…FM};uses-arelationshipsclassA{…DM…FMj(B&…)};classB{…DM…FM};classDerivedClass:publicBaseClass{public://publicsection…private://privatesection…};10.3.1DeclaringaderivedclassclassDerivedClass:publicBaseClass{public://publicsection…private
5、://privatesection…};10.3.1DeclaringaderivedclassDerivedclassnameclassDerivedClass:publicBaseClass{public://publicsection…private://privatesection…};10.3.1DeclaringaderivedclassAccessspecifier(usuallypublic)classDerivedClass:publicBaseClass{public://publicsection…private://privat
6、esection…};10.3.1DeclaringaderivedclassClassnameofbaseclassDeclarevehicleclassandderivedclasscar:classvehicle{public:voidinitialize(intin_wheels,floatin_weight);intget_wheels();floatget_weight();floatget_loading();private:intwheels;floatweight;floatloading;};classcar:publicvehic
7、le{public:voidinitialize(intin_wheels,floatin_weight,intpeople=4);intpassengers();private:intpassenger_load;};ExampleThesyntaxfortheconstructorofderivedclassis10.3.2ImplementingaderivedclassDClass::DClass(Plist):BClass(Plist),DMbrList{//Bodyofderivedclassconstructor…}Thesyntaxfo
8、rtheconstructorofderivedclassis10.3.2Implementi
此文档下载收益归作者所有