1 / 27

C++ Programming Lecture 17

C++ Programming Lecture 17. Wei Liu ( 刘威 ) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology Feb. 2014. Lecture 17. Chapter 20. Object-Oriented Programming: Inheritance 20.1 Introduction 20.2 Base Classes and Derived Classes 20.3 protected Members

rhonda
Télécharger la présentation

C++ Programming Lecture 17

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ ProgrammingLecture 17 Wei Liu (刘威) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology Feb. 2014

  2. Lecture 17 • Chapter 20. Object-Oriented Programming: Inheritance • 20.1 Introduction • 20.2 Base Classes and Derived Classes • 20.3 protected Members • 20.4 Relationship between Base Classes and Derived Classes • 20.5 Constructors and Destructors in Derived Classes • 20.6 public, protected and private Inheritance • 20.7 Software Engineering with Inheritance

  3. 20.1 Introduction • Inheritance is a form of software reuse in which you create a class that absorbs an existing class’s data and behaviors and enhances them with new capabilities. • You can designate that the new class should inherit the members of an existing class. • 继承是一种软件重用的方式。如果现有类可以描述事物的现有数据和行为,通过继承来获得和增强类的能力 • This existing class is called the base class, and the new class is referred to as the derived class. • 现有的类被称为基类,从基类继承出来的类被称为派生类

  4. Lecture 17 • Chapter 20. Object-Oriented Programming: Inheritance • 20.1 Introduction • 20.2 Base Classes and Derived Classes • 20.3 protected Members • 20.4 Relationship between Base Classes and Derived Classes • 20.5 Constructors and Destructors in Derived Classes • 20.6 public, protected and private Inheritance • 20.7 Software Engineering with Inheritance

  5. 20.2 Base Classes and Derived Classes • 现实世界的继承 • 从“先辈”处获得属性和行为特征 • C++中类的继承 • 继承:新的类从已有类那里得到已有的特性 • 派生:从已有类产生新类的过程 • 单继承:一个派生类只有一个直接基类 • 多继承:一个派生类同时有多个基类

  6. 20.2 Base Classes and Derived Classes • 派生类生成的过程 • 吸收已有的基类的成员 • 构造函数、析构函数除外 • 改造基类的成员 • 声明同名的成员后,隐藏了原基类的成员 • 添加新的成员 Student 吸收已有的基类成员 学生 Graduate 添加新的成员 研究生 改造基类的成员

  7. 20.2 Protected Members • C++中类的成员的访问控制属性 • public: 公有的 • 在该类中直接访问 • 在该类以外,可以通过该类的名字、引用或者指针访问 • private: 私有的 • 在该类中直接访问 • 在该类以外,只能通过该类的友元来访问 • protected: 保护的 • 在该类中直接访问 • 在该类以外,可以通过该类的派生类、该类的友元访问

  8. 公共的成员,可以被类以外直接访问 保护的成员,可以被该类的派生类 的成员或者友元访问 私有的成员,只能通过友元或者公共 接口来访问

  9. Lecture 17 • Chapter 20. Object-Oriented Programming: Inheritance • 20.1 Introduction • 20.2 Base Classes and Derived Classes • 20.3 protected Members • 20.4 Relationship between Base Classes and Derived Classes • 20.5 Constructors and Destructors in Derived Classes • 20.6 public, protected and private Inheritance • 20.7 Software Engineering with Inheritance

  10. 继承的好处:避免了对同样功能的代码的“复制-粘贴”,有利于代码的维护继承的好处:避免了对同样功能的代码的“复制-粘贴”,有利于代码的维护

  11. 派生类修改了基类的计算部分 派生类新增了对派生数据成员的操作

  12. 为了在派生类中,便于使用从基类继承的数据成员,可以在基类中,将这些为了在派生类中,便于使用从基类继承的数据成员,可以在基类中,将这些 数据成员声明为“保护”的数据成员

  13. 当基类的数据成员声明为private时: 私有数据 基类的公共接口 成员函数 派生类的对象 基类的对象 当基类的数据成员声明为protected时: 私有数据 派生类的对象 基类的对象 但是,绕过了基类的公共接口函数,有可能破坏了基类的数据维护关系, 使得基类对象的数据处于不一致的状态。因此更好的选择是将基类的数据 成员声明为private。

  14. 更好的选择是将基类的数据成员声明为private,通过基类的公共接口函数更好的选择是将基类的数据成员声明为private,通过基类的公共接口函数 对这些数据成员进行访问。

  15. Lecture 17 • Chapter 20. Object-Oriented Programming: Inheritance • 20.1 Introduction • 20.2 Base Classes and Derived Classes • 20.3 protected Members • 20.4 Relationship between Base Classes and Derived Classes • 20.5 Constructors and Destructors in Derived Classes • 20.6 public, protected and private Inheritance • 20.7 Software Engineering with Inheritance

  16. 20.5 Constructors and Destructors in Derived Classes • 基类的构造函数和析构函数不能被继承 • 派生类的构造函数 • 如果 派生类没有新增数据成员 • 系统缺省会调用基类的构造函数和析构函数来处理 • 如果 派生类有新增的数据成员 • 新增成员的初始化需要由派生类的构造函数完成 • 原基类成员的初始化通过调用基类的构造函数完成 • 注意:对派生类中新增成员对象的初始化,调用顺序按照他们在类中声明的顺序 • 派生类的析构函数 • 做好新增的数据成员的清理工作,系统会自动调用原基类的析构函数清理原基类成员

  17. 20.6 public, protected and private Inheritance • C++中类的继承方式 • public:公有继承 • 基类的公有成员和保护成员的访问属性不变 • 基类的私有成员不可直接访问 • protected: 保护继承 • 基类的公有成员和保护成员都以保护成员身份在派生类 • 基类的私有成员不可直接访问 • private:私有继承 • 基类的公有成员和保护成员都以私有成员身份在派生类 • 基类的私有成员不可直接访问 在实际中建议:做好基类的成员访问的限定,通过公有继承来派生新的类

  18. Lecture 17 • Chapter 20. Object-Oriented Programming: Inheritance • 20.1 Introduction • 20.2 Base Classes and Derived Classes • 20.3 protected Members • 20.4 Relationship between Base Classes and Derived Classes • 20.5 Constructors and Destructors in Derived Classes • 20.6 public, protected and private Inheritance • 20.7 Software Engineering with Inheritance

  19. 面向对象的软件开发 • 面向对象的软件开发方法 • 从分析问题模型开始,识别出“对象” • 对对象的特征进行抽象,获得“类” • 最后用程序语言去描述他们的过程 • 程序更加关心对象与对象之间的关系 • 面向过程的开发  面向对象的开发 • 如何编码实现过程  如何分析对象关系 • 以功能实现为中心  以数据操作为中心

  20. 对象与对象的关系 • 对象之间有两种关系:复合,继承 • 对象的复合 • 对象A包含对象B,拥有,has-a • 对象的继承 • 对象A是对象B的一个特例,是,is-a • 类的设计 • 如果新的类中有成员是已有的类的对象,使用复合方式比较合适 • 如果新的类可以视为已有类的子类或者特例,使用继承比较合适

  21. 面向对象的分析案例 点 线段包括两个顶点; 线段的运算是计算线段 的长度 线段 多边形包括多个线段; 多边形的运算是获得多 边形的周长 多边形 三角形 三角形包括三个线段, 是多边形的特例,三角 形的典型运算是获得其 面积

  22. 面向对象的分析案例 -startPoint 1 1 -endPoint 1 1 思考: 这两个类是什么关系? 在Line类中是否需要设置数据成员length? 在Line类中是否需要设置输入四个坐标值的setline函数?

  23. 面向对象的分析案例 -borders 1 1….* #defineMAX_BORDER_NUM6 思考: 这两个类是什么关系? 在Polygon类中需要设置Point类型的数据成员? 在Polygon类能否支持动态数量的边长数组? 在Polygon类中能否加上对形状的判别?

  24. 面向对象的分析案例 思考: 这两个类是什么关系? 在Trianlge类中还需要其他的数据成员吗? 在Trianlge类中能否加上对形状的判别?

  25. Experiment • 实现前述提到的几个类,分别实现其头文件h文件和源文件cpp文件,测试你的类是否有效

  26. 谢谢! 刘威 副教授 互联网技术与工程研究中心 华中科技大学电子与信息工程系 电话:13986224922 Email: liuwei@hust.edu.cn 网址:http://itec.hust.edu.cn

More Related