《c 程序设计基础》第八章-继承和派生.ppt_第1页
《c 程序设计基础》第八章-继承和派生.ppt_第2页
《c 程序设计基础》第八章-继承和派生.ppt_第3页
《c 程序设计基础》第八章-继承和派生.ppt_第4页
《c 程序设计基础》第八章-继承和派生.ppt_第5页
已阅读5页,还剩101页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C 程序设计基础 第8章继承与派生北京邮电大学信通院方莉mrs fangli 2 第8章继承与派生 类具有封装性 继承性和多态性 使得软件开发者可以把类设计成相对独立的模块 像一个个积木块 便于重用 类的继承性是实现软件重用的一种重要形式 本章将学习 继承和派生的概念 继承方式 派生类的构造函数和析构函数 3 第8章继承与派生 8 1继承的概念8 2定义基类和派生类8 3构造函数和析构函数8 4转换与继承 4 8 1继承的概念 例子 自行车Bicycle 山地车MountainBike 竞速自行车RacingBike 双人自行车TandemBike 5 8 1继承的概念 例子 山地车继承了自行车的特征 山地车 派生 于自行车 6 8 1继承的概念 类的继承是在现有类的基础之上 创建新类的机制 称现有的类为基类 新建立的类为派生类 派生类继承了基类的属性和行为派生类是基类的特殊情况 基类是共性的抽象 派生类是个性的体现 不必从 草稿 开始创建特殊的程序对象 继承是处理 特殊情况 的面向对象编程机制 7 8 1继承的概念 8 第8章继承与派生 8 1继承的概念8 2定义基类和派生类8 3构造函数和析构函数8 4转换与继承 9 8 2定义基类和派生类 定义派生类时要声明继承方式 三种继承方式 publicProtectedprivate访问控制受继承方式的影响 不同继承方式的影响主要体现在 派生类成员对基类成员的访问控制 派生类对象对基类成员的访问控制 继承导致一种特殊的语法现象 同名覆盖 10 8 2 1简单的继承和派生 问题 想在屏幕上画出正三角形 矩形或圆形方法一 结构化方法DrawTri intx inty charcolor intside DrawRect intx inty charcolor intlength intwidth DrawCircle intx inty charcolor intRadius 方法二 面向对象 classcircle圆形classrectangle矩形classtriangle三角形基类 classshape色彩color位置 x y 泛化 11 图形circle圆形rectangle矩形triangle三角形 基类称为父类派生类称为子类 分层次的设计类使模块划分更合理 便于软件系统的设计和维护 8 2 1简单的继承和派生 12 classShape private intm x m y 位置charm color 颜色public Shape voidsetposition intx inty voidsetcolor charcolor intgetx intgety chargetcolor 例1 定义基类shape 成员函数的分类 构造函数设置属性值读取属性值 13 classTriangle publicShape public Triangle intx inty charcolor R floatslen 1 floatGetSideLength const voidSetTriangle intx inty charcolor floatslen voidDraw private floatm SideLength 例1 定义派生类 等边三角形类 成员函数的分类 构造函数设置属性值读取属性值 14 派生类的定义格式class派生类名 继承方式基类名 public 派生类公有成员 private 派生类私有成员 派生类只有一个直接基类为单继承 8 2 2定义派生类 15 class派生类名 继承方式基类名1 继承方式基类名n public 派生类公有成员 private 派生类私有成员 有多个基类 派生类有多个基类为多继承 8 2 2定义派生类 16 例如 已有基类b1和b2 定义派生类derive 其中包括 私有整型成员newInt 公有函数成员intnewFun 私有函数成员intmax inta intb 写出类derive的定义 8 2 2定义派生类 classderive publicb1 privateb2 private intnewInt public voidnewFun private intmax inta intb 17 类的继承方式是派生类对基类成员的继承方式 类的继承方式影响类外模块对于派生类从基类继承来的成员的访问权限 每一个 继承方式 只用于限制对紧随其后之基类的继承 classderive publicb1 privateb2 private intnewInt public voidnewFun private intmax inta intb 8 2 2定义派生类 18 classbase classderiver1 publicbase classderiver2 publicderiver1 父类被称为子类的直接基类父类的父类或更高层次的父类被称为这个子类的间接基类 8 2 2定义派生类 19 classShape public Shape intx 0 inty 0 charc R intGetX const voidSetX intx intGetY const voidSetY intx charGetColor const voidSetColor charc protected charm color intm x intm y 例2 图形类及其派生类的声明 20 classCircle publicShape public Circle intx inty floatr 1 charcolor R voidSetCircle intx inty floatr charcolor floatGetRadius const voidDraw private floatm Radius 例2 图形类及其派生类的声明 21 classTriangle publicShape public Triangle intx inty charcolor R floatslen 1 voidSetTriangle intx inty charcolor floatslen floatGetSideLength const voidDraw private floatm SideLength 例2 图形类及其派生类的声明 22 classRectangle publicShape public Rectangle intx inty charcolor intlength 10 intwidth 10 voidSetRectangle intx inty charcolor intlength intwidth intGetWidth const intGetHeight const voidDraw private intm Width intm Length 例2 图形类及其派生类的声明 23 派生类的成员包括 1 继承基类的成员 2 派生类定义时声明的成员 派生类自己增加的成员 完成两个需求 1 修改基类成员 2 描述新的特征或方法 从基类继承的成员 派生类增加的成员 24 8 2 3访问控制和继承关系 类成员的可见性公共成员 public保护成员 protected私有成员 private继承的方式公有继承 public 保护继承 protected 私有继承 private 派生类继承了基类中的所有成员 但不包括构造函数析构函数 25 8 2 3访问控制和继承关系 不同继承方式决定的不同访问控制权限体现在 派生类的成员函数对其继承的基类成员的访问控制 其它模块通过派生类对象对其继承的基类成员的访问控制 继承来的成员的访问权限 26 1 公有继承 公有继承的派生类定义形式 class派生类名 public基类名 派生类新成员定义 基类成员在派生类中的访问属性不变 派生类的成员函数可以访问基类的公有成员和保护成员 不能访问基类的私有成员 派生类以外的其它函数可以通过派生类的对象 访问从基类继承的公有成员 但不能访问从基类继承的保护成员和私有成员 派生类成员函数可以访问 27 public是定义公有继承方式的关键字 公有继承方式定义的派生类 继承了基类中除构造函数和析构函数外的其余成员 公有成员 保护成员和私有成员被继承的基类成员在派生类中仍将保持其原来的访问属性 派生类的成员函数可以访问基类的公有成员和保护成员 不能访问基类的私有成员 派生类以外的其它函数可以通过派生类的对象 访问从基类继承的公有成员 但不能访问从基类继承的保护成员和私有成员 8 2 3访问控制和继承关系 28 classPoint 基类Point类的定义 public 公有函数成员voidInitP floatxx 0 floatyy 0 X xx Y yy voidMove floatxOff floatyOff X xOff Y yOff floatGetX returnX floatGetY returnY private 私有数据成员floatX Y 8 2 3访问控制和继承关系 29 classRectangle publicPoint 派生类声明部分 public 新增公有函数成员voidInitR floatx floaty floatw floath InitP x y 访问基类公有成员函数W w H h floatGetH returnH floatGetW returnW private 新增私有数据成员floatW H 派生类中的成员函数可以直接访问基类中的public和protected成员 但不能访问基类的private成员 8 2 3访问控制和继承关系 30 main Rectanglerect cout rect X cout rect GetX 使用派生类的对象只能访问基类的public成员 8 2 3访问控制和继承关系 错误 正确 31 classRectangle publicPoint 派生类声明部分 public 新增公有函数成员voidInitR floatx floaty floatw floath X x Y y 访问基类私有成员W w H h floatGetH returnH floatGetW returnW private 新增私有数据成员floatW H 派生类中的成员函数可以直接访问基类中的public和protected成员 但不能访问基类的private成员 8 2 3访问控制和继承关系 错误 32 classPoint 基类Point类的定义 public voidInitP floatxx 0 floatyy 0 X xx Y yy voidMove floatxOff floatyOff X xOff Y yOff floatGetX returnX floatGetY returnY protected floatX Y 8 2 3访问控制和继承关系 33 classRectangle publicPoint 派生类声明部分 public 新增公有函数成员voidInitR floatx floaty floatw floath X x Y y 访问基类的保护成员W w H h floatGetH returnH floatGetW returnW private 新增私有数据成员floatW H 派生类中的成员函数可以直接访问基类中的public和protected成员 但不能访问基类的private成员 8 2 3访问控制和继承关系 正确 34 main Rectanglerect cout rect X cout rect GetX 使用派生类的对象只能访问基类的public成员 依然错误 8 2 3访问控制和继承关系 正确 35 2 私有继承 私有继承的派生类定义形式 class派生类名 private基类名 派生类新成员定义 基类成员 在派生类中的访问属性都变成private 派生类的成员函数 可以访问基类的公有成员和保护成员 不能访问基类的私有成员 派生类以外的其它函数 不能通过派生类的对象 访问从基类继承的任何成员 派生类成员函数可以访问 派生类对象 36 private是定义私有继承方式的关键字以私有继承方式定义的派生类 继承了基类中可以继承的成员 公有成员 保护成员和私有成员 这些成员在派生类中的访问属性都是私有的 派生类的成员函数可以访问基类的公有成员和保护成员 不能访问基类的私有成员 派生类以外的其它函数则不能通过派生类的对象访问从基类继承的任何成员 8 2 3访问控制和继承关系 37 classPoint 基类声明public voidInitP floatxx 0 floatyy 0 X xx Y yy voidMove floatxOff floatyOff X xOff Y yOff floatGetX returnX floatGetY returnY private floatX Y 8 2 3访问控制和继承关系 38 classRectangle privatePoint 派生类声明 public 新增外部接口voidInitR floatx floaty floatw floath InitP x y W w H h 派生类访问基类公有成员voidMove floatxOff floatyOff Point Move xOff yOff floatGetX returnPoint GetX floatGetY returnPoint GetY floatGetH returnH floatGetW returnW private 新增私有数据floatW H 派生类中的成员函数可以直接访问基类中的public和protected成员 但不能访问基类的private成员 8 2 3访问控制和继承关系 正确 39 classRectangle privatePoint 派生类声明 public 新增外部接口voidInitR floatx floaty floatw floath X x Y y W w H h voidMove floatxOff floatyOff Point Move xOff yOff floatGetX returnPoint GetX floatGetY returnPoint GetY floatGetH returnH floatGetW returnW private 新增私有数据floatW H 派生类中的成员函数可以直接访问基类中的public和protected成员 但不能访问基类的private成员 8 2 3访问控制和继承关系 错误 40 classPoint 基类声明public voidInitP floatxx 0 floatyy 0 X xx Y yy voidMove floatxOff floatyOff X xOff Y yOff floatGetX returnX floatGetY returnY protected floatX Y 8 2 3访问控制和继承关系 41 classRectangle privatePoint 派生类声明 public 新增外部接口voidInitR floatx floaty floatw floath X x Y y W w H h voidMove floatxOff floatyOff Point Move xOff yOff floatGetX returnPoint GetX floatGetY returnPoint GetY floatGetH returnH floatGetW returnW private 新增私有数据floatW H 8 2 3访问控制和继承关系 正确 42 main Rectanglerect cout rect X cout rect GetX 使用派生类的对象不能访问基类中的任何成员 8 2 3访问控制和继承关系 错误 错误 43 3 保护继承 保护继承的派生类定义形式 class派生类名 protected基类名 派生类新成员定义 基类成员 公有成员和保护成员在派生类中变成保护类型的 基类的私有成员属性不变 派生类的成员函数 可以访问基类的公有成员和保护成员 不能访问基类的私有成员 派生类以外的其它函数 不能通过派生类的对象 访问从基类继承的任何成员 派生类成员函数可以访问 派生类对象 44 protected是定义保护继承方式的关键字以保护继承方式定义的派生类 继承了基类中可以继承的成员 公有成员 保护成员和私有成员 其中基类的公有成员和保护成员在派生类中访问控制属性变成保护类型的 基类的私有成员保持原来属性 派生类的成员函数可以访问基类的公有成员和保护成员 不能访问基类的私有成员 派生类以外的其它函数则不能通过派生类的对象访问从基类继承的任何成员 8 2 3访问控制和继承关系 45 classPoint 基类声明public voidInitP floatxx 0 floatyy 0 X xx Y yy voidMove floatxOff floatyOff X xOff Y yOff floatGetX returnX floatGetY returnY private floatX Y 8 2 3访问控制和继承关系 46 classRectangle protectedPoint 派生类声明 public 新增外部接口voidInitR floatx floaty floatw floath InitP x y W w H h 派生类访问基类公有成员voidMove floatxOff floatyOff Point Move xOff yOff floatGetX returnPoint GetX floatGetY returnPoint GetY floatGetH returnH floatGetW returnW private 新增私有数据floatW H 派生类中的成员函数可以直接访问基类中的public和protected成员 但不能访问基类的private成员 8 2 3访问控制和继承关系 正确 47 classRectangle protectedPoint 派生类声明 public 新增外部接口voidInitR floatx floaty floatw floath X x Y y W w H h voidMove floatxOff floatyOff Point Move xOff yOff floatGetX returnPoint GetX floatGetY returnPoint GetY floatGetH returnH floatGetW returnW private 新增私有数据floatW H 派生类中的成员函数可以直接访问基类中的public和protected成员 但不能访问基类的private成员 8 2 3访问控制和继承关系 错误 48 classPoint 基类声明public voidInitP floatxx 0 floatyy 0 X xx Y yy voidMove floatxOff floatyOff X xOff Y yOff floatGetX returnX floatGetY returnY protected floatX Y 8 2 3访问控制和继承关系 49 classRectangle protectedPoint 派生类声明 public 新增外部接口voidInitR floatx floaty floatw floath X x Y y W w H h voidMove floatxOff floatyOff Point Move xOff yOff floatGetX returnPoint GetX floatGetY returnPoint GetY floatGetH returnH floatGetW returnW private 新增私有数据floatW H 派生类中的成员函数可以直接访问基类中的public和protected成员 但不能访问基类的private成员 8 2 3访问控制和继承关系 正确 50 main Rectanglerect cout rect X cout rect GetX 使用派生类的对象不能访问基类中的任何成员 8 2 3访问控制和继承关系 错误 错误 51 私有继承和保护继承的区别 孙类成员函数无法访问 孙类成员函数可以访问 52 8 2 3访问控制和继承关系 53 派生类修改基类的成员 是在派生类中声明了一个与基类成员同名的新成员 在派生类作用域内或者在类外通过派生类的对象直接使用这个成员名 只能访问到派生类中声明的同名新成员 这个新成员覆盖了从基类继承的同名成员 这种情况称为同名覆盖 8 2 4同名覆盖 54 classbase public voidf cout base endl classderiver publicbase public voidf cout deriver endl intmain deriverderobj derobj f return0 输出结果 A baseB deriver OverRide 只能访问到派生类中的同名新成员 8 2 4同名覆盖 55 同名覆盖示例 includeusingnamespacestd classbase public voidfunction cout functionofclassbase endl classderiver publicbase public voidfunction cout functionofclassderiver endl 同名的新成员 同名覆盖 56 voidmain deriverderobj derobj function 输出结果 functionofclassderiver 同名覆盖示例 57 第8章继承与派生 8 1继承的概念8 2定义基类和派生类8 3构造函数和析构函数8 4转换与继承 58 8 3派生类构造函数和析构函数 派生类继承了基类中除构造函数和析构函数之外的所有成员 基类的构造函数和析构函数不能被派生类所继承 派生类一般需要定义自己的构造函数和析构函数 派生类的构造及析构函数通常会受到基类构造及析构函数的影响 59 8 3 1基类只有无参数的构造函数 在基类具有无参构造函数 派生类又没有定义构造函数的时候 系统会自动的调用基类无参构造函数 来构造派生类对象中的基类成分 60 includeusingnamespacestd classbase private intm data public voidSetData intdata m data data intGetData returnm data 使用系统提供的默认构造函数 61 classderiver publicbase private intm member public voidSetMember intm m member m intGetMember returnm member 使用系统提供的默认构造函数 62 voidmain intn 10 deriverobj obj SetMember n cout obj GetMember endl 输出结果 10 使用系统提供的默认构造函数 63 8 3 1基类只有无参数的构造函数 如果基类没有无参构造函数 派生类也不定义自己的构造函数 在编译的时候 一定会有语法错误 64 如果基类和对象数据成员的构造函数都无参数 派生类构造函数形参表中将只包含用于初始化它自己的基本类型数据成员的参数 如果这个派生类恰好没有基本类型的数据成员 则其构造函数的形参表为空 可以不定义构造函数 而使用系统提供的默认构造函数 8 3 1基类只有无参数的构造函数 65 8 3 2派生类的构造函数 不参与继承的特殊函数构造函数析构函数作为特权地位的友元函数赋值运算符函数派生类需要自己定义的构造函数和析构函数 66 基类的构造函数不被继承 需要在派生类中自行定义 派生类的构造函数完成 初始化本类的数据成员 完成派生类中基类成分的初始化 通过调用基类的构造函数 并为基类的构造函数传递参数 如果数据成员有对象 则需调用对象成员的构造函数 8 3 2派生类的构造函数 67 派生类的构造函数 构造函数参数表中以合适的初值为参数 初始化本类中新增成员 利用成员初始化表隐含调用基类和新增对象数据成员的构造函数 初始化它们各自的数据成员 构造函数的调用次序系统会使用派生类构造函数的形参表的参数调用基类和内嵌对象成员的构造函数 8 3 2派生类的构造函数 68 派生类构造函数的一般形式 派生类名 派生类名 基类所需的形参 本类成员所需的形参 基类1 基类参数表1 基类n 基类参数表n 对象成员1 对象参数表1 对象成员m 对象参数表m 本类基本类型数据成员初始化 8 3 2派生类构造函数 69 单继承时的构造函数派生类名 派生类名 基类所需的形参 本类成员所需的形参 基类名 参数 本类成员初始化赋值语句 8 3 2派生类构造函数 70 例8 4单继承派生类构造函数 71 多文件结构 主函数 ch8 4 main cpp类TShape的声明 TShape04 h类TShape的实现 TShape04 cpp类TEllipse的声明 TEllipse04 h类TEllipse的实现 TEllipse04 cpp 例8 4单继承派生类构造函数 72 TShape04 cpp include TShape04 h includeTShape TShape uintx uinty x x y y voidTShape Draw std cout ThisisTShape Draw std endl voidTShape getXY uint TShape04 htypedefunsignedintuint typedefunsignedcharuchar classTShape private uint x y 几何形状的位置protected 声明几何形状的颜色 允许TShape的派生类直接访问这些颜色属性 而不允许在类外通过类的对象直接访问这些属性 uchar RED GREEN BLUE public TShape uintx uinty voidgetXY uint 例8 4单继承派生类构造函数 73 TEllipse04 cpp include TEllipse04 h includeTEllipse TEllipse uintlongR uintshortR uintx uinty TShape x y longR longR shortR shortR 在派生类构造函数中初始化基类保护成员 RED 0 x00 GREEN 0 x00 BLUE 0 x00 TEllipse TEllipse voidTEllipse Draw uintx y getXY x y 调用基类函数获取椭圆的圆心坐标std cout RED GREEN BLUE atpoint std cout x y std endl 错误 在派生类中不能访问基类私有成员std cout x y std endl TEllipse04 h include TShape04 h classTEllipse publicTShape protected uint longR shortR public TEllipse uintlongR uintshortR uintx uinty TEllipse voidDraw voidgetR uint 例8 4单继承派生类构造函数 74 ch8 4 main cpp include TEllipse04 h includeusingnamespacestd voidmain TEllipseelps 10u 5u 20u 30u elps setRGB 0 x00 0 x00 0 xff 调用基类的函数访问基类的保护成员 elps RED 0 x10 错误 不能通过派生类的对象调用基类的私有成员elps Draw 调用派生类的Draw函数 例8 4单继承派生类构造函数 75 派生类名 派生类名 基类所需形参 本类成员所需形参 基类1 基类参数表1 基类n 基类参数表n 对象成员1 对象参数表1 对象成员m 对象参数表m 本类基本类型数据成员初始化 8 3 3包含内嵌对象的派生类构造函数 76 派生类的构造函数提供 基类构造函数形参表所需的参数初始化派生类的内嵌对象数据成员所需的参数初始化派生类基本数据类型成员所需的参数将例8 4中的颜色抽取形成一个新类TColor 用TColor类的对象作为TShape类中的数据成员 重新设计程序 8 3 3包含内嵌对象的派生类构造函数 77 例8 5派生类的构造函数 78 多文件结构 主函数 ch8 5 main cpp类TColor的声明 TColor05 h类TColor的实现 TColor05 cpp类TShape的声明 TShape05 h类TShape的实现 TShape05 cpp类TEllipse的声明 TEllipse05 h类TEllipse的实现 TEllipse05 cpp头文件GlobalType05 h 例8 5派生类的构造函数 79 TShape05 h pragmaonce include GlobalType05 h include TColor05 h classTShape private uint x y 几何形状的位置protected TColor color 颜色public TShape uintx uinty TShape uintx uinty TColorcolor TShape voidgetXY uint GlobalType05 h pragmaonce 预处理指令 避免重复包含本头文件typedefunsignedintuint typedefunsignedcharuchar TColor05 h pragmaonce include GlobalType05 h enumEColorComponent RED GREEN BLUE classTColor private uchar RED GREEN BLUE public TColor constucharR 0 x00 constucharG 0 x00 constucharB 0 x00 普通构造函数TColor constTColor 例8 5派生类的构造函数 80 TColor05 cpp include TColor05 h TColor TColor ucharR 0 x00 普通构造函数ucharG 0 x00 ucharB 0 x00 RED R GREEN G BLUE B TColor TColor constTColor TEllipse h pragmaonce include TShape05 h include GlobalType05 h classTEllipse publicTShape protected uint longR shortR public TEllipse uintlongR uintshortR uintx uinty TColorcolor TEllipse uintlongR uintshortR uintx uinty TEllipse voidDraw voidgetR uint 例8 5派生类的构造函数 81 voidTShape getXY uint TShape05 cpp include TShape05 h include TColor05 h includeTShape TShape uintx uinty color x x y y TShape TShape uintx uinty TColorcolor x x y y color color voidTShape Draw uintR G B R static cast color getComponent RED G static cast color getComponent GREEN B static cast color getComponent BLUE std cout Drawanshapewithcolor std cout R G B atpoint std cout x y std endl 例8 5派生类的构造函数 82 TEllipse05 cpp include TEllipse05 h includeTEllipse TEllipse uintlongR uintshortR uintx uinty TColorcolor TShape x y longR longR shortR shortR color color TEllipse TEllipse uintlongR uintshortR uintx uinty TShape x y longR longR shortR shortR TEllipse TEllipse voidTEllipse Draw uintx y getXY x y 调用基类函数获取椭圆的圆心坐标uintR G B R static cast color getComponent RED G static cast color getComponent GREEN B static cast color getComponent BLUE std cout Drawanellipsewithcolor std cout R G B atpoint std cout x y std endl 例8 5派生类的构造函数 83 ch8 5 main cpp include TEllipse05 h includeusingnamespacestd voidmain TShapeshp 0u 0u shp setColor TColor 0 xff 0 x00 0 x00 cout FILE LINE shp Draw TEllipseelps01 10u 5u 0u 0u cout FILE LINE elps01 Draw 例8 5派生类的构造函数 TEllipseelps02 10u 5u 20u 30u TColor 0 x00 0 xff 0 x00 cout FILE LINE elps02 TShape Draw cout FILE LINE elps02 Draw elps02 setColor shp getColor cout FILE LINE elps02 Draw elps02 setColor TColor shp getColor cout FILE LINE elps02 Draw 84 TShape06 h pragmaonce include GlobalType06 h include TColor06 h classTShape private uint x y 几何形状的位置protected TColor color 颜色public TShape uintx uinty TShape uintx 0u uinty 0u 默认构造函数TShape uintx uinty TColorcolor TShape voidgetXY uint TEllipse06 cpp include TEllipse06 h includeTEllipse TEllipse uintlongR uintshortR uintx uinty TColorcolor TShape x y uintx uinty TColorcolor longR longR shortR shortR 在派生类构造函数中访问基类保护成员 color color TEllipse TEllipse uintlongR uintshortR uintx uinty TShape x y uintx uinty longR longR shortR shortR 例8 6遗漏的基类构造函数 85 派生类的构造函数的一般总结 派生类的构造函数的职责初始化基类初始化对象数据成员初始化基本类型的数据成员方式构造函数初始化列表构造函数函数体除非有特殊要求 const或者reference数据成员只能由初始化列表来获得初值 若不需做上述工作 则可不定义构造函数 而使用系统提供的默认构造函数 86 派生类的构造函数 构造函数的调用次序基类的构造函数内嵌对象的构造函数派生类的构造函数因此基类和内嵌对象的初始化只能放在初始化列表中 不能放到派生类的构造函数体中 多继承时 基类构造函数的调用顺序 按照定义派生类时这些基类被继承的顺序 与他们在初始化列表的次序无关 派生类的多个对象成员的构造函数的调用顺序 按照派生类定义这些成员的顺序进行 与他们在初始化列表中的先后次序无关 有多个基类 多个内嵌对象 87 8 3 4派生类的析构函数 派生类不能继承基类的析构函数 需要自己定义析构函数 以便在派生类对象消亡之前进行必要的清理工作 派生类的析构函数只负责清理它新定义的非对象数据成员 对象数据成员由对象成员所属类的析构函数负责析构 析构函数的调用次序与构造函数相反先派生类析构函数 再基类析构函数 复习 析构函数的功能在对象消亡之前进行必要的清理工作 88 classTColor private string color public TColor stringcolor BLACK cout TColor构造函数 endl color color TColor cout TColor析构函数 endl Main07 cpp include includeusingnamespacestd classTPoint protected int x y public TPoint intx 0 inty 0 cout TPoint构造函数 x y endl x x y y TPoint cout TPoint析构函数 x y endl 例8 7派生类析构函数 89 classTEllipse publicTShape private TPoint pLeftFocus RightFocus public TEllipse TShape RightFocus 2 0 cout 派生类构造函数 endl pLeftFocus newTPoint 1 0 TEllipse cout 派生类析构 endl if pLeftFocus deletepLeftFocus intmain TEllipseelps return0 classTShape protected TColor pColor public TShape cout 基类构造函数 endl pColor newTColor TShape cout 基类析构函数 endl if 0 pColor deletepColor 例8 7派生类析构函数 90 includeusingnamespacestd classbase private intm base data public base intdata m base data data cout baseobjectconstruction endl base cout baseobjectdeconstruction endl 8 3 4派生类的析构函数 91 classAbc private floatm abc data public Abc floatdata m abc data data cout Objectmemberconstruction endl Abc cout Objectmemberdeconstruction endl 8 3 4派生类的析构函数 92 classderiver publicbase private doublem deriver data Abcm member1 int m ptr public deriver intbd floatid doubledd deriver voidfunction 8 3 4派生类的析构函数 93 deriver deriver intbd floatid doubledd base bd m member1 id m deriver data dd m ptr newint 256 if m ptr NULL cout memoryerrorinderiverobj endl cout Deriverobjconstruction endl 8 3 4派生类的析构函数 94 deriver deriver if m ptr NULL delete m ptr cout Deriverobjdeconstruction endl 8 3 4派生类的析构函数 95 voidderiver function cout Maybeyouwanttodosomethingwithm ptrinthisfunction endl cout Doasyoulike

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论