软件设计(II)教学课件:Chapter9 Pointers, Virtual Functions and Polymorphism.pptVIP

  • 0
  • 0
  • 约6.9千字
  • 约 23页
  • 2021-11-30 发布于安徽
  • 举报

软件设计(II)教学课件:Chapter9 Pointers, Virtual Functions and Polymorphism.ppt

* Upcasting and Downcasting Upcasting Assigning a pointer of a derived class type to a pointer of its base class type Done implicitly Downcasting Assigning a pointer of a base class type to a pointer of its derived class type Done explicitly using dynamic_cast GeometricObject *p = new Circle(1); Circle *p1 = new Circle(2); p = p1; p1 = dynamic_castCircle*(p); * The typeid Operator To return the type information of a variable/object The information is stored in an object of class type_info For example, string x; cout typeid(x).name() endl; Summary Polymorphism Compile / Runtime polymorphism Object pointers this pointer Techniques to realize runtime polymorphism Virtual function Invoked via pointer of base class Abstract function * Review Questions Q9.2 Q9.4 Q9.8 * Chapter 9 Pointers, Virtual Functions and Polymorphism §9.1 Introduction §9.2 Pointers to Objects §9.3 Virtual Functions §9.4 Dynamic Casting §9.1 Introduction Polymorphism: “one name, multiple forms” Functions, data types Function binding Invocation statement ?? function body Compile time polymorphism Static (early) binding, static linking Function overloading Operator overloading Run time polymorphism Dynamic (late) binding, dynamic linking Virtual function + pointer of base class * Techniques for Polymorphism * Polymorphism Run time polymorphism Compile time polymorphism Function Overloading Operator overloading Virtual function Example Situation * class HM { public: void show(){ coutHuman\n; } }; class CN: public HM { public: void show(){ cout“Chinese\n; } }; class CT: public CN{ public: void show(){ cout“Cantonese\n; } }; int fun(HM h){h.show();} fun(HM()); fun(CN()); fun(CT()); Human Human Human Human Chinese Cantonese Dynamic binding Anonymous object! §9.2 Pointers to Objects Create objects at run time class item {}; item x; item *it_ptr = x; Referring member functions x.show(); it_ptr-show(); (*it_ptr).show(); Array of objects item * ptr = new item[10]; * * The

文档评论(0)

1亿VIP精品文档

相关文档