




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 9 Pointers, Virtual Functions and Polymorphism9.1 Introduction9.2 Pointers to Objects9.3 Virtual Functions9.4 Dynamic Casting9.1 IntroductionPolymorphism: “one name, multiple forms”lFunctions, data typesFunction bindinglInvocation statement function bodyCompile time polymorphismlStatic (earl
2、y) binding, static linkinglFunction overloadinglOperator overloadingRun time polymorphismlDynamic (late) binding, dynamic linkinglVirtual function + pointer of base class2Techniques for Polymorphism3PolymorphismRun timepolymorphismCompile timepolymorphismFunctionOverloadingOperatoroverloadingVirtual
3、 functionExample Situation4class HM public: void show() coutHumann; ;class CN: public HM public: void show() cout“Chinesen; ;class CT: public CNpublic: void show() coutshow();(*it_ptr).show();Array of objectsitem * ptr = new item10;56The “this” PointerA special built-in pointer that points to the cu
4、rrent objectUsed to cope with hidden data fields/ Construct a circle object Circle:Circle(double radius) this-radius = radius; (*this).radius = radius; / Set a new radius void Circle:setRadius(double radius) this-radius = (radius = 0) ? radius : 0; The “this” PointerPerson &Person:greater(Person
5、& x) if (x.ageage) return x; else return *this;max = a.greater(b);7the invoking object!the argument object!Pointers to Derived ClassesPointers of a base class type is compatible with pointers of a derived classStatements for a base class is workable for derived classes8B *cptr;/pointer to class
6、B type variableB b;/base objectD d;/derived objectcptr = &b;/cptr points to object bcptr-show();cptr = &d; /cptr points to object dcptr-show();class Bpublic: int b; void show() coutb=bendl;class D : public Bpublic: int d; void show() coutb=bendl; coutd=db = 100; coutshow(); D derived; /deriv
7、ed object bptr = &derived; bptr-b=200; coutshow(); D *dptr; dptr= &derived; dptr-d=300; coutshow(); coutusing (D*)bptr)d=400; (D*)bptr)-show(); return 0;9.3 Virtual Functions10The function declared with the keyword “virtual”OverridinglTo redefine a virtual function in the derived classclass
8、B public: virtual string toString() return class B; ; class D: public B string toString() return class D; ; class B public: void display() cout“Display base; virtual void show() cout“n show base“;class D: public B public : void display() coutDisplay derived; void show() cout“show derived;11int main(
9、) B b; D d; B *bptr; coutdisplay(); bptr-show(); coutdisplay(); bptr-show(); return 0;Example of Virtual FunctionsRealize dynamic bindinglMust be accessed by using object pointers/referencesMust be defined in base classShould be redefined (overriding) in the derived classlIf not, base version will b
10、e invokedNo virtual constructorslVirtual destructors are allowedCan be a friend of another class12Notes on Virtual FunctionsVirtual Destructors13class B public: B() cout“Dest Bn”; ; class D: public B public: D() cout“Dest Dn”; ; int main() B *bp = new D(); delete bp; Dest BDest DDest Bvirtual B()Pur
11、e Virtual FunctionsA function without bodyldoes nothing, just a placeholderAlso called abstract functionClasses has pure virtual function cant be used to create objectsCompiler requires each derived class:ldefine the function, orlcontinue to declare it as a abstract function14virtual void display()
12、= 0;Abstract Classes15Class is the abstraction of instances/objectsA base class is more abstract/general than derived classesAbstract class (抽象类)lIn logic: A class so abstract that it cannot have any specific instancesIt can only be used as base classl In syntax:A class with abstract functions (抽象函数
13、)16Abstract FunctionI.e. Pure Virtual Function (纯虚函数)lCant be implemented in abstract classesFor example GeometricObject -color: string -filled: bool #GeometricObject() #GeometricObject(color: string, filled: bool) +getColor(): string +setColor(color: string): void +isFilled(): bool +setFilled(fille
14、d: bool): void +tostring(): string +getArea(): double +getPerimeter(): double “ protected” class GeometricObjectprotected: GeometricObject(); GeometricObject(string color, bool filled);public: string getColor(); void setColor(string color); bool isFilled(); void setFilled(bool filled); string toStri
15、ng(); virtual double getArea() = 0; virtual double getPerimeter() = 0;private: string color; bool filled; 17Abstract Class Example GeometricObject -color: string -filled: bool #GeometricObject() #GeometricObject(color: string, filled: bool) +getColor(): string +setColor(color: string): void +isFille
16、d(): bool +setFilled(filled: bool): void +tostring(): string +getArea(): double +getPerimeter(): double Circle -radius: double +Circle() +Circle(radius: double) +Circle(radius: double, color: string, filled: bool) +getRadius(): double +setRadius(radius: double): void +getDiameter(): double Rectangle
17、 -width: double -height: double +Rectangle() +Rectangle(width: double, height: double) +Rectangle(width: double, height: double, color: string, filled: bool) +getWidth(): double +setWidth(width: double): void +getHeight(): double +setHeight(height: double): void AbstractGeometricObject.hRunAbstractG
18、eometricObject.cppDerivedCircle2.hDerivedCircle2.cppRectangle2.hRectangle2.cppTestGeometricObject2 .cpp189.4 Dynamic CastingHow to display radius, diameter, area, and perimeter if the object is a circle?/ A function for displaying a geometric objectvoid displayGeometricObject(GeometricObject &ob
19、ject) cout The area is object.getArea() endl; cout The perimeter is object.getPerimeter() endl;19Dynamic CastingThe dynamic_cast operator checks if p points to a Circle objectlIf yes, p1 is assigned the address of the objectlIf no, p1 is assigned to NULL (the constant 0)GeometricObject *p = &object;Circle *p1 = dynamic_cast(p);if (p1 != NULL) cout The radius is getRadius() endl; cout The diameter is getDiameter() endl;RunDynamicCastingDemo20Upcasting and DowncastingUpcastinglAssigning a pointer of a derived class type to a p
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 果洛环保塑胶跑道施工方案
- 白云区五下数学试卷
- 山东城市电梯灯施工方案
- 洋浦疏港高速公路工程SG01标段水稳拌合站环境影响报告表(公示稿)环评报告表
- 现场道路清理方案
- 弘景光电:盈利预测报告及审核报告
- 乌海市环氧自流平施工方案
- 山东省泰安市2025届高三一轮检测(泰安一模)日语参考答案
- 智能制造对劳动市场的影响
- 危化企业安全生产监控与检查方案
- 2024解析:第八章牛顿第一定律、二力平衡-讲核心(解析版)
- 《劳动法与劳动关系》课件
- 2025陕西延长石油(集团)有限责任公司招聘(1881人)笔试备考题库及答案解析
- 无人机航拍技术教案(完整版)
- 打架案例分析
- 2024脑血管病指南
- GB/T 25229-2024粮油储藏粮仓气密性要求
- 计算机网络基础与应用中职完整全套教学课件
- 《大气细颗粒物及其主要组分致肺衰老与纤维化的分子机制研究》
- 数字经济学-课件 第1、2章 数字经济学基础、数据要素
- 《保密法》培训课件
评论
0/150
提交评论