Chapter 12 Object-Oriented Programming Inheritance 0 2018 SEU. All rights reserved. 1
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 1 Object-Oriented Programming: Inheritance Chapter 12
OBJECTIVES o To create classes by inheriting from existing classes o How inheritance promotes software reuse The notions of base classes and derived classes and the relationships between them o The protected member access specifier. o The use of constructors and destructors in inheritance hierarchies o The differences between public, protected and pri ivate inheritance The use of inheritance to customize existing g software 0 2018, SEU. All rights reserved. 2
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 2 OBJECTIVES To create classes by inheriting from existing classes. How inheritance promotes software reuse. The notions of base classes and derived classes and the relationships between them. The protected member access specifier. The use of constructors and destructors in inheritance hierarchies. The differences between public, protected and private inheritance. The use of inheritance to customize existing software
Topics 12.1 Introduction 122 Base classes(基类) and derived classes 派生类) 12.3 protected Members o 12.4 Relationship between Base classes and Derived classes o 12.5 Constructors and destructors in Derived Classes 12.6 public, protected and private Inheritance o 12.7 Software Engineering with Inheritance 0 2018, SEU. All rights reserved. 3
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 3 Topics 12.1 Introduction 12.2 Base Classes(基类) and Derived Classes (派生类) 12.3 protected Members 12.4 Relationship between Base Classes and Derived Classes 12.5 Constructors and Destructors in Derived Classes 12.6 public, protected and private Inheritance 12.7 Software Engineering with Inheritance
121 Introduction-继承的概念 °软件重用 °在已存在的类基础上构建新的类 吸纳已有类的数据及行为(数据成员及成员函数) °可以强化扩展一些新的功能 Graduate Student 重写? Student String Name String Name int Age int Age Void Exam 重用? Void Exam Void Research U 2UI0, SEU. All iGrs reserved
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 4 12.1 Introduction --继承的概念 软件重用 在已存在的类基础上构建新的类 吸纳已有类的数据及行为(数据成员及成员函数) 可以强化扩展一些新的功能 Student String Name int Age ……. Void Exam() Graduate Student String Name int Age ……. Void Exam() Void Research() 重写? 重用?
12.1 Introduction-类之间的关系 Shape(基类 public ls-a(继承 Inheritance protected Has-a(组合) (继承关系) private crce(派生类 声明格式: class: …} class Circle: public Shape....]; 图形 闭合图形 开放图形 多边形 椭圆形(折线)(直线(弧线 O 2018, SEt. All rights reserved. 5
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 5 12.1 Introduction --类之间的关系 派生类 Inheritance (继承关系) public protected private 基 类 图 形 闭合图形 开放图形 多边形 椭圆形 折线 直线 弧线 Is-a(继承) Has-a(组合) Shape Circle 声明格式:class : {……}; class Circle: public Shape{……};
is-a和has-a的区别? ois-a代表继承 个类继承自另一个类 °属于更加专有的一类对象 可以一个类继承一些行为,也可修改甚至创建新的行为 例如 vehicle类,有加速、减速等行为,car继承 vehicle, 同样有这些行为,也可以有新的行为(如打开后备箱等) ohas-a代表组合关系 个对象包含其它的成员对象 °例如 Employee类中包含 firstname、 lastname、 birthdate、 hiredate等对象 0 2018. SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 is-a和has-a的区别? is-a代表继承 一个类继承自另一个类 属于更加专有的一类对象 可以一个类继承一些行为,也可修改甚至创建新的行为 例如vehicle类,有加速、减速等行为,car继承vehicle, 同样有这些行为,也可以有新的行为(如打开后备箱等) has-a代表组合关系 一个对象包含其它的成员对象 例 如 Employee 类中包含 firstname 、 lastname 、 birthdate、hiredate等对象
Topics o 12.1 Introduction 122 Base classes(基类) and derived classes (派生类) 12.3 protected Members o 12.4 Relationship between Base classes and Derived classes o 12.5 Constructors and destructors in Derived Classes 12.6 public, protected and private Inheritance o 12.7 Software Engineering with Inheritance 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 Topics 12.1 Introduction 12.2 Base Classes(基类) and Derived Classes (派生类) 12.3 protected Members 12.4 Relationship between Base Classes and Derived Classes 12.5 Constructors and Destructors in Derived Classes 12.6 public, protected and private Inheritance 12.7 Software Engineering with Inheritance
12.2 Base Classes and derived Classes Base class(基类):被继承的类。 Derived class(派生类):继承后得到的类 Base class Derived classes student GraduateStudent, Undergraduatestudent shape Circle, Triangle, Rectangle, sphere, Cube Loan CarLoan, HomeImprovementLoan, MortgageLoan Employee Faculty Staff Account CheckingAccount, SavingsAccount Class graduateStudent: public Student(……} ●。● 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 12.2 Base Classes and Derived Classes Base class(基类):被继承的类。 Derived class(派生类):继承后得到的类。 Base class Derived classes Student GraduateStudent, UndergraduateStudent Shape Circle, Triangle, Rectangle, Sphere, Cube Loan CarLoan, HomeImprovementLoan, MortgageLoan Employee Faculty, Staff Account CheckingAccount, SavingsAccount Class GraduateStudent: public Student{……}; ……
12.2 Base Classes and derived asses °继承的层次关系(大学社区成员关系图示例) Com munityMem ber Employee Student Alumnus Single inheritance Single Faculty Staff inheritance Administrator Teacher inheritance Administrator Tea cher Multiple inheritance 继承机制作用:软件复用;对事物进行分类; 支持软件的增量开发;对概念进行组合。 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 12.2 Base Classes and Derived Classes 继承的层次关系(大学社区成员关系图示例) 继承机制作用: 软件复用;对事物进行分类; 支持软件的增量开发;对概念进行组合
12.2 Base Classes and derived asses 定义派生类时需指出继承方式,如未显式指出,默认为 private °单继承: class TwoDimensionalShape: public Shape Two Dim ensionalShape Three Dimensional Shape Circle Square Triangle Sphere Cube Tetrahedron 多继承: class administratorTeacher: public Teacher, public Administrator 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 12.2 Base Classes and Derived Classes 定义派生类时需指出继承方式,如未显式指出,默认为private 单继承:class TwoDimensionalShape : public Shape 多继承:class AdministratorTeacher: public Teacher, public Administrator