Chapter 13 Object-Oriented Programming Polymorphism 0 2018, SEU. All rights reserved. 1
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 1 Object-Oriented Programming: Polymorphism Chapter 13
OBJECTIVES What polymorphism(多态)i, how it makes programming more convenient, and how it makes systems more extensible and maintainable ° To declare and use virtual functions(虚函数)to effect polymorphism The distinction between abstract and concrete classes(抽象类和具体类 ° To declare pure virtual functions(纯虚函数)to create abstract classes 0 2018, SEU. All rights reserved. 2
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 2 OBJECTIVES What polymorphism(多态) is, how it makes programming more convenient, and how it makes systems more extensible and maintainable. To declare and use virtual functions(虚函数) to effect polymorphism. The distinction between abstract and concrete classes(抽象类和具体类). To declare pure virtual functions(纯虚函数) to create abstract classes
Topics 13.1 Introduction o 13. 2 Relationships Among Objects in an Inheritance hierarchy 13.3 Type Fields and switch Statements 13. 4 Abstract Classes and pure virtual functions o 13.5 Case Study: Payroll System Using Polymorphism 0 2018, SEU. All rights reserved. 3
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 3 Topics 13.1 Introduction 13.2 Relationships Among Objects in an Inheritance Hierarchy 13.3 Type Fields and switch Statements 13.4 Abstract Classes and Pure virtual Functions 13.5 Case Study: Payroll System Using Polymorphism
13.1 Introduction -通用化编程需求 画板 Tool 基类 Shape:长方形*2、椭圆形*3、三角形*4、菱形*5等 用 vector或者arry来保存指针 °基类指针vs派生类指针 当需更新画板时,枚举指针并调用各自draw函数 当需计算面积时,枚举指针并调用各自area函数 0 2018, SEU. All rights reserved. 4
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 4 13.1 Introduction ---通用化编程需求 基类Shape:长方形*2、椭圆形*3、三角形*4、菱形*5等 用vector或者array来保存指针 基类指针 vs 派生类指针 当需更新画板时,枚举指针并调用各自draw函数 当需计算面积时,枚举指针并调用各自area函数 画板 Tool
13.1 Introduction 需 求 °用户通过键盘输入多个员工信息,统计收入数据 (1)CommissionEmployee name, ssn, grosssales, commisionRate (2)BasePlus Commission Employee name, ssn, grossSales, commisionRate, base Salary °用 vector或者aray来保存指向员工对象的指针 o Commission Employee pointer 希望通过这些通用指针来调用各自的 earnings0函 数以进行统计 o 2018, SEU. All rights reserved 5
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 5 用户通过键盘输入多个员工信息,统计收入数据: (1)CommissionEmployee name, ssn, grossSales, commisionRate (2)BasePlusCommissionEmployee name, ssn, grossSales, commisionRate, baseSalary 用vector或者array来保存指向员工对象的指针 CommissionEmployee Pointer 希望通过这些通用指针来调用各自的earnings()函 数以进行统计 13.1 Introduction --- 需 求
13.1 Introduction 面向对象 °数据封装 °继承 多态 0 2018, SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 面向对象 数据封装 继承 多态 13.1 Introduction
13.1 Introduction 多态: 解释1:同样的消息被类的不同对象接收时导致的完全 不同的行为的一种现象。这里所说的消息即对类的成 员函数的调用。 解释2:通过指向派生类的基类指针,调用派生类的函 数;将不同的派生类对象都当作基类来处理,并根据对 象不同产生不同的行为,以屏蔽各派生类对象之间的 差异。写出通用的代码,使得程序员可以方便地处理 普遍性问题。 C++语言支持两种类型的多态: ①编译时的多态(静态多态)——函数重载 ②运行时的多态(动态多态)——虚函数 多态性提高了软件的可扩展性,使得可以用与接 收消息对象类型无关的方式编写 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 13.1 Introduction 多态: 解释1:同样的消息被类的不同对象接收时导致的完全 不同的行为的一种现象。这里所说的消息即对类的成 员函数的调用。 解释2:通过指向派生类的基类指针,调用派生类的函 数; 将不同的派生类对象都当作基类来处理,并根据对 象不同产生不同的行为,以屏蔽各派生类对象之间的 差异。写出通用的代码,使得程序员可以方便地处理 普遍性问题。 C++语言支持两种类型的多态: ① 编译时的多态(静态多态) ——函数重载 ② 运行时的多态(动态多态) ——虚函数 多态性提高了软件的可扩展性,使得可以用与接 收消息对象类型无关的方式编写
Topics 13.1 Introduction o 13.2 Relationships Among Objects in an Inheritance Hierarch 13.3 Type Fields and switch Statements 13. 4 Abstract Classes and pure virtual functions o 13.5 Case Study: Payroll System Using Polymorphism 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 Topics 13.1 Introduction 13.2 Relationships Among Objects in an Inheritance Hierarchy 13.3 Type Fields and switch Statements 13.4 Abstract Classes and Pure virtual Functions 13.5 Case Study: Payroll System Using Polymorphism
13.2 Relationships Among Objects in an Inheritance hierarch 13.2.1 Invoking Base-Class Functions from Derived Class objects(基类指针指向派生类,调用基类函数) o 13.2.2 Aiming Derived-Class Pointers at Base-Class Objects(派生类指针指向基类,错误) o 13.23 Derived-Class Member-Function Calls via Base Class pointers(基类指针指向派生类,调用派生类函数, 错误) °13.24 Virtua| Functions(应用虚函数,解决上述问题) 13.2.5 Summary of the allowed Assignments Between Base-Class and derived -class objects and pointers (基类/派生类对象和指针之间的赋值) 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 13.2 Relationships Among Objects in an Inheritance Hierarchy 13.2.1 Invoking Base-Class Functions from DerivedClass Objects(基类指针指向派生类,调用基类函数) 13.2.2 Aiming Derived-Class Pointers at Base-Class Objects(派生类指针指向基类,错误) 13.2.3 Derived-Class Member-Function Calls via BaseClass Pointers(基类指针指向派生类,调用派生类函数, 错误) 13.2.4 Virtual Functions(应用虚函数,解决上述问题) 13.2.5 Summary of the Allowed Assignments Between Base-Class and Derived-Class Objects and Pointers (基类/派生类对象和指针之间的赋值)
13.2.1 Invoking Base-Class Functions from Derived-Class Objects °基类 Commission Employee void printO const °派生类 Base Plus Commission Employee void print( const commissionEmployeePtr &basePlus CommissionEmployee 注意:基类指针调用函数,输出什么 程序 P13.1-5 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 13.2.1 Invoking Base-Class Functions from Derived-Class Objects 基类CommissionEmployee void print() const; 派生类BasePlusCommissionEmployee void print() const; commissionEmployeePtr = &basePlusCommissionEmployee; 注意:基类指针调用函数,输出什么