- 0
- 0
- 约6.9千字
- 约 23页
- 2021-11-30 发布于安徽
- 举报
* 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
您可能关注的文档
- 高等数学课件:9-7常系数线性微分方程组.ppt
- 高等数学课件:10.1向量及其运算(1-30).ppt
- 高等数学课件:10.2 空间直角坐标系与向量代数(1-24).ppt
- 高等数学课件:10.3 平面与直线.ppt
- 高等数学课件:10.4.1曲面及其方程.ppt
- 高等数学课件:10.4.2二次曲面.ppt
- 高等数学课件:10.5向量函数 空间曲线.ppt
- 高等数学课件:10-1 收敛原理与数项级数.ppt
- 高等数学课件:10-2 正项级数.ppt
- 高等数学课件:10-3任意项级数.ppt
- 四川水利职业技术学院《移动开发》2023-2024学年第二学期期末试卷.doc
- 吉林省安图县第三中学2024-2025学年八上数学期末调研模拟试题含解析.doc
- 2026届咸阳市永寿县四年级数学第一学期期末质量跟踪监视试题含解析.doc
- 白银矿冶职业技术学院《设计流程规范与管理》2023-2024学年第一学期期末试卷.doc
- 三亚城市职业学院《博弈论基础》2023-2024学年第一学期期末试卷.doc
- 甘肃省平凉市铁路中学2025届数学七年级第一学期期末检测试题含解析.doc
- 云南省玉溪市红塔区云2025年初三第二次调研英语试题含答案.doc
- 2025届浙江省杭州市采荷中学数学九年级第一学期期末预测试题含解析.doc
- 南京邮电大学《统计学》2023-2024学年第一学期期末试卷.doc
- 北京外国语大学附属中学2025年化学高二下期末教学质量检测模拟试题含解析.doc
原创力文档

文档评论(0)