欢迎来到天天文库
浏览记录
ID:48761676
大小:190.50 KB
页数:66页
时间:2020-01-22
《继承是面向对象语言的重要特征之一继承是把已有类作为.ppt》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、CHAPTER10Inheritance继承IntroductionAkeyfeatureofanobject-orientedlanguageininheritance.Inheritanceistheabilitytodefinenewclassesusingexistingclassesasabasis.Thenewclassinheritstheattributesandbehaviorsoftheclassesonwhichitisbased,anditcanalsohaveattributesandb
2、ehaviorsthatarespecifictoit.继承是面向对象语言的重要特征之一。继承是把已有类作为基类定义新的类。这个新的类继承了基类的属性和操作,而且还可以具有自己特定的属性和操作。Threerelationshipsarecommonlyusedinobject-orienteddesign:is-arelationshipsHierarchyrelationship.Itspecifiesthatoneabstractionisaspecializationofanother.has-arelat
3、ionshipsContainrelationship.Someobjectispartofanother.uses-arelationshipsOneobjectusesanotherobjectinsomeway.10.1OBJECT-ORIENTEDDESIGNUSINGINHERITANCEis-arelationshipsAB1B2C1C2C3BaseclassofB1andB2DerivedclassofABaseclassofC1,C2andC3DerivedclassofABaseclassof
4、C3DerivedclassofB1andB2MultipleinheritanceDerivedclassofB1has-arelationshipsclassA{…BDMi…FM};classB{…DM…FM};uses-arelationshipsclassA{…DM…FMj(B&…)};classB{…DM…FM};classDerivedClass:publicBaseClass{public://publicsection…private://privatesection…};10.3.1Declar
5、ingaderivedclassclassDerivedClass:publicBaseClass{public://publicsection…private://privatesection…};10.3.1DeclaringaderivedclassDerivedclassnameclassDerivedClass:publicBaseClass{public://publicsection…private://privatesection…};10.3.1DeclaringaderivedclassAcc
6、essspecifier(usuallypublic)classDerivedClass:publicBaseClass{public://publicsection…private://privatesection…};10.3.1DeclaringaderivedclassClassnameofbaseclassDeclarevehicleclassandderivedclasscar:classvehicle{public:voidinitialize(intin_wheels,floatin_weight
7、);intget_wheels();floatget_weight();floatget_loading();private:intwheels;floatweight;floatloading;};classcar:publicvehicle{public:voidinitialize(intin_wheels,floatin_weight,intpeople=4);intpassengers();private:intpassenger_load;};ExampleThesyntaxfortheconstru
8、ctorofderivedclassis10.3.2ImplementingaderivedclassDClass::DClass(Plist):BClass(Plist),DMbrList{//Bodyofderivedclassconstructor…}Thesyntaxfortheconstructorofderivedclassis10.3.2Implementi
此文档下载收益归作者所有