面向对象程序设计英文教学课件:11_Polymorphism

上传人:hs****ma 文档编号:569812691 上传时间:2024-07-31 格式:PPT 页数:29 大小:669.50KB
返回 下载 相关 举报
面向对象程序设计英文教学课件:11_Polymorphism_第1页
第1页 / 共29页
面向对象程序设计英文教学课件:11_Polymorphism_第2页
第2页 / 共29页
面向对象程序设计英文教学课件:11_Polymorphism_第3页
第3页 / 共29页
面向对象程序设计英文教学课件:11_Polymorphism_第4页
第4页 / 共29页
面向对象程序设计英文教学课件:11_Polymorphism_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《面向对象程序设计英文教学课件:11_Polymorphism》由会员分享,可在线阅读,更多相关《面向对象程序设计英文教学课件:11_Polymorphism(29页珍藏版)》请在金锄头文库上搜索。

1、Object-Oriented Programming & C+Object-Oriented Programming & C+College of Computer Science, CQU12 PolymorphismObject-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 Polymorphism1OutlinepMultiple InheritancepPolymorphism pThe Slicing ProblempUse Virtual FunctionsObject-Ori

2、ented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 Polymorphism2Knowledge PointspUsing inheritance and dynamic bindingBook: Accelerated C+, Chapter 13Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 Polymorphism3Reference MaterialspObject-Oriente

3、d Programming: InheritanceBook: C+ How to Program (8th edition), Chapter 12 (30 pages)pObject-Oriented Programming: Polymorphism Book: C+ How to Program (8th edition), Chapter 13 (40 pages)Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismMultiple Inherita

4、ncepMultiple inheritance occurs when a class inherits from two or more base classes, like this:class Base1 ;class Base2 ;class Derv : public Base1, public Base2 ;Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismAmbiguity ResolutionpIf there is a function

5、with the same name in both base classes, then in a derived class, the scope resolution operator must be used to specify which function is meantObject-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismAmbiguity Resolutionclass A1f();class A2f();class B : public A1

6、, public A26int main() B b; b.f(); / error b.A1:f(); b.A2:f(); return 0;Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismAmbiguity ResolutionpRepeated Base ClassesnThis forms a diamond-shaped pattern.nA Child object will also contain two Gparent sub-objec

7、ts, one inherited via Mother and one inherited via Fatherclass Gparent protected: string name;class Mother : public Gparent ;class Father : public Gparent ;class Child : public Mother, public Father ;Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismAmbigu

8、ity ResolutionpRepeated Base ClassesnSolve the problem by virtual base classes nThe virtual keyword tells the compiler to inherit only one sub-object from a class into subsequent derived classes.class Gparent protected: string name;class Mother : virtual public Gparent ;class Father : virtual public

9、 Gparent ;class Child : public Mother, public Father ;Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismVirtual Base Classes #include using namespace std;class B0/ declare base class B0 public:B0(int n)coutinit B0.endl;class B1: virtual public B0public:B1(

10、int a) : B0(a) coutinit B1.endl;class B2: virtual public B0 public:B2(int a) : B0(a) coutinit B2.endl;class D1: public B1, public B2public:D1(int a) : B0(a), B1(a), B2(a)coutinit D1.endl;int main()D1 d1(1);return 0;OutputOutput:init B0.init B0.init B1.init B1.init B2.init B2.init D1.init D1.Object-O

11、riented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismPolymorphismpPolymorphism refers to the ability to associate multiple meanings with one function name using a mechanism called late bindingpPolymorphism is a key component of the philosophy of object oriented progr

12、ammingObject-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismExample(non Polymorphism)11Code example_1Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismDiscusspHow to add a Line object to the Graphics?pHow to change t

13、he drawing order of the Objects on the Graphics?pOther problems of the example12Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismExample(Polymorphism)13Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismDiscusspC

14、ode example_2pHow to add a Line object to the Graphics?pHow to change the drawing order of the Objects on the Graphics?pIf Shape:draw() is a non virtual function, what will happen?(example_3)pIf the type of Graphics:shapes is vector and Graphics:add(Shape* shape) is changed to Graphics:add(Shape sha

15、pe) , what will happen?(example_4)14Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismOverride vs redefinepVirtual functions whose definitions are changed in a derived class are said to be overriddenpNon-virtual functions whose definitions are changed in a

16、 derived class are redefinedObject-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismPolymorphismpFunctions in base class which are overridden in subclass must be declared as virtual function;nA virtual function required to be implemented in derived class is name

17、d pure virtual function, for example:Class Shape public: virtual void draw()=0;nA class containing pure virtual function is an abstract class.pCircle is a subclass of Shape and draw is a virtual function, then:16Shape* shape=new Circle();shape-draw(); /call Circle:draw()Circle c;Shape shape=c;shape.

18、draw(); /call Shape:draw()PolymorphismSlice ProblemObject-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismThe Slicing ProblempIt is legal to assign a derived class object into a base class variablenThis slices off data in the derived class that is not also part

19、 of the base classnMember functions and member variables are lostObject-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismExamplepConsiderclass Pet public:virtual void print();string name;and class Dog :public Pet public: virtual void print();string breed;Object-

20、Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismA Sliced Dog is a PetpC+ allows the following assignments:Dog vdog; vpet vpet; vdog.name = Tiny; vdog.breed= “Tibetan Mastiff;vpet= vdog;pHowever, vpet will loose the breed member of vdog since an object of class

21、Pet has no breed membernThis code would be illegal: coutprint( ); is legal and produces: name: Tiny breed: Great DanePet *ppet;Dog *pdog;pdog = new Dog;pdog-name = Tiny;pdog-breed = GreatDane;ppet = pdog;void Dog:print( ) cout name: name endl; cout breed: “ breed print( ); worked because print was d

22、eclared as a virtual functionpThis code would still produce an error:cout name: name breed: breed;Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismWhy?pppet-breed is still illegal because ppet is a pointer to a Pet object that has no breed memberpFunction

23、 print( ) was declared virtual by class Pet nWhen the computer sees ppet-print( ), it checks the virtual table for classes Pet and Dog and finds that ppet points to an object of type DogpBecause ppet points to a Dog object, code for Dog:print( )is used Object-Oriented Programming & C+Object-Oriented

24、 Programming & C+12 Polymorphism12 PolymorphismRemember Two RulespTo help make sense of object oriented programming with dynamic variables, remember these rules nIf the domain type of the pointer pParent is a base class for the domain type of pointer pChild, the following assignment of pointers is a

25、llowedpParent= pChild;and no data members will be lostnAlthough all the fields of the pChild are there, virtual functions are required to access themObject-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismVirtual DestructorspDestructors should be made virtual?nC

26、onsiderBase *pBase= new Derived;delete pBase;nIf the destructor in Base is virtual, the destructor for Derived is invoked as pBase points to a Derived object, returning Derived members to the free storepThe Derived destructor in turn calls the Base destructorObject-Oriented Programming & C+Object-Or

27、iented Programming & C+12 Polymorphism12 PolymorphismNon-Virtual DestructorspIf the Base destructor is not virtual, only the Base destructor is invokedpThis leaves Derived members, not part of Base, in memoryObject-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 Polymorphi

28、smstatic matching vs. dynamic bindingMatching a function signature and binding a function implementation are two separate issues. (Static Matching) The declared type of the variable decides which function to match at compile time. The compiler finds a matching function according to parameter type, n

29、umber of parameters, and order of the parameters at compile time. (Dynamic Binding) A virtual function may be implemented in several derived classes. C+ dynamically binds the implementation of the function at runtime, decided by the actual class of the object referenced by the variable.Object-Oriented Programming & C+Object-Oriented Programming & C+12 Polymorphism12 PolymorphismHomeworkpBuild the following program.

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号