c++ programming lecture 17

27
C++ Programming Lecture 17 Wei Liu ( 刘刘 ) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology Feb. 2014

Upload: rhonda

Post on 23-Feb-2016

58 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: C++ Programming Lecture 17

C++ ProgrammingLecture 17

Wei Liu ( 刘威 )Dept. of Electronics and Information Eng.

Huazhong University of Science and TechnologyFeb. 2014

Page 2: C++ Programming Lecture 17

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

-2-

Page 3: C++ Programming Lecture 17

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. 现有的类被称为基类,从基类继承出来的类被称为派生类

-3-

Page 4: C++ Programming Lecture 17

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

-4-

Page 5: C++ Programming Lecture 17

20.2 Base Classes and Derived Classes 现实世界的继承

从“先辈”处获得属性和行为特征 C++ 中类的继承

继承:新的类从已有类那里得到已有的特性 派生:从已有类产生新类的过程 单继承:一个派生类只有一个直接基类 多继承:一个派生类同时有多个基类

-5-

Page 6: C++ Programming Lecture 17

-6-

Page 7: C++ Programming Lecture 17

20.2 Base Classes and Derived Classes 派生类生成的过程

吸收已有的基类的成员 构造函数、析构函数除外

改造基类的成员 声明同名的成员后,隐藏了原基类的成员

添加新的成员

-7-

Student

Graduate

学生

研究生

Student

+ int id;+ string name;+ float score;- char gender;

+ Student()+ display()

Graduate

+ int id;+ string name;+ float score;- char gender;- float pay;

+ Graduate()+ display()

吸收已有的基类成员

添加新的成员改造基类的成员

Page 8: C++ Programming Lecture 17

20.2 Protected Members C++ 中类的成员的访问控制属性

public: 公有的 在该类中直接访问 在该类以外,可以通过该类的名字、引用或者指针访问

private: 私有的 在该类中直接访问 在该类以外,只能通过该类的友元来访问

protected: 保护的 在该类中直接访问 在该类以外,可以通过该类的派生类、该类的友元访问

-8-

Page 9: C++ Programming Lecture 17

-9-

公共的成员,可以被类以外直接访问

保护的成员,可以被该类的派生类的成员或者友元访问

私有的成员,只能通过友元或者公共接口来访问

Page 10: C++ Programming Lecture 17

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-

Page 11: C++ Programming Lecture 17

-11-

CommissionEmployee

- string firstName;- string lastName;- string socialSecurityNumber;- double grossSales;- double commissionRate;

+ CommissionEmployee(const string &, const string &, double, double)+ void setFirstName(const string &);+ string getFirstName() const;……+ double earnings() const;+ void print() const;

BasePlusCommissionEmployee

- string firstName;- string lastName;- string socialSecurityNumber;- double grossSales;- double commissionRate;- double baseSalary;

+ CommissionEmployee(const string &, const string &, double, double, double)+ void setFirstName(const string &);+ string getFirstName() const;…+ void setBaseSalary (const double &);+ double getBaseSalary() const;…+ double earnings() const;+ void print() const;

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

Page 12: C++ Programming Lecture 17

-12-

派生类修改了基类的计算部分

派生类新增了对派生数据成员的操作

Page 13: C++ Programming Lecture 17

-13-

CommissionEmployee

- string firstName;- string lastName;- string socialSecurityNumber;- double grossSales;- double commissionRate;

+ CommissionEmployee(const string &, const string &, double, double)+ void setFirstName(const string &);+ string getFirstName() const;……

CommissionEmployee

# string firstName;# string lastName;# string socialSecurityNumber;# double grossSales;# double commissionRate;

+ CommissionEmployee(const string &, const string &, double, double)+ void setFirstName(const string &);+ string getFirstName() const;……

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

Page 14: C++ Programming Lecture 17

-14-

基类的公共接口成员函数 私有数据派生类的对象基类的对象

当基类的数据成员声明为 private时:

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

私有数据派生类的对象基类的对象

当基类的数据成员声明为 protected时:

Page 15: C++ Programming Lecture 17

-15-

CommissionEmployee

- string firstName;- string lastName;- string socialSecurityNumber;- double grossSales;- double commissionRate;

+ CommissionEmployee(const string &, const string &, double, double)+ void setFirstName(const string &);+ string getFirstName() const;……

CommissionEmployee

# string firstName;# string lastName;# string socialSecurityNumber;# double grossSales;# double commissionRate;

+ CommissionEmployee(const string &, const string &, double, double)+ void setFirstName(const string &);+ string getFirstName() const;……

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

Page 16: C++ Programming Lecture 17

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-

Page 17: C++ Programming Lecture 17

20.5 Constructors and Destructors in Derived Classes 基类的构造函数和析构函数不能被继承 派生类的构造函数

如果 派生类没有新增数据成员 系统缺省会调用基类的构造函数和析构函数来处理

如果 派生类有新增的数据成员 新增成员的初始化需要由派生类的构造函数完成 原基类成员的初始化通过调用基类的构造函数完成

注意:对派生类中新增成员对象的初始化,调用顺序按照他们在类中声明的顺序 派生类的析构函数

做好新增的数据成员的清理工作,系统会自动调用原基类的析构函数清理原基类成员-17-

Page 18: C++ Programming Lecture 17

20.6 public, protected and private Inheritance C++ 中类的继承方式

public: 公有继承 基类的公有成员和保护成员的访问属性不变 基类的私有成员不可直接访问

protected: 保护继承 基类的公有成员和保护成员都以保护成员身份在派生类 基类的私有成员不可直接访问

private: 私有继承 基类的公有成员和保护成员都以私有成员身份在派生类 基类的私有成员不可直接访问

在实际中建议:做好基类的成员访问的限定,通过公有继承来派生新的类-18-

Page 19: C++ Programming Lecture 17

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-

Page 20: C++ Programming Lecture 17

面向对象的软件开发 面向对象的软件开发方法

从分析问题模型开始,识别出“对象” 对对象的特征进行抽象,获得“类” 最后用程序语言去描述他们的过程 程序更加关心对象与对象之间的关系

面向过程的开发 面向对象的开发 如何编码实现过程 如何分析对象关系 以功能实现为中心 以数据操作为中心

-20-

Page 21: C++ Programming Lecture 17

对象与对象的关系 对象之间有两种关系:复合,继承

对象的复合 对象 A 包含对象 B ,拥有, has-a

对象的继承 对象 A 是对象 B 的一个特例,是, is-a

类的设计 如果新的类中有成员是已有的类的对象,使用复合方式比较合适 如果新的类可以视为已有类的子类或者特例,使用继承比较合适

-21-

Page 22: C++ Programming Lecture 17

面向对象的分析案例

-22-

点线段多边形

三角形

多边形包括多个线段;多边形的运算是获得多边形的周长

线段包括两个顶点;线段的运算是计算线段的长度

三角形包括三个线段,是多边形的特例,三角形的典型运算是获得其面积

Page 23: C++ Programming Lecture 17

面向对象的分析案例

-23-

Point 点- double x; - double y;+ Point(double,double);+ void setPoint(double, double);+ double getX();+ double getY();

Line 线段- Point startPoint; - Point endPoint;+ Line(point, point);+ void setLine(point, point);+ Point getStart ();+ Point getEnd();+ double getLength();

- startPoint

1

1

1

1

- endPoint

思考:这两个类是什么关系?在 Line 类中是否需要设置数据成员 length ?在 Line 类中是否需要设置输入四个坐标值的 setline 函数?

Page 24: C++ Programming Lecture 17

面向对象的分析案例

-24-

Line 线段- Point startPoint; - Point endPoint;+ Line(point, point);+ void setLine(point, point);+ Point getStart();+ Point getEnd();+ double getLength();

- borders

1….*1

Polygon 多边形- Line borders[]; - int number;

+ Polygon(Point[], int);+ void setPolygon(Point[], int);+ int getNumber();+ double getPerimeter();

#define MAX_BORDER_NUM 6

思考:这两个类是什么关系?在 Polygon 类中需要设置 Point 类型的数据成员?在 Polygon 类能否支持动态数量的边长数组?在 Polygon 类中能否加上对形状的判别?

Page 25: C++ Programming Lecture 17

面向对象的分析案例

-25-

Triangle 三角形

+ double getArea();

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

Polygon 多边形- Line borders[]; - int number;

+ Polygon(Point[], int);+ void setPolygon(Point[], int);+ int getNumber();+ double getPerimeter();

Page 26: C++ Programming Lecture 17

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

-26-

Page 27: C++ Programming Lecture 17

谢谢!刘威 副教授

互联网技术与工程研究中心华中科技大学电子与信息工程系电话: 13986224922Email: [email protected] 网址: http://itec.hust.edu.cn