当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

川北医学院:《C++程序设计》课程教学资源(课件讲稿)第12章 多态性与虚函数 Polymorphism & Virtual Functions

资源类别:文库,文档格式:PDF,文档页数:60,文件大小:1.57MB,团购合买
12.1 多态性的概念 12.2 一个典型的例子 12.3 虚函数 12.4 纯虚函数与抽象类
点击下载完整版文档(PDF)

同名成员的二义性(ambiguous)问题 class N N类 { public: int a; void display A类 B类 cout长<“N:a=& <<a<<endl; 3 C类 017年4月26日星期 第11章继承与派生 1 HOME 12时14分39秒 BACK NEXT

HOME2017年4月26日星期 三12时14分39秒 第11章 继承与派生 1 class N {public: int a; void display() { cout<<“N::a=“ <<a<<endl; } };

同名成员的二义性(ambiguous)问题 (1)两个基类有同名成员 -用基类名来限定,如cl.A:display(0; (2)两个基类和派生类都有同名成员 同名覆盖,派生类新增的同名成员覆盖了基类中 的同名成员 用基类名来限定,如cl.A:display0; (3)两个基类从同一个基类派生 通过直接基类名来限定,如cl.A:display(0; c1.N:displayO; 2017年4月26日星期 第12章多态性与虚函数 0雪2时14分39秒 BACK NEXT

HOME2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 2 • (1) 两个基类有同名成员 – 用基类名来限定,如c1.A::display(); • (2) 两个基类和派生类都有同名成员 – 同名覆盖,派生类新增的同名成员覆盖了基类中 的同名成员 – 用基类名来限定,如c1.A::display(); • (3)两个基类从同一个基类派生 – 通过直接基类名来限定,如c1.A::display(); – c1.N::display();

基类与派生类的转换 1.派生类对象向基类对象赋值 2.派生类对象向基类对象的引用赋值或初始 化 3. 函数的参数是基类对象引用,相应的实参 可以用子类对象。 4.派生类对象的地址可以赋给指向基类对象 的指针变量 不论娜种方式都只能访问派生类中的基类成 员,而不能访问派生类增加的成员。 017年4月26日星期 HOME 第12章多态性与虚函数 12时14分39秒 NEX

HOME2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 3 • 1. 派生类对象向基类对象赋值 • 2. 派生类对象向基类对象的引用赋值或初始 化 • 3. 函数的参数是基类对象引用,相应的实参 可以用子类对象。 • 4. 派生类对象的地址可以赋给指向基类对象 的指针变量。 • 不论哪种方式都只能访问派生类中的基类成 员,而不能访问派生类增加的成员

例11.10 出9g2m> using namespace std; class Student /声明Student类 public: St知dent(int,string,loat);/声明构造函数 void display(); private: Int num: string name: float score; tudent::Student(intnstring am,float s) 般哭ie盘带core客7 coutdisplay)月 /调用sl.display函数 t=&grad1: ot->display(): 捕猪指部ay函数 017年4月26日星期 第12章多态性与虚函数 4 HOME 12时14分39秒 BACK NEXT

HOME #include #include using namespace std; class Student //声明Student类 {public: Student(int, string,float); //声明构造函数 void display( ); private: int num; string name; float score; };Student::Student(int n, string nam,float s) { num=n; name=nam; score=s;} void Student::display( ) { coutdisplay( ); //调用stud1.display函数 pt=&grad1; //指针指向grad1 pt->display( ); //调用grad1.display函数 } 2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 4

第12章 多态性与虚函数 Polymorphism & Virtual Functions 12.1 多态性的概念 12.2 一个典型的例子 12.3 虚函数 12.4纯虚函数与抽象类 2017年4月26日星期 第12章多态性与虚函数 5 HOMR 2时14分39秒 BACK NEXT

HOME2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 5 12.1 多态性的概念 12.2 一个典型的例子 12.3 虚函数 12.4 纯虚函数与抽象类

12.1多态性的概念 多态性(polymorphism)是面向对象程序设计的 一个重要特征。 当向不同的对象发送同一个消息时,不同的对 象在接收时会产生不同的行为。 通过继承产生的不同层次的派生类,与基类成 员同名的成员在不同的派生类中有不同的含 义。也可以说,多态性是“一个接口,多种 方法” 2017年4月26日星期 第12章多态性与虚函数 0雪2时14分39秒 BACK NEXT

HOME2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 6 多态性(polymorphism)是面向对象程序设计的 一个重要特征。 当向不同的对象发送同一个消息时,不同的对 象在接收时会产生不同的行为。 通过继承产生的不同层次的派生类,与基类成 员同名的成员在不同的派生类中有不同的含 义。也可以说,多态性是“一个接口,多种 方法”

动态多态性,又称运行时多态性,在程序运行 过程中才动态地确定操作所针对的对象,通 过虚函数(virtual function)实现。 通过继承而产生了相关的不同的派生类,与基 类成员同名的成员在不同的派生类中有不同 的含义。也可以说,多态性是“一个接口, 多种方法”。 2017年4月26日星期 第12章多态性与虚函数 H0雪2时14分39秒 BACK NEXT

HOME2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 7 动态多态性,又称运行时多态性,在程序运行 过程中才动态地确定操作所针对的对象,通 过虚函数(virtual function)实现。 通过继承而产生了相关的不同的派生类,与基 类成员同名的成员在不同的派生类中有不同 的含义。也可以说,多态性是“一个接口, 多种方法”

从系统实现的角度看,多态性分为两类: 静态多态性,又称编译时多态性,在程序编译 时系统就能决定调用的是哪个函数,如函数 重载和运算符重载。 动态多态性,又称运行时多态性,在程序运行 过程中才动态地确定操作所针对的对象,通 过虚函数(virtual function)实现。 017年4月26日星期 第12章多态性与虚函数 8 HOME 12时14分39秒 BACK NEXT

HOME2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 8 从系统实现的角度看,多态性分为两类: 静态多态性,又称编译时多态性,在程序编译 时系统就能决定调用的是哪个函数,如函数 重载和运算符重载。 动态多态性,又称运行时多态性,在程序运行 过程中才动态地确定操作所针对的对象,通 过虚函数(virtual function)实现

The concept of polymorphism Polymorphism (implemented in C++with virtual functions)is the third essential feature of an object oriented programming language, after data abstraction and inheritance. Polymorphism allows improved code organization and readability as well as the creation of extensible programs that can be “grown”not only during the original creation of the project,but also when new features are desired. 2017年4月26日星期 第12章多态性与虚函数 0画2时14分39秒 BACK

HOME2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 9 Polymorphism (implemented in C++ with virtual functions) is the third essential feature of an object oriented programming language, after data abstraction and inheritance. Polymorphism allows improved code organization and readability as well as the creation of extensible programs that can be “grown” not only during the original creation of the project, but also when new features are desired

The virtual function allows one type to express its distinction from another,similar type,as long as they're both derived from the same base type.This distinction is expressed through differences in behavior of the functions that you can call through the base class. 017年4月26日星期 第12章多态性与虚函数 HOME 10 12时14分39秒 BACK NEXT

HOME2017年4月26日星期 三12时14分39秒 第12章 多态性与虚函数 10 The virtual function allows one type to express its distinction from another, similar type, as long as they’re both derived from the same base type. This distinction is expressed through differences in behavior of the functions that you can call through the base class

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共60页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有