Modeling & Simulation 数学原理 物理规律和程序设计的综合 Magic Fluids Heart Simulator Dongke Sun (Southeast University) C++ Programming and Algorithms April 23,

Size: px
Start display at page:

Download "Modeling & Simulation 数学原理 物理规律和程序设计的综合 Magic Fluids Heart Simulator Dongke Sun (Southeast University) C++ Programming and Algorithms April 23,"

Transcription

1 程序设计与算法语言 继承与派生 C/C++ Programming and Algorithms Inheritance and Derivation Dongke Sun ( 孙东科 ) dksun@seu.edu.cn 东南大学机械工程学院 School of Mechanical Engineering Southeast University April 23, 2018

2 Modeling & Simulation 数学原理 物理规律和程序设计的综合 Magic Fluids Heart Simulator Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

3 面向对象的程序设计 : 抽象 继承和多态 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

4 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

5 提纲 1 2 单一继承 3 多重继承 4 基类成员的初始化 5 总结与思考 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

6 提纲 1 2 单一继承 3 多重继承 4 基类成员的初始化 5 总结与思考 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

7 什么是继承 类的继承, 是新的类从已有类那里得到已有的特性 例如, 对已有类 A 加以扩展, 增加一些新的属性和行为, 构成一个新的类 B, 此时类 B 将类 A 已有的属性和行为继承下来 我们就可称类 B 继承了类 A, 或称类 A 派生了类 B 从已有类产生新类的过程就是类的继承和派生 原有的类称为基类或父类, 产生的新类称为派生类或子类 继承有何意义 继承使程序的设计更符合发展规律, 即事物的发展是一个从低级到高级的发展过程, 类的继承也是反映由原始的简单代码到丰富的高级代码这一发展过程 继承能帮助我们描述事物的层次关系 有效而精确的理解事物直至抓住事物的本质 为什么要继承 继承提供了在已有类的基础上开发出新类的机制, 可以节省重复代码的编写工作, 是软件重用的基础 这能够提高代码复用率 缩短程序开发成本 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

8 继承的功能继承基类的数据与方法 : 将基类数据成员或成员函数全盘吸收 增加新的成员 改变现有成员的属性 : 重新定义已有成员函数 : 继承的分类 针对派生类而言, 根据基类的个数可分为 : 单一继承和多重继承 单一继承 : 派生类的基类只有一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; 多重继承 : 派生类的基类不少于一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName1>, <AccessKeyword> <BaseClassName2>,..., <AccessKeyword> <BaseClassNameN> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

9 继承的功能继承基类的数据与方法 : 将基类数据成员或成员函数全盘吸收 增加新的成员 改变现有成员的属性 : 重新定义已有成员函数 : 继承的分类 针对派生类而言, 根据基类的个数可分为 : 单一继承和多重继承 单一继承 : 派生类的基类只有一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; 多重继承 : 派生类的基类不少于一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName1>, <AccessKeyword> <BaseClassName2>,..., <AccessKeyword> <BaseClassNameN> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

10 继承的功能继承基类的数据与方法 : 将基类数据成员或成员函数全盘吸收 增加新的成员 改变现有成员的属性 : 继承与访问权限调整等 重新定义已有成员函数 : 继承的分类 针对派生类而言, 根据基类的个数可分为 : 单一继承和多重继承 单一继承 : 派生类的基类只有一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; 多重继承 : 派生类的基类不少于一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName1>, <AccessKeyword> <BaseClassName2>,..., <AccessKeyword> <BaseClassNameN> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

11 继承的功能继承基类的数据与方法 : 将基类数据成员或成员函数全盘吸收 增加新的成员 改变现有成员的属性 : 继承与访问权限调整等 重新定义已有成员函数 : 继承的分类 针对派生类而言, 根据基类的个数可分为 : 单一继承和多重继承 单一继承 : 派生类的基类只有一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; 多重继承 : 派生类的基类不少于一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName1>, <AccessKeyword> <BaseClassName2>,..., <AccessKeyword> <BaseClassNameN> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

12 继承的功能继承基类的数据与方法 : 将基类数据成员或成员函数全盘吸收 增加新的成员 改变现有成员的属性 : 继承与访问权限调整等 重新定义已有成员函数 : 覆盖 ( 重写 ) 和隐藏等技术 继承的分类 针对派生类而言, 根据基类的个数可分为 : 单一继承和多重继承 单一继承 : 派生类的基类只有一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; 多重继承 : 派生类的基类不少于一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName1>, <AccessKeyword> <BaseClassName2>,..., <AccessKeyword> <BaseClassNameN> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

13 继承的功能继承基类的数据与方法 : 将基类数据成员或成员函数全盘吸收 增加新的成员 改变现有成员的属性 : 继承与访问权限调整等 重新定义已有成员函数 : 覆盖 ( 重写 ) 和隐藏等技术 继承的分类 针对派生类而言, 根据基类的个数可分为 : 单一继承和多重继承 单一继承 : 派生类的基类只有一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; 多重继承 : 派生类的基类不少于一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName1>, <AccessKeyword> <BaseClassName2>,..., <AccessKeyword> <BaseClassNameN> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

14 继承的功能继承基类的数据与方法 : 将基类数据成员或成员函数全盘吸收 增加新的成员 改变现有成员的属性 : 继承与访问权限调整等 重新定义已有成员函数 : 覆盖 ( 重写 ) 和隐藏等技术 继承的分类 针对派生类而言, 根据基类的个数可分为 : 单一继承和多重继承 单一继承 : 派生类的基类只有一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; 多重继承 : 派生类的基类不少于一个 其语法格式为 : 1 class <DerivedClass> : <AccessKeyword> <BaseClassName1>, <AccessKeyword> <BaseClassName2>,..., <AccessKeyword> <BaseClassNameN> 3 // Declarations and Definitions 成员声明 : 类似于普通类 4 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

15 三种继承方式 (Access Keywords) 公有继承 (public), 私有继承 (private), 保护继承 (protected) 继承方式在基类中在派生类中在派生类外 public protected private 继承后的访问权限 public 仍为 public 成员 可以直接访问 protected 仍为 protected 成员 不可直接访问 private 不可直接访问 不可直接访问 public 变为 protected 成员 不可直接访问 protected 仍为 protected 成员 不可直接访问 private 不可直接访问 不可直接访问 public 变为 private 成员 不可直接访问 protected 变为 private 成员 不可直接访问 private 不可直接访问 不可直接访问 派生类 : 不可访问基类的私有成员 ; 访问其他成员时, 视具体继承规则而定 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

16 继承的功能 : 继承基类的数据与方法, 全盘吸收基类成员数据和成员函数 1 class CSpaceMeshes 3 public: 4 CSpaceMeshes(); 5 ~CSpaceMeshes(); 6 int Nx; int Ny; 7 double length_x; double length_y; 8 protected: 9 void ReadPara(int & error); 10 }; 11 class CRealWorld : public CSpaceMeshes 1 13 public: 14 CRealWorld(); 15 ~CRealWorld(); 16 protected: 17 double * u_x; double * u_y; // 18 double * rho; double density; // 19 private: 20 void ReadPara(int & error); // 21 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

17 继承的功能 : 继承基类的数据与方法, 全盘吸收基类成员数据和成员函数 1 class CSpaceMeshes 3 public: 4 CSpaceMeshes(); 5 ~CSpaceMeshes(); 6 int Nx; int Ny; 7 double length_x; double length_y; 8 protected: 9 void ReadPara(int & error); 10 }; 11 class CRealWorld : public CSpaceMeshes 1 13 public: 14 CRealWorld(); 15 ~CRealWorld(); 16 protected: 17 double * u_x; double * u_y; // 新定义 18 double * rho; double density; // 新定义 19 private: 20 void ReadPara(int & error); // 新定义 21 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

18 继承的功能 : 利用继承调整可见成员的访问权限 前提 : 对于派生类中的可见 ( 访问 ) 成员, 即基类中的非私有成员 目标 : 在派生类中, 将继承自基类的公有成员调整为私有成员? 在派生类中, 将继承自基类的私有成员调整为公有成员? 1 class BaseClass 3 public: 4 int npublica; 5 protected: 6 int nprotecteda; 7 private: 8 int nprivatea; 9 }; 10 class DerivedClass : public BaseClass 11 { 12 public: 13 using BaseClass::nPrivateA; // 14 using BaseClass::nProtectedA;// 15 private: 16 using BaseClass::nPublicA; // 17 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

19 继承的功能 : 利用继承调整可见成员的访问权限 前提 : 对于派生类中的可见 ( 访问 ) 成员, 即基类中的非私有成员 目标 : 在派生类中, 将继承自基类的公有成员调整为私有成员?Correct! 在派生类中, 将继承自基类的私有成员调整为公有成员? 1 class BaseClass 3 public: 4 int npublica; 5 protected: 6 int nprotecteda; 7 private: 8 int nprivatea; 9 }; 10 class DerivedClass : public BaseClass 11 { 12 public: 13 using BaseClass::nPrivateA; // 14 using BaseClass::nProtectedA;// 正确, 父类受保护成员变公有成员 15 private: 16 using BaseClass::nPublicA; // 正确, 父类公有成员变私有成员 17 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

20 继承的功能 : 利用继承调整可见成员的访问权限 前提 : 对于派生类中的可见 ( 访问 ) 成员, 即基类中的非私有成员 目标 : 在派生类中, 将继承自基类的公有成员调整为私有成员?Correct! 在派生类中, 将继承自基类的私有成员调整为公有成员?No Way! 1 class BaseClass 3 public: 4 int npublica; 5 protected: 6 int nprotecteda; 7 private: 8 int nprivatea; 9 }; 10 class DerivedClass : public BaseClass 11 { 12 public: 13 using BaseClass::nPrivateA; // 错误, 父类私有成员在子类不可见 14 using BaseClass::nProtectedA;// 正确, 父类受保护成员变公有成员 15 private: 16 using BaseClass::nPublicA; // 正确, 父类公有成员变私有成员 17 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

21 继承的功能 : 重新定义已有成员函数, 同名隐藏 1 class CLBFlowBGK 3 public: 4 CLBFlowBGK(); ~CLBFlowBGK(); 5 void Propagate(); // 迁移 6 void Bounceback(); // 反弹 7 void Relaxation(); // 松弛, 8 protected: 9 double delta_x; 10 double delta_t; } 13 class CLBFlowMRT : public CLBFlowBGK 14 { 15 public: 16 CLBFlowMRT(); ~CLBFlowMRT(); 17 void Relaxation(); // 松弛, 18 private: 19 double Sdiag[QF][QF]; // }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

22 继承的功能 : 重新定义已有成员函数, 同名隐藏 1 class CLBFlowBGK 3 public: 4 CLBFlowBGK(); ~CLBFlowBGK(); 5 void Propagate(); // 迁移 6 void Bounceback(); // 反弹 7 void Relaxation(); // 松弛, 无 virtual 修饰 8 protected: 9 double delta_x; 10 double delta_t; } 13 class CLBFlowMRT : public CLBFlowBGK 14 { 15 public: 16 CLBFlowMRT(); ~CLBFlowMRT(); 17 void Relaxation(); // 松弛, 同名隐藏 : 同名同参且基类的无 virtual 修饰 18 private: 19 double Sdiag[QF][QF]; // 新定义 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

23 继承的功能 : 重新定义已有成员函数, 函数覆盖 1 class CLBFlowBGK 3 public: 4 CLBFlowBGK(); ~CLBFlowBGK(); 5 void Propagate(); // 迁移 6 void Bounceback(); // 反弹 7 virtual void Relaxation(); // 松弛, 8 protected: 9 double delta_x; 10 double delta_t; } 13 class CLBFlowMRT : public CLBFlowBGK 14 { 15 public: 16 CLBFlowMRT(); ~CLBFlowMRT(); 17 void Relaxation(); // 松弛, 18 private: 19 double Sdiag[QF][QF]; // }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

24 继承的功能 : 重新定义已有成员函数, 函数覆盖 1 class CLBFlowBGK 3 public: 4 CLBFlowBGK(); ~CLBFlowBGK(); 5 void Propagate(); // 迁移 6 void Bounceback(); // 反弹 7 virtual void Relaxation(); // 松弛, 有 virtual 修饰 8 protected: 9 double delta_x; 10 double delta_t; } 13 class CLBFlowMRT : public CLBFlowBGK 14 { 15 public: 16 CLBFlowMRT(); ~CLBFlowMRT(); 17 void Relaxation(); // 松弛, 函数覆盖 : 同名同参且基类的有 virtual 修饰 18 private: 19 double Sdiag[QF][QF]; // 新定义 }; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

25 设计继承的法则, 我们换一种方式理解成员权限的定义 : 1 首先, 根据物品的使用权限, 把父母的东西分为三类 只能父母使用, 子女不可以使用 父母子女使用, 外人不可以使用 父母子女使用, 外人也可以使用 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

26 设计继承的法则, 我们换一种方式理解成员权限的定义 : 1 首先, 根据物品的使用权限, 把父母的东西分为三类 2 然后, 根据物品分类规则, 划分内容 ( 变量和函数 ) 的访问权限 只能父母使用, 子女不可以使用 : 把这部分内容定义为 private 父母子女使用, 外人不可以使用 : 把这部分内容定义为 protected 父母子女使用, 外人也可以使用 : 把这部分内容定义为 public Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

27 设计继承的法则, 我们换一种方式理解成员权限的定义 : 1 首先, 根据物品的使用权限, 把父母的东西分为三类 2 然后, 根据物品分类规则, 划分内容 ( 变量和函数 ) 的访问权限 3 最后, 根据继承方式, 对父母物品的访问类型进一步约束 只能父母使用, 子女不可以使用 : 把这部分内容定义为 private 父母子女使用, 外人不可以使用 : 把这部分内容定义为 protected 父母子女使用, 外人也可以使用 : 把这部分内容定义为 public 进一步约束, 例如 :class DerivedClass : public BaseClass Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

28 设计继承的法则, 我们换一种方式理解成员权限的定义 : 1 首先, 根据物品的使用权限, 把父母的东西分为三类 2 然后, 根据物品分类规则, 划分内容 ( 变量和函数 ) 的访问权限 3 最后, 根据继承方式, 对父母物品的访问类型进一步约束 只能父母使用, 子女不可以使用 : 把这部分内容定义为 private 父母子女使用, 外人不可以使用 : 把这部分内容定义为 protected 父母子女使用, 外人也可以使用 : 把这部分内容定义为 public 进一步约束, 例如 :class DerivedClass : public BaseClass 最终, 形成对父母物品继承后的访问法则 : 无论哪种继承方式, 继承方式对子女能访问父母的哪些物品 不能访问哪些物品是没有限制的, 继承方式是为了限制后来的继承 也就是说 : Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

29 设计继承的法则, 我们换一种方式理解成员权限的定义 : 1 首先, 根据物品的使用权限, 把父母的东西分为三类 2 然后, 根据物品分类规则, 划分内容 ( 变量和函数 ) 的访问权限 3 最后, 根据继承方式, 对父母物品的访问类型进一步约束 只能父母使用, 子女不可以使用 : 把这部分内容定义为 private 父母子女使用, 外人不可以使用 : 把这部分内容定义为 protected 父母子女使用, 外人也可以使用 : 把这部分内容定义为 public 进一步约束, 例如 :class DerivedClass : public BaseClass 最终, 形成对父母物品继承后的访问法则 : 无论哪种继承方式, 继承方式对子女能访问父母的哪些物品 不能访问哪些物品是没有限制的, 继承方式是为了限制后来的继承 也就是说 : 无论哪种继承方式, 派生类成员都不可访问基类的私有成员, 但可以访问基类的保护成员和公有成员 ; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

30 设计继承的法则, 我们换一种方式理解成员权限的定义 : 1 首先, 根据物品的使用权限, 把父母的东西分为三类 2 然后, 根据物品分类规则, 划分内容 ( 变量和函数 ) 的访问权限 3 最后, 根据继承方式, 对父母物品的访问类型进一步约束 只能父母使用, 子女不可以使用 : 把这部分内容定义为 private 父母子女使用, 外人不可以使用 : 把这部分内容定义为 protected 父母子女使用, 外人也可以使用 : 把这部分内容定义为 public 进一步约束, 例如 :class DerivedClass : public BaseClass 最终, 形成对父母物品继承后的访问法则 : 无论哪种继承方式, 继承方式对子女能访问父母的哪些物品 不能访问哪些物品是没有限制的, 继承方式是为了限制后来的继承 也就是说 : 无论哪种继承方式, 派生类成员都不可访问基类的私有成员, 但可以访问基类的保护成员和公有成员 ; 当以派生类作为新基类继续派生出新类时, 系统会根据来自于原基类的继承权限控制新派生类访问原基类成员的权限 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

31 基类的私有成员可以被派生类继承, 但不能被派生类直接访问 1 class BaseClass 3 public: int npublica; 4 protected: int nprotecteda; 5 private: int nprivatea; // 私有成员是否被继承并影响了派生类对象的大小? 6 }; 7 class DerivedClass : public BaseClass 8 { 9 private: 10 int nprivateb; 11 }; 12 int main(void) 13 { 14 BaseClass baseobject; 15 DerivedClass deriobject; 16 cout<<"the baseobject size is: "<< sizeof(baseobject) <<";\t"; 17 cout<<"the deriobject size is: "<< sizeof(deriobject) <<endl; 18 return 0; 19 } 程序输出结果 : Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

32 基类的私有成员可以被派生类继承, 但不能被派生类直接访问 1 class BaseClass 3 public: int npublica; 4 protected: int nprotecteda; 5 private: int nprivatea; // 私有成员是否被继承并影响了派生类对象的大小? 6 }; 7 class DerivedClass : public BaseClass 8 { 9 private: 10 int nprivateb; 11 }; 12 int main(void) 13 { 14 BaseClass baseobject; 15 DerivedClass deriobject; 16 cout<<"the baseobject size is: "<< sizeof(baseobject) <<";\t"; 17 cout<<"the deriobject size is: "<< sizeof(deriobject) <<endl; 18 return 0; 19 } 程序输出结果 :The size of baseobject: 12; The size of deriobject: 16. Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

33 基类的私有成员不能被派生类直接访问, 但可通过基类的公有函数访问 1 class Person int age; 3 public: 4 Person() { age = 21; } 5 int GetAge() { return age; } // 基类的公有函数 6 }; 7 class Student : public Person 8 { 9 int score; 10 public: 11 Student() { score = 60; } 12 int GetScore(){ return score; } // 派生类的公有函数 13 }; 14 int main(void) 15 { 16 Person man; Student boy; 17 cout<<"age="<<boy.getage()<<"\t Score="<<boy.GetScore()<<endl; 18 return 0; 19 } 程序输出结果 : Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

34 基类的私有成员不能被派生类直接访问, 但可通过基类的公有函数访问 1 class Person int age; 3 public: 4 Person() { age = 21; } 5 int GetAge() { return age; } // 基类的公有函数 6 }; 7 class Student : public Person 8 { 9 int score; 10 public: 11 Student() { score = 60; } 12 int GetScore(){ return score; } // 派生类的公有函数 13 }; 14 int main(void) 15 { 16 Person man; Student boy; 17 cout<<"age="<<boy.getage()<<"\t Score="<<boy.GetScore()<<endl; 18 return 0; 19 } 程序输出结果 :Age=21 Score=60 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

35 派生类可以把基类中除构造函数和析构函数外的所有成员都继承过来 1 class base 3 private: int x; 4 public: 5 base(int xloc=0):x(xloc) 6 { cout<<"base xloc \n"; } 7 ~base() { } 8 }; 9 class derive: public base 10 { 11 public: 12 derive() 13 { 14 base(1); // 函数调用 15 } 16 } 在派生类的构造函数中可以直接调用基类的构造函数, 但不应该视这种调用为是继承 这只是单纯的函数调用, 而不是继承 在派生类的析构函数执行结束后, 系统也会自动调用基类的析构函数 这本质上也是函数调用, 而不是继承 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

36 派生类可以把基类中除构造函数和析构函数外的所有成员都继承过来 1 class base 3 private: int x; 4 public: 5 base(int xloc=0):x(xloc) 6 { cout<<"base xloc \n"; } 7 ~base() { } 8 }; 9 class derive: public base 10 { 11 public: 12 derive() 13 { 14 base(1); // 函数调用 15 } 16 } 在派生类的构造函数中可以直接调用基类的构造函数, 但不应该视这种调用为是继承 这只是单纯的函数调用, 而不是继承 在派生类的析构函数执行结束后, 系统也会自动调用基类的析构函数 这本质上也是函数调用, 而不是继承 构造函数 this 指针与函数继承通常 this 指针在对象构造完毕 内存分配完成后才能产生 ; 在构造函数执行过程中, 对象尚未实例化 还没有内存空间, 此时不存在 this 指针 由于构造函数没有 this 指针, 所以基类的构造函数并没有被派生类继承 : 由派生类调用基类构造函数实际上和直接调用基类的构造函数没有区别, 这不是真正意义的继承 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

37 温故知新派生类可以把基类中除构造函数和析构函数外的所有成员都继承过来 1 class this 指针是类的非静态成员函数中一个被隐藏的参数 base 9 class derive:, public 指向被调用函数所 base 在的对象 ; 10 { 3 private: int x; 11 public: 4 public: 程序编译时, 编译器把调用类的非静态成员函数之实例的地址赋给这个被 12 derive() 5 隐藏的参数 base(int; xloc=0):x(xloc) 13 { 6 { cout<<"base xloc \n"; } 14 base(1); // 函数调用全局仅有一个 this 指针, 当一个对象被创建时,this 指针就存放指向对象 7 ~base() { } 15 } 8 }; 数据的首地址 ; 16 } 只有获得一个对象在派生类的构造函数中可以直接调用基类的构造函数 ( 类的实例 ) 后, 才能也只能在类的非静态成员函数中, 但不应该视这种调用为是继承 这只是单纯的函数调用使用 this 指针, 而不是继承 在派生类的析构函数执行结束后, 系统也会自动调用基类的析构函数 这本质上也是函数调用, 而不是继承 构造函数 this 指针与函数继承通常 this 指针在对象构造完毕 内存分配完成后才能产生 ; 在构造函数执行过程中, 对象尚未实例化 还没有内存空间, 此时不存在 this 指针 由于构造函数没有 this 指针, 所以基类的构造函数并没有被派生类继承 : 由派生类调用基类构造函数实际上和直接调用基类的构造函数没有区别, 这不是真正意义的继承 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

38 温故知新派生类可以把基类中除构造函数和析构函数外的所有成员都继承过来 1 class this 指针是类的非静态成员函数中一个被隐藏的参数 base 9 class derive:, public 指向被调用函数所 base 在的对象 ; 10 { 3 private: int x; 11 public: 4 public: 程序编译时, 编译器把调用类的非静态成员函数之实例的地址赋给这个被 12 derive() 5 隐藏的参数 base(int; xloc=0):x(xloc) 13 { 6 { cout<<"base xloc \n"; } 14 base(1); // 函数调用全局仅有一个 this 指针, 当一个对象被创建时,this 指针就存放指向对象 7 ~base() { } 15 } 8 }; 数据的首地址 ; 16 } 只有获得一个对象在派生类的构造函数中可以直接调用基类的构造函数 ( 类的实例 ) 后, 才能也只能在类的非静态成员函数中, 但不应该视这种调用为是继承 这只是单纯的函数调用使用 this 指针, 而不是继承 知识扩展在派生类的析构函数执行结束后, 系统也会自动调用基类的析构函数 这本质上也是函数调用, 而不是继承 this is a pointer containing the address to the "current object". It is not a 构造函数 variable that this is 指针与函数继承 stored somewhere (or could even be changed), it is a special keyword 通常 this 指针在对象构造完毕 内存分配完成后才能产生 with these properties. ; 在构造函数执行过程中, 对象尚未实例化 还没有内存空间, 此时不存在 this 指针 Taking the address of this makes no sense. If you want to know the address of 由于构造函数没有 the "current object" this 指针 you, can 所以基类的构造函数并没有被派生类继承 simply output: std::cout «this; or store : as 由派生类调用基类构造函数实际上和直接调用基类的构造函数没有区别 void * a = this;., 这不是真正意义的继承 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

39 扩展 : 基类的构造函数能被派生类继承吗?( 继承构造函数 ) 1 class C { 2 public: 3 C() { cout << "constructor: C" << endl; } 4 explicit C(int c){ cout << "constructor: C, c = " << c << endl; } 5 virtual ~C() { cout << "destructor: C" << endl; } 6 }; 7 class D: public C { 8 public: 9 using C::C; // Inheriting Constructors (C++11 support) 10 ~D() { cout << "destructor: D" << endl; } 11 }; 12 int main() 13 { 14 D d1; // C() >D() > D() > C() 调用被 继承 的构造函数 D() 15 D d2(1); // C(int) >D(int) > D() > C() 调用被 继承 的构造函数 D(int) 16 return 0; 17 } 在类 D 中并没有定义形如 D(int) 的构造函数, 在产生 D 的对象时调用了 继承 自类 C 的构造函数 提示 : 关键词 explicit 禁止隐式调用类内的单参数构造函数 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

40 程序运行结果扩展 : 基类的构造函数能被派生类继承吗?( 继承构造函数 ) constructor: C 1 class C { constructor: 2 public: C, c = 1 destructor: 3 C() D { cout << "constructor: C" << endl; } destructor: 4 explicitcc(int c){ cout << "constructor: C, c = " << c << endl; } 5 destructor: virtual ~C() D { cout << "destructor: C" << endl; } 6 }; destructor: 7 class D: public C C { 8 public: 9 using C::C; // Inheriting Constructors (C++11 support) 10 ~D() { cout << "destructor: D" << endl; } 11 }; 12 int main() 13 { 14 D d1; // C() >D() > D() > C() 调用被 继承 的构造函数 D() 15 D d2(1); // C(int) >D(int) > D() > C() 调用被 继承 的构造函数 D(int) 16 return 0; 17 } 在类 D 中并没有定义形如 D(int) 的构造函数, 在产生 D 的对象时调用了 继承 自类 C 的构造函数 提示 : 关键词 explicit 禁止隐式调用类内的单参数构造函数 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

41 扩展 : 基类的构造函数能被派生类继承吗?( 委托构造函数 ) 1 class A { 2 int a; 3 public: 4 A() { cout << "constructor: A" << endl; } 5 A(int i) : a(i) { cout << "constructor: A, a = " << a << endl; } 6 virtual ~A() { cout << "destructor: A" << endl; } 7 }; 8 class B : public A { 9 int b; 10 public: 11 B() { cout << "constructor: B" << endl; } 12 B(int j) : b(j) { cout << "constructor: B, b = " << b << endl; } 13 B(int i, int j): A(i), b(j) { cout << "constructor: B, a = " << a << ", b = " << b << endl; } 14 ~B() { cout << "destructor: B" << ::std::endl; } 15 }; 16 class C { 17 public: 18 C() { cout << "constructor: C" << endl; } 19 explicit C(int c){ cout << "constructor: C, c = " << c << endl; } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

42 21 virtual ~C() { cout << "destructor: C" << endl; } 22 }; 23 class D : public C { 24 public: 25 using C::C; // inheriting constructors (C++11 support) 26 ~D() { cout << "destructor: D" << endl; } 27 }; 28 int main() 29 { 30 B b1; // A() >B() > B() > A() 隐式调用直接基类的无参构造函数 31 B b2(2); // A() >B(int) > B() > A() 隐式调用直接基类的无参构造函数 32 B b3(1, 2); // A(int) > B(int, int) > B() > A() 调用委托构造函数 D d1; // C() >D() > D() > C() 调用被 继承 的构造函数 D() 35 D d2(1); // C(int) >D(int) > D() > C() 调用被 继承 的构造函 数 D(int) 36 return 0; 37 } 在类 D 中并没有定义形如 D(int) 的构造函数, 在产生 D 的对象时调用了 继承 自类 C 的构造函数 提示 : 关键词 explicit 禁止隐式调用类内的单参数构造函数 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

43 程序运行结果 21 virtual ~C() { cout << "destructor: C" << endl; } constructor: A 22 }; constructor: 23 class D : public B C { constructor: 24 public: A constructor: 25 using C::C; B, b = // 2 inheriting constructors (C++11 support) 26 ~D() { cout << "destructor: D" << endl; } constructor: A, a = 1 27 }; constructor: 28 int main() B, a = 1, b = 2 constructor: 29 { C 30 constructor: B b1; C, // c = A() >B() > B() > A() 1 隐式调用直接基类的无参构造函数 31 B b2(2); // A() >B(int) > B() > A() 隐式调用直接基类的无参构造函数 destructor: D 32 B b3(1, 2); // A(int) > B(int, int) > B() > A() 调用委托构造函数 destructor: 33 C destructor: 34 D d1; D // C() >D() > D() > C() 调用被 继承 的构造函数 D() 35 destructor: D d2(1); C // C(int) >D(int) > D() > C() 调用被 继承 的构造函数 D(int) destructor: 36 return 0; B destructor: 37 } A destructor: B 在类 D 中并没有定义形如 D(int) 的构造函数, 在产生 D 的对象时调用了 继 destructor: A 承 自类 C 的构造函数 destructor: B 提示 : 关键词 explicit 禁止隐式调用类内的单参数构造函数 destructor: A Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

44 基类的构造函数能被派生类继承吗? 构造函数不能被继承, 如果派生类不显式调用基类的构造函数, 编译器就会自动调用基类的无参构造函数 ; Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

45 基类的构造函数能被派生类继承吗? 构造函数不能被继承, 如果派生类不显式调用基类的构造函数, 编译器就会自动调用基类的无参构造函数 ; 构造函数不能为 virtual 函数, 而析构函数可以是 virtual 函数, 详见 虚 (virtual) 函数 章节 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

46 基类的构造函数能被派生类继承吗? 构造函数不能被继承, 如果派生类不显式调用基类的构造函数, 编译器就会自动调用基类的无参构造函数 ; 构造函数不能为 virtual 函数, 而析构函数可以是 virtual 函数, 详见 虚 (virtual) 函数 章节 C++11 支持继承构造函数 (inheriting constructors); 这种 继承 实质上是编译器自动生成调用基类构造函数的代码, 不是真正意义上的继承 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

47 基类的构造函数能被派生类继承吗? 构造函数不能被继承, 如果派生类不显式调用基类的构造函数, 编译器就会自动调用基类的无参构造函数 ; 构造函数不能为 virtual 函数, 而析构函数可以是 virtual 函数, 详见 虚 (virtual) 函数 章节 C++11 支持继承构造函数 (inheriting constructors); 这种 继承 实质上是编译器自动生成调用基类构造函数的代码, 不是真正意义上的继承 请记住并理解这些术语 继承 (inheritance): 由一个已有的类 ( 基类 ) 定义一个新类 ( 派生类 ) 的编程技术 派生类将继承基类的公有 私有和保护类型的数据成员 基类 (base class): 可供其他类继承的类 基类的成员也将成为派生类的成员, 其在派生类中的访问权限视继承规则而定 派生 (derived class): 从其他类派生而来的类 派生类可以覆盖其基类的函数, 也可以定义自己的新成员 派生类的作用域嵌套在基类的作用域中 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

48 基类的构造函数能被派生类继承吗? 构造函数不能被继承, 如果派生类不显式调用基类的构造函数, 编译器就会自动调用基类的无参构造函数 ; 构造函数不能为 virtual 函数, 而析构函数可以是 virtual 函数, 详见 虚 (virtual) 函数 章节 C++11 支持继承构造函数 (inheriting constructors); 这种 继承 实质上是编译器自动生成调用基类构造函数的代码, 不是真正意义上的继承 请记住并理解这些术语 继承 (inheritance): 由一个已有的类 ( 基类 ) 定义一个新类 ( 派生类 ) 的编程技术 派生类将继承基类的公有 私有和保护类型的数据成员 基类 (base class): 可供其他类继承的类 基类的成员也将成为派生类的成员, 其在派生类中的访问权限视继承规则而定 派生 (derived class): 从其他类派生而来的类 派生类可以覆盖其基类的函数, 也可以定义自己的新成员 派生类的作用域嵌套在基类的作用域中 通过继承, 我们可以编写一些新的类, 这些类既能共享基类的方法 ( 成员函数 ), 还能根据需要调整 覆盖或增加属于派生类的方法 ( 成员函数 ) Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

49 提纲 单一继承 1 2 单一继承 3 多重继承 4 基类成员的初始化 5 总结与思考 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

50 单一继承 : 公有继承 单一继承 公有继承 ( 派生 ) 1 class A }; 5 class B : public A // B 公有继承了 A 6 { }; 类 A 中的 public 和 protected 成员, 在 B 中仍然保持其原访问特性 虽然 A 中的私有成员也被继承下来了, 但在 B 中不可直接访问 Table: 类成员的访问特性 类 A 中成员在类 B 中在 B 外 ( 通过 B 的对象 ) public 仍为 public 成员 可以直接访问 protected 仍为 protected 成员 不可直接访问 private 不可直接访问 不可直接访问 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

51 单一继承 : 公有继承 单一继承 A Person class and A BaseballPlayer class 1 class Person 3 public: 4 std::string m_name; 5 int m_age; 6 7 Person(std::string name = "", int age = 0) 8 : m_name(name), m_age( age) 9 { } 10 std::string getname() const 11 { return m_name; } 12 int getage() const 13 { return m_age; } 14 }; 1 class BaseballPlayer 3 public: 4 double m_battingaverage; 5 int m_homeruns; 6 7 BaseballPlayer(double battingaverage = 0.0, int homeruns = 0) 8 : m_battingaverage( battingaverage), m_homeruns( homeruns) 9 { 10 // code here please 11 } 12 }; Now, we also want to keep track of a baseball player s name and age, and we already have that information as part of our Person class. Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

52 单一继承 : 公有继承 单一继承 A Person class and A BaseballPlayer class We have three choices for how to add name and age to BaseballPlayer: Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

53 单一继承 : 公有继承 单一继承 A Person class and A BaseballPlayer class We have three choices for how to add name and age to BaseballPlayer: Add name and age to the BaseballPlayer class directly as members. This is probably the worst choice, as we re duplicating code that already exists in our Person class. Any updates to Person will have to be made in BaseballPlayer too. Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

54 单一继承 : 公有继承 单一继承 A Person class and A BaseballPlayer class We have three choices for how to add name and age to BaseballPlayer: Add name and age to the BaseballPlayer class directly as members. This is probably the worst choice, as we re duplicating code that already exists in our Person class. Any updates to Person will have to be made in BaseballPlayer too. Add Person as a member of BaseballPlayer using composition. But we have to ask ourselves, "Does a BaseballPlayer have-a Person?" No, they don t. So this isn t the right paradigm. Have BaseballPlayer inherit those attributes from Person. Remember that inheritance represents an is-a relationship. Is a BaseballPlayer a Person? Yes, they are. So inheritance is a good choice here. Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

55 单一继承 : 公有继承 单一继承 A Person class and A BaseballPlayer class We have three choices for how to add name and age to BaseballPlayer: Add name and age to the BaseballPlayer class directly as members. This is probably the worst choice, as we re duplicating code that already exists in our Person class. Any updates to Person will have to be made in BaseballPlayer too. Add Person as a member of BaseballPlayer using composition. But we have to ask ourselves, "Does a BaseballPlayer have-a Person?" No, they don t. So this isn t the right paradigm. Have BaseballPlayer inherit those attributes from Person. Remember that inheritance represents an is-a relationship. Is a BaseballPlayer a Person? Yes, they are. So inheritance is a good choice here. Figure: Derivation diagram/chart Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

56 单一继承 : 公有继承 单一继承 Making BaseballPlayer a derived class To have BaseballPlayer inherit from our Person class, the syntax is fairly simple. After the class BaseballPlayer declaration, we use a colon, the word public, and the name of the class we wish to inherit. This is called public inheritance. 1 class BaseballPlayer : public Person // BaseballPlayer publicly inheriting Person 3 public: 4 double m_battingaverage; 5 int m_homeruns; 6 BaseballPlayer(double battingaverage = 0.0, int homeruns = 0) 7 : m_battingaverage(battingaverage), m_homeruns(homeruns) 8 { 9 } 10 }; When BaseballPlayer inherits from Person, BaseballPlayer receives the member functions and variables from Person. Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

57 单一继承 : 公有继承 单一继承 Making BaseballPlayer a derived class To have BaseballPlayer inherit from our Person class, the syntax is fairly simple. After the class BaseballPlayer declaration, we use a colon, the word public, and the name of the class we wish to inherit. This is called public inheritance. 1 class BaseballPlayer : public Person // BaseballPlayer publicly inheriting Person 3 public: 4 double m_battingaverage; 5 int m_homeruns; 6 BaseballPlayer(double battingaverage = 0.0, int homeruns = 0) 7 : m_battingaverage(battingaverage), m_homeruns(homeruns) 8 { 9 } 10 }; When BaseballPlayer inherits from Person, BaseballPlayer receives the member functions and variables from Person. Thus, BaseballPlayer objects willhave 4 member variables: m_battingaverage and m_homeruns from BaseballPlayer, and m_name and m_age from Person. Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

58 单一继承 : 公有继承 单一继承 Making BaseballPlayer a derived class 1 class Person { 2 public: 3 string m_name; int m_age; 4 Person(string name = "", int age = 0) : m_name(name), m_age(age){ } 5 std::string getname( ) const { return m_name; } 6 int getage ( ) const { return m_age; } 7 }; 8 class BaseballPlayer : public Person { // publicly inheriting Person 9 public: 10 double m_battingaverage; int m_homeruns; 11 BaseballPlayer(double battingaverage = 0.0, int homeruns = 0) 12 : m_battingaverage(battingaverage), m_homeruns(homeruns) { } 13 }; 14 int main() { 15 BaseballPlayer joe; // Create a new object 16 joe.m_name = "Joe"; // Assign it a name (m_name is public) 17 std::cout << joe.getname() << \n ; // Print out the name 18 return 0; 19 } Which prints the value: Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

59 单一继承 : 公有继承 单一继承 Making BaseballPlayer a derived class 1 class Person { 2 public: 3 string m_name; int m_age; 4 Person(string name = "", int age = 0) : m_name(name), m_age(age){ } 5 std::string getname( ) const { return m_name; } 6 int getage ( ) const { return m_age; } 7 }; 8 class BaseballPlayer : public Person { // publicly inheriting Person 9 public: 10 double m_battingaverage; int m_homeruns; 11 BaseballPlayer(double battingaverage = 0.0, int homeruns = 0) 12 : m_battingaverage(battingaverage), m_homeruns(homeruns) { } 13 }; 14 int main() { 15 BaseballPlayer joe; // Create a new object 16 joe.m_name = "Joe"; // Assign it a name (m_name is public) 17 std::cout << joe.getname() << \n ; // Print out the name 18 return 0; 19 } Which prints the value: Joe Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

60 单一继承 : 公有继承 单一继承 An Employee derived class We ll write an Employee class. An employee "is-a" person, so using inheritance is appropriate: 1 class Employee: public Person // Employee publicly inherits from Person 3 public: 4 double m_hourlysalary; 5 long m_employeeid; 6 Employee(double hourlysalary, long employeeid) 7 : m_hourlysalary(hourlysalary), m_employeeid(employeeid) 8 { 9 } 10 void printnameandsalary() const 11 { 12 std::cout << m_name << ": " << m_hourlysalary << \n ; 13 } 14 }; The printnameandsalary() uses variables both from the class it belongs to Employee::m_hourlySalary and the parent class Person::m_name. Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

61 单一继承 : 公有继承 单一继承 有继承关系如下 : 1 class A 3 private: int ax; 4 protected: int ay; 5 public: int az; 6 void f1( ) {... } 7 int Getax( ) {return ax;} 8 void Setax(int x) {ax = x;} 9 }; 10 class B: public A 11 { 12 private: int bx; 13 protected: int by; 14 public: int bz; 15 void f2( ){...} 16 float f3( ){...} 17 }; 相当于 ( 但不是 ) 对 B 类做如下定义 : 1 class B 3 private: int bx; 4 protected: int ay; // 继承 5 int by; 6 public: int az; // 继承 7 int bz; 8 void f1( ) // 继承 9 {... } 10 int Getax( ) // 继承 11 {return ax;} 12 void Setax(int x) // 继承 13 {ax = x; } 14 void f2( ){... } 15 float f3( ){... } }; 在类 B 的成员函数中可直接访问类 A 的数据成员变量 ay az 和成员函数 f1( ) Getax( ) Setax( ) 在类 B 中不可直接访问 A 类的私有成员 ax Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

62 单一继承 : 公有继承 单一继承 单一继承 : 先定义 点 类 Point, 再由 点 类派生出 圆 类 Circle 1 #define PI class Point{ 3 int x, y; 4 public: 5 Point(int a=0,int b=0){x=a;y=b;} 6 void ShowPoint( ) 7 {cout<<"p:("<<x<<, <<y<<")\n";} 8 int Getx(){ return x; } 9 int Gety(){ return y; } 10 void SetP(int a,int b){x=a;y=b;} 11 }; 12 class Circle: public Point{ 13 int r; 14 public: 15 Circle(int x, int y, int ra) : Point(x, y){ r = ra; } 16 void SetR(int ra){ r = ra; } 17 double Area(){ return PI*r*r; } 18 void Move(int x_offset, int y_offset) 19 { int x1=getx(); x1+=x_offset; 20 int y1=gety(); y1+=y_offset; 21 SetP(x1, y1); } 22 void ShowCircle( ) 23 { ShowPoint( ); 24 cout<<"r:"<<r<< \t ; 25 cout<<"a: "<<Area()<<endl; } 26 }; 27 int main(){ 28 Circle c(0,0,2); 29 c.showcircle(); 30 c.move(2, 2); 31 c.showcircle(); 32 c.setp(0, 0); c.setr(1); 33 c.showcircle(); 34 return 0; 35 } 请在 CodeBlocks 中运行 InheritanceCircle.cpp Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

63 单一继承 : 私有继承 单一继承 私有继承的格式 1 class A }; 5 class B : private A // B 私有继承了 A 6 { } 将 A 中的 public 和 protected 成员均变成了 B 的 private 成员 同理,A 中的私有成员被继承下来了, 但在 B 中不可直接访问 Table: 类成员的访问特性 类 A 中成员在类 B 中在 B 外 ( 通过 B 的对象 ) public 变为 private 成员 不可直接访问 protected 变为 private 成员 不可直接访问 private 不可直接访问 不可直接访问 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

64 单一继承 : 私有继承 单一继承 有继承关系如下 : 1 class A 3 private: int ax; 4 protected: int ay; 5 public: int az; 6 void f1( ){... } 7 int Getax( ) 8 { return ax; } 9 void Setax( int x ) 10 { ax = x; } 11 }; 12 class B : private A 13 { 14 private: int bx; 15 protected: int by; 16 public: int bz; 17 void f2( ){...} 18 float f3( ){...} 19 }; 相当于 ( 但不是 ) 对 B 类做如下定义 : 1 class B 3 private: int bx; 4 int ay; // 继承 5 int az; // 继承 6 void f1( ){... } // 继承 7 int Getax( ) // 继承 8 { return ax; } 9 void Setax( int x ) // 继承 10 { ax = x; } 11 protected: int by; 12 public: int bz; 13 void f2( ){...} 14 float f3( ){...} 15 }; 在 B 的成员函数中可直接访问 A 类的数据成员变量 ay az 和成员函数 f1( ) Getax( ) Setax( ) 在类 B 中不可直接访问 A 类的私有成员 ax, 但可通过 Getax( ) Setax( ) 访问 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

65 单一继承 : 私有继承 单一继承 有继承关系如下 : 1 class A 3 private: int ax; 4 protected: int ay; 5 public: int az; 6 void f1( ){... } 7 int Getax( ) 8 { return ax; } 9 void Setax( int x ) 10 { ax = x; } 11 }; 12 class B : private A 13 { 14 private: int bx; 15 protected: int by; 16 public: int bz; 17 void f2( ){...} 18 float f3( ){...} 19 }; 相当于 ( 但不是 ) 对 B 类做如下定义 : 1 class B 3 private: int bx; 4 int ay; // 继承 5 int az; // 继承 6 void f1( ){... } // 继承 7 int Getax( ) // 继承 8 { return ax; } 9 void Setax( int x ) // 继承 10 { ax = x; } 11 protected: int by; 12 public: int bz; 13 void f2( ){...} 14 float f3( ){...} 15 }; 在 B 的成员函数中可直接访问 A 类的数据成员变量 ay az 和成员函数 f1( ) Getax( ) Setax( ) 在类 B 中不可直接访问 A 类的私有成员 ax, 但可通过 Getax( ) Setax( ) 访问 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

66 单一继承 : 私有继承 单一继承 有继承关系如下 : 1 class A 3 private: int ax; 4 protected: int ay; 5 public: int az; 6 void f1( ){... } 7 int Getax( ) 8 { return ax; } 9 void Setax( int x ) 10 { ax = x; } 11 }; 12 class B : private A 13 { 14 private: int bx; 15 protected: int by; 16 public: int bz; 17 void f2( ){...} 18 float f3( ){...} 19 }; 相当于 ( 但不是 ) 对 B 类做如下定义 : 1 class B 3 private: int bx; 4 int ay; // 继承 5 int az; // 继承 6 void f1( ){... } // 继承 7 int Getax( ) // 继承 8 { return ax; } 9 void Setax( int x ) // 继承 10 { ax = x; } 11 protected: int by; 12 public: int bz; 13 void f2( ){...} 14 float f3( ){...} 15 }; 在 B 的成员函数中可直接访问 A 类的数据成员变量 ay az 和成员函数 f1( ) Getax( ) Setax( ) 在类 B 中不可直接访问 A 类的私有成员 ax, 但可通过 Getax( ) Setax( ) 访问 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

67 单一继承 : 保护继承 单一继承 保护继承的格式 1 class A }; 5 class B : protected A // B 保护 继承了 A 6 { } 将 A 中的 public 和 protected 成员均变成了 B 的 protected 成员 同理,A 中的私有成员被继承下来了, 但在 B 中不可直接访问 Table: 类成员的访问特性 类 A 中成员在类 B 中在 B 外 ( 通过 B 的对象 ) public 变为 protected 成员 不可直接访问 protected 仍为 protected 成员 不可直接访问 private 不可直接访问 不可直接访问 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

68 单一继承 : 小结 单一继承 无论何种继承方式, 私有成员都无法在派生类中被直接访问 ; 根据派生方式的不同, 保护成员的访问特性可以被 ( 或不被 ) 传递到派生类中 : 对于公有继承 ( 派生 ), 基类的 protected 成员在派生类中依然保持 prote cted 属性 对于保护继承 ( 派生 ), 基类的 protected 成员在派生类中依然保持 prote cted 属性 对于私有继承 ( 派生 ), 基类的 protected 成员在派生类中变成了 private 成员, 此时基类的 protected 成员就再也无法在派生类中被直接访问了 在继承或派生链中, 一旦出现私有继承, 则父类的成员的类内直接访问特性就无法在后面的派生中传递下去 类的 private 成员只能实现本类的数据隐藏, 不能将其类内直接访问特性传递到派生类中 类的 protected 成员既可以在本类中实现数据隐藏 ( 在类外不可被直接访问 ), 又可以将其类内直接访问特性传递到派生类中 ( 在派生类中可直接访问 ) Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

69 提纲 多重继承 1 2 单一继承 3 多重继承 4 基类成员的初始化 5 总结与思考 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

70 多重继承 多重继承 多重继承 : 派生类的基类不少于一个 多重继承的语法格式如下 1 class 派生类名 <>: 继承方式 <1> 基类名 <1>, 继承方式 <2> 基类名 <2>,... 3 private: // 私有成员说明, 派生类新的私有成员 public: // 公有成员说明, 派生类新的公有成员 protected: // 保护成员说明, 派生类新的保护成员 }; 多重继承举例 先定义 点 类 Point 和 半径 类 Radius, 再由 Point 类和 Radius 类派生出 圆 类 Circle 类的继承与派生关系如右图所示 Figure: Derivation diagram/chart Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

71 多重继承 : 例程 多重继承 阅读下面程序代码, 写出运行结果 1 class Point{ 2 protected: 3 int x, y; //A 4 public: 5 Point(int a=0, int b=0) 6 { x=a; y=b; } 7 void ShowPoint( ) 8 { cout<<"p("<<x<<, <<y<<" )\n"; } 9 int Getx( ) { return x; } 10 int Gety( ) { return y; } 11 void Setxy(int a, int b) 1 x=a; y=b; } 13 }; 14 class Radius{ 15 protected: 16 int r; //B 17 public: 18 Radius(int ra=0) { r = ra; } 19 void Setr(int ra){ r = ra; } 20 int Getr( ) {return r;} 21 }; 22 class Circle : public Point, public Radius{ //C 23 public: 24 Circle(int x, int y, int ra): Point(x, y), Radius(ra){ } 25 double Area( ) 26 { return *r*r; } 27 void Move(int xm, int ym) 28 { x+=xm; y+=ym; } 29 void Show( ) 30 { ShowPoint( ); 31 cout<<"r:"<<r<< \t ; 32 cout<<"a:"<<area()<< \n ;} 33 }; 34 int main(){ 35 Circle c(0,0,2); c.show(); 36 c.move(2,2); c.show(); 37 c.setxy(0,0); c.setr(1); 38 c.show(); return 0; 39 } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

72 多重继承 : 例程 多重继承 程序的运行结果为 : P(0,0) R:2 A: P(2,2) R:2 A: P(0,0) R:1 A: 请在 CodeBlocks 中运行 InheritanceCircle2.cpp 请注意这里 : 22 class Circle : public Point, public Radius { //C 多个基类 类 Circle 是由两个基类 Point 和 Radius 派生出来的 在定义派生类 Circle 的构造函数时, 应调用基类 Point 和基类 Radius 的构造函数即 Point(int, int) 和 Radius(int) 以初始化基类的数据成员 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

73 提纲 基类成员的初始化 1 2 单一继承 3 多重继承 4 基类成员的初始化 5 总结与思考 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

74 基类成员的初始化 基类成员的初始化 若一个类是由多个基类派生出来的, 则应在定义派生类构造函数时显式地调用基类的构造函数, 以完成对基类成员的初始化 派生类构造函数的一般格式为 1 ClassName :: ClassName(args) : Base1(arg1), Base2(arg2),..., Basen(argn) 3 派生类自身的构造函数体 <>; 4 }; 其中 ClassName 是派生类名,Base1 Base2...Basen 为基类的类名 args 是派生类构造函数的形参列表,arg1 arg2...argn 是基类构造函数的实参列表 构造函数的调用顺序为 : 5 Base1(arg1); Base2(arg2);...; Basen(argn); 最后执行 < 派生类自身的构造函数体 > 析构函数的调用顺序为构造函数调用顺序的逆序 : 先执行派生类自身的析构函数体, 然后按 6 Basen(argn);...; Base2(arg2); Base1(arg1); 的顺序调用析构函数 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

75 基类成员的初始化 基类成员的初始化 1 class Base1 3 protected: int data1; 4 public: 5 Base1(int a=0) 6 { data1 = a; 7 cout<<"base1 Cons\n"; } 8 ~Base1( ) 9 { cout<<"base1 Dest\n"; } 10 }; 11 class Base protected: int data2; 14 public: 15 Base2(int a=0) 16 { data2 = a; 17 cout<<"base2 Cons\n"; } 18 ~Base2( ) 19 { cout<<"base2 Dest\n"; } 20 }; 21 class Derived : public Base1, public Base2//The Order 2 23 int d; 24 public: 25 Derived(int x, int y, int z) :Base1(x), Base2(y)//Calling 26 { d=z; 27 cout<<"derived Cons\n";} 28 ~Derived( ) 29 { 30 cout<<"derived Dest\n";} 31 void Show( ) 3 33 cout<<data1<<, <<data2 <<, <<d<<endl; 34 } 35 }; 36 int main( ) 37 { 38 Derived c(1, 2, 3); 39 c.show( ); 40 return 0; 41 } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

76 基类成员的初始化 基类成员的初始化 1 class Base1 程序的运行结果 Base1 3 protected: Cons int data1; 4 Base2 public: Cons 5 Base1(int a=0) Derived Cons 6 { data1 = a; 1, 7 2, 3 cout<<"base1 Cons\n"; } Derived 8 Dest ~Base1( ) 9 { cout<<"base1 Dest\n"; } Base2 Dest 10 }; Base1 11 class DestBase protected: int data2; 14 public: 15 Base2(int a=0) 16 { data2 = a; 17 cout<<"base2 Cons\n"; } 18 ~Base2( ) 19 { cout<<"base2 Dest\n"; } 20 }; 21 class Derived : public Base1, public Base2//The Order 2 23 int d; 24 public: 25 Derived(int x, int y, int z) :Base1(x), Base2(y)//Calling 26 { d=z; 27 cout<<"derived Cons\n";} 28 ~Derived( ) 29 { 30 cout<<"derived Dest\n";} 31 void Show( ) 3 33 cout<<data1<<, <<data2 <<, <<d<<endl; 34 } 35 }; 36 int main( ) 37 { 38 Derived c(1, 2, 3); 39 c.show( ); 40 return 0; 41 } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

77 基类成员的初始化 基类成员的初始化 1 class Base1 2 程序的运行结果构造函数的顺序 23 int d; Base1 调用基类的构造函数的顺序并不是 3 protected: Cons int data1; 25 行的书写顺序决定的 24 public:, 而是 21 行定义派生 4 public: 25 Derived(int x, int y, int z) 类时的派生顺序决定的 Base2 Cons, 即使将 38 行改写成 5 Base1(int a=0) :Base1(x), Base2(y)//Calling Derived Cons 6 { data1 = a; 26 { d=z; 25 1, 7 2, 3 Derived(int x, int y, int z) : Base2(y), Base1(x) cout<<"base1 Cons\n"; } 27 cout<<"derived Cons\n";} Derived 8 Dest ~Base1( ) 28 ~Derived( ) 程序的运行结果依然不变 9 { cout<<"base1 Dest\n"; } 29 { Base2 Dest 10 }; 30 cout<<"derived Dest\n";} Base1 请在 CodeBlocks 11 class Dest 中运行 InheritanceConDestructor2.cpp Base2 31 void Show( ) protected: int data2; 33 cout<<data1<<, <<data2 14 public: <<, <<d<<endl; 15 Base2(int a=0) 34 } 16 { data2 = a; 35 }; 17 cout<<"base2 Cons\n"; } 36 int main( ) 18 ~Base2( ) 37 { 19 { cout<<"base2 Dest\n"; } 38 Derived c(1, 2, 3); 20 }; 39 c.show( ); 21 class Derived : public Base1, 40 return 0; public Base2//The Order 41 } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

78 基类成员的初始化对象成员构造函数和析构函数的调用顺序 若派生类中包含对象成员, 则在派生类构造函数的初始化成员列表中 : 不仅应列举基类的构造函数, 还应列举对象成员的构造函数以初始化对象成员 1 class Base1 3 int data1; 4 public: 5 Base1(int a) { data1=a; } }; 1 class Base2 3 int data2; 4 public: 5 Base2(int a) { data2=a; } }; 1 class Derived : public Base1, public Base2 // 公有继承 3 int d; 4 Base1 b1, b2; // 对象成员 5 public: 6 Derived(int a, int b) : Base1(a), Base2(20), b1(200), b2(a+b){ d=a; } }; 先调用基类的构造函数, 再调用对象成员的构造函数, 最后执行对象自身的构造函数 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

79 基类成员的初始化对象成员构造函数和析构函数的调用顺序 若派生类中包含对象成员, 则在派生类构造函数的初始化成员列表中 : 不仅应列举基类的构造函数, 还应列举对象成员的构造函数以初始化对象成员 1 class Base1 3 int data1; 4 public: 5 Base1(int a) { data1=a; } }; 1 class Base2 3 int data2; 4 public: 5 Base2(int a) { data2=a; } }; 1 class Derived : public Base1, public Base2 // 公有继承 3 int d; 4 Base1 b1, b2; // 对象成员 5 public: 6 Derived(int a, int b) : Base1(a), Base2(20), b1(200), b2(a+b){ d=a; } }; 先调用基类的构造函数, 再调用对象成员的构造函数, 最后执行对象自身的构造函数 请问 : 一个 Derived 类的对象有多少个数据成员 ( 自身的和继承的 )? Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

80 提纲 总结与思考 1 2 单一继承 3 多重继承 4 基类成员的初始化 5 总结与思考 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

81 总结与思考 总结与思考 写出下面代码的运行结果 1 class Base{ 2 public: 3 Base() 4 { cout<<"base cons\n"; } 5 ~Base() 6 { cout<<"base dest\n"; } 7 }; 8 class Derived: public Base{ 9 public: 10 Derived(int i) 11 { num = i; 12 cout<<"derd cons "<<num; 13 cout<<"\n"; } 14 ~Derived() 15 { cout<<"derd dest"<<"\n"; } 16 int num; 17 }; 18 int main(){ 19 Derived dobj(1); 20 Base * bptr; 21 Derived * dptr; 22 bptr = new Derived(2); 23 delete bptr; return 0; 24 } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

82 总结与思考 总结与思考 写出下面代码的运行结果 1 class Base{ 2 public: 3 Base() 4 { cout<<"base cons\n"; } 5 ~Base() 6 { cout<<"base dest\n"; } 7 }; 8 class Derived: public Base{ 9 public: 10 Derived(int i) 11 { num = i; 12 cout<<"derd cons "<<num; 程序运行结果 : Base cons Derd cons 1 Base cons Derd cons 2 Base dest Derd dest Base dest 13 cout<<"\n"; } 14 ~Derived() 15 { cout<<"derd dest"<<"\n"; } 16 int num; 17 }; 18 int main(){ 19 Derived dobj(1); 20 Base * bptr; 21 Derived * dptr; 22 bptr = new Derived(2); 23 delete bptr; return 0; 24 } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

83 总结与思考 作业 总结与思考 判断与改错 1 类的指针不可以作为这个类自身的成员 ( ) 2 类的对象不可以作为这个类自身的成员 ( ) 3 常成员函数关键字 const 可用于区分重载成员函数 ( ) 4 重载类的成员函数时, 不允许在重载函数中使用缺省参数 ( ) 5 可以将公有派生类的对象赋值给基类的对象 ( ) 6 语句 Sample s(); 表示定义了类 Sample 的一个对象 ( ) 7 基类的私有成员可以被派生类继承, 但不能被派生类直接访问 ( ) 8 派生类的成员函数不可以直接访问私有继承的私有成员 ( ) 9 类的对象成员按照初始化表的顺序进行构造 ( ) 思考与简答 10 什么是继承与派生? 基类成员在派生类中的访问权限是何种规则? 11 基类的构造函数与析构函数能被派生类继承吗? 私有成员能被继承吗? 12 设计一个包含继承关系的类, 写出构造函数并说明它的调用时机 Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

84 总结与思考 作业 总结与思考 调试程序, 改正错误 : 可修改语句, 但不允许增加或删除语句 1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 class complex 5 { 6 private: 7 float x; 8 float y; 9 public: 10 complex (float a, float b) { x=a; y=b; } 11 float & real() { return x; } 12 float & image() { return y; } 13 float Mod() { float z=x*x+y*y; return z; } 14 bool operator < (complex& c) 15 { 16 if(mod()<c.mod()) return true; 17 else return false; 18 } 19 friend ostream& operator<<(ostream& os, complex &c) 20 { return os << c.x << + << c.y << "i "; } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

85 总结与思考 作业 总结与思考 21 void type() 2 23 if(x==0) cout<<" 是一个纯虚数模是,"<<Mod()<<endl; 24 else if(y==0) cout<<" 是一个实数模是,"<<Mod()<<endl; 25 else cout<<" 是一个复数模是,"<<Mod()<<endl; 26 } 27 }; 28 int main() 29 { 30 complex c[2]; 31 for(int i=0; i<2; i++) 3 33 cout<<" 请输入一个复数用空格分隔 (): " ; 34 cin >> c[i].x >> c[i].y; 35 cout << c[i], c[i].type(); 36 } 37 if(c[0] > c[1]) 38 cout<<" 前者的模比后者小 "<<endl; 39 else 40 cout<<" 前者的模不比后者小 "<<endl; 41 return 0; 42 } Dongke Sun (Southeast University) C++ Programming and Algorithms April 23, / 47

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double

More information

Strings

Strings Inheritance Cheng-Chin Chiang Relationships among Classes A 類 別 使 用 B 類 別 學 生 使 用 手 機 傳 遞 訊 息 公 司 使 用 金 庫 儲 存 重 要 文 件 人 類 使 用 交 通 工 具 旅 行 A 類 別 中 有 B 類 別 汽 車 有 輪 子 三 角 形 有 三 個 頂 點 電 腦 內 有 中 央 處 理 單 元 A

More information

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft PowerPoint - L17_Inheritance_v4.pptx C++ Programming Lecture 17 Wei Liu ( 刘 威 ) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology May. 2015 Lecture 17 Chapter 20. Object-Oriented Programming: Inheritance

More information

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double x) { d

More information

extend

extend (object oriented) Encapsulation Inheritance Polymorphism Dynamic Binding (base class) (derived class) 1 class Base { int I; void X(); void Y(); class Derived: public Base { private: int j; void z(); Derived

More information

Microsoft Word - chap10.doc

Microsoft Word - chap10.doc 78 10. Inheritance in C++ 我 們 已 介 紹 了 物 件 導 向 程 式 的 第 一 個 主 要 特 性, 即 程 式 可 模 組 化 成 為 類 別 ( 物 件 ), 類 別 具 有 資 料 封 裝 的 特 性 接 下 來 我 們 要 介 紹 物 件 導 向 程 式 的 另 一 個 主 要 特 性, 那 就 是 類 別 具 有 繼 承 的 功 能 繼 承 就 是 重 複

More information

C/C++程序设计 - 字符串与格式化输入/输出

C/C++程序设计 - 字符串与格式化输入/输出 C/C++ / Table of contents 1. 2. 3. 4. 1 i # include # include // density of human body : 1. 04 e3 kg / m ^3 # define DENSITY 1. 04 e3 int main ( void ) { float weight, volume ; int

More information

幻灯片 1

幻灯片 1 信息科学技术学院 程序设计实习 郭炜微博 http://weibo.com/guoweiofpku http://blog.sina.com.cn/u/3266490431 刘家瑛微博 http://weibo.com/pkuliujiaying 1 信息科学技术学院 程序设计实习 郭炜刘家瑛 继承和派生 ( 教材 P215) 2 继承和派生的概念 继承 : 在定义一个新的类 B 时, 如果该类与某个已有的类

More information

Microsoft PowerPoint - 04-Inheritance.ppt

Microsoft PowerPoint - 04-Inheritance.ppt 继承 派生与多态性 1 本章主要内容 类的继承与派生 类成员的访问控制 简单继承与多重继承 派生类的构造 析构函数 多态性 2 1 类的继承与派生 保持已有类的特性, 并在其基础上产生新的类, 称新类继承了已有类的特征, 或称已有类派生出新类 被继承的已有类称为基类 ( 或父类 ) 派生出的新类称为派生类 派生类将自动继承基类的所有特性 ( 属性和方法 ) 派生类可以定义新的特性 ( 属性和方法 )

More information

Microsoft PowerPoint - 8. 运算符重载 Operator Overloading.pptx

Microsoft PowerPoint - 8. 运算符重载 Operator Overloading.pptx 运算符重载 Operator Overloading class Point { public: ; double x_, y_; Why Operator Overloading? Point (double x =0, double y = 0):x_(x),y_(y) { int main(){ Point a(1., 2), b(3,4); Point c = a + b; return 0;

More information

c_cpp

c_cpp C C++ C C++ C++ (object oriented) C C++.cpp C C++ C C++ : for (int i=0;i

More information

C++_Lecture

C++_Lecture C++ 程序设计 张岳新编著 苏州大学出版社 1 第十一章 继承和派生类 2 继承性是面向对象程序设计中最重要的机制 这种机制提供了无限重复利用程序资源的一种途径 通过 C++ 语言中的继承机制, 可以扩充和完善旧的程序设计以适应新的需求 这样不仅可以节省程序开发的时间和资源, 并且为未来程序增添了新的资源 3 class Student { int num; char name[30]; char

More information

第七讲 继承与多态

第七讲  继承与多态 第 七 章 继 承 与 派 生 1 本 章 主 要 内 容 的 继 承 成 员 的 访 问 控 制 单 继 承 与 多 继 承 派 生 的 构 造 析 构 函 数 成 员 的 标 识 与 访 问 深 度 探 索 2 的 继 承 与 派 生 的 继 承 与 派 生 保 持 已 有 的 特 性 而 构 造 新 的 过 程 称 为 继 承 在 已 有 的 基 础 上 新 增 自 己 的 特 性 而 产 生

More information

untitled

untitled 1 7 7.1 7.2 7.3 7.4 7.5 2 7.1 VFT virtual 7.1 3 1 1. 2. public protected public 3. VFT 4. this const volatile 4 2 5. ( ) ( ) 7.1 6. no-static virtual 7.2 7. inline 7.3 5 3 8. this this 9. ( ) ( ) delete

More information

C++ 程序设计 告别 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 告别 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 告别 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1 1 TEMPLATE 1 Template 描述 使用模板函数求最大值 使用如下 main 函数对程序进行测试 int main() { double a, b; cin >> a >> b; cout c >> d; cout

More information

C/C++语言 - C/C++数据

C/C++语言 - C/C++数据 C/C++ C/C++ Table of contents 1. 2. 3. 4. char 5. 1 C = 5 (F 32). 9 F C 2 1 // fal2cel. c: Convert Fah temperature to Cel temperature 2 # include < stdio.h> 3 int main ( void ) 4 { 5 float fah, cel ;

More information

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课 复习 Java 包 创建包 : package 语句, 包结构与目录结构一致 使用包 : import restaurant/ - people/ - Cook.class - Waiter.class - tools/ - Fork.class

More information

Chapter12 Derived Classes

Chapter12   Derived Classes 继 承 -- 派 生 类 复 习 1. 有 下 面 类 的 说 明, 有 错 误 的 语 句 是 : class X { A) const int a; B) X(); C) X(int val) {a=2 D) ~X(); 答 案 :C 不 正 确, 应 改 成 X(int val) : a(2) { 2. 下 列 静 态 数 据 成 员 的 特 性 中, 错 误 的 是 A) 说 明 静 态 数

More information

エスポラージュ株式会社 住所 : 東京都江東区大島 東急ドエルアルス大島 HP: ******************* * 关于 Java 测试试题 ******

エスポラージュ株式会社 住所 : 東京都江東区大島 東急ドエルアルス大島 HP:  ******************* * 关于 Java 测试试题 ****** ******************* * 关于 Java 测试试题 ******************* 問 1 运行下面的程序, 选出一个正确的运行结果 public class Sample { public static void main(string[] args) { int[] test = { 1, 2, 3, 4, 5 ; for(int i = 1 ; i System.out.print(test[i]);

More information

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double x) { d =

More information

Microsoft PowerPoint - 第06讲_继承.ppt [兼容模式]

Microsoft PowerPoint - 第06讲_继承.ppt [兼容模式] 程序设计实习 (I): C++ 程序设计 第六讲继承 上节内容回顾 三种运算符重载的实现方式 重载为普通函数 重载为成员函数 重载为友元 常见的运算符重载 流运算符 (>>

More information

Microsoft Word - 新1-12.doc

Microsoft Word - 新1-12.doc 实训 5 面向对象编程练习 实训 5 面向对象编程练习 5.1 实训目的 通过编程和上机实验理解 Java 语言是如何体现面向对象编程基本思想 以及如何创建类 和对象 了解成员变量和成员方法的特性 5.2 实训要求 编写一个体现面向对象思想的程序 编写一个创建对象和使用对象的方法的程序 5.3 实训内容 5.3.1 创建对象并使用对象 1 定义一个 Person 类 可以在应用程序中使用该类 成员属性

More information

无类继承.key

无类继承.key 无类继承 JavaScript 面向对象的根基 周爱 民 / aimingoo aiming@gmail.com https://aimingoo.github.io https://github.com/aimingoo rand = new Person("Rand McKinnon",... https://docs.oracle.com/cd/e19957-01/816-6408-10/object.htm#1193255

More information

面向对象的程序设计 : 抽象 继承和多态 Dongke Sun (Southeast University) C++ Programming and Algorithms April 12, / 38

面向对象的程序设计 : 抽象 继承和多态 Dongke Sun (Southeast University) C++ Programming and Algorithms April 12, / 38 程序设计与算法语言 继承与派生 C/C++ Programming and Algorithms Inheritance and Derivation Dongke Sun ( 孙东科 ) dksun@seu.edu.cn 东南大学机械工程学院 School of Mechanical Engineering Southeast University April 12, 2018 面向对象的程序设计

More information

untitled

untitled 1 Outline 數 料 數 數 列 亂數 練 數 數 數 來 數 數 來 數 料 利 料 來 數 A-Z a-z _ () 不 數 0-9 數 不 數 SCHOOL School school 數 讀 school_name schoolname 易 不 C# my name 7_eleven B&Q new C# (1) public protected private params override

More information

软件工程文档编制

软件工程文档编制 实训抽象类 一 实训目标 掌握抽象类的定义 使用 掌握运行时多态 二 知识点 抽象类的语法格式如下 : public abstract class ClassName abstract void 方法名称 ( 参数 ); // 非抽象方法的实现代码 在使用抽象类时需要注意如下几点 : 1 抽象类不能被实例化, 实例化的工作应该交由它的子类来完成 2 抽象方法必须由子类来进行重写 3 只要包含一个抽象方法的抽象类,

More information

Microsoft Word - ch04三校.doc

Microsoft Word - ch04三校.doc 4-1 4-1-1 (Object) (State) (Behavior) ( ) ( ) ( method) ( properties) ( functions) 4-2 4-1-2 (Message) ( ) ( ) ( ) A B A ( ) ( ) ( YourCar) ( changegear) ( lowergear) 4-1-3 (Class) (Blueprint) 4-3 changegear

More information

FY.DOC

FY.DOC 高 职 高 专 21 世 纪 规 划 教 材 C++ 程 序 设 计 邓 振 杰 主 编 贾 振 华 孟 庆 敏 副 主 编 人 民 邮 电 出 版 社 内 容 提 要 本 书 系 统 地 介 绍 C++ 语 言 的 基 本 概 念 基 本 语 法 和 编 程 方 法, 深 入 浅 出 地 讲 述 C++ 语 言 面 向 对 象 的 重 要 特 征 : 类 和 对 象 抽 象 封 装 继 承 等 主

More information

C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 1 CIRCLE 1 Circle 描述 编写一个圆类 Circle, 实现半径的输入 面积的计算和输出 输入 圆的半径 (double 类型 ) 输出 圆的面积 ( 保留小数点后两位 ) 样例输入 3 样例输出 28.27 提示 圆周率的取值需要比较精确, 以保证计算结果的精度 #include

More information

untitled

untitled 3 C++ 3.1 3.2 3.3 3.4 new delete 3.5 this 3.6 3.7 3.1 3.1 class struct union struct union C class C++ C++ 3.1 3.1 #include struct STRING { typedef char *CHARPTR; // CHARPTR s; // int strlen(

More information

Microsoft PowerPoint - 6. 用户定义类型User-defined Datatypes.ppt [兼容模式]

Microsoft PowerPoint - 6. 用户定义类型User-defined Datatypes.ppt [兼容模式] 用户定义类型 User-defined Datatypes classes and structs 几何向量 (Geometry Vector) 二维平面上的向量由起点和终点构成 每个点包含两个坐标 (x, y), 因此一个向量需要四个实数表示 Start= (0.9,1.5) Start= (0.4,0.8) int main() { double xstart = 0.4; double xend

More information

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc References (Section 5.2) Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 15-16, 2010 H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 0 / 22 Fun Time (1) What happens in memory? 1 i n t i ; 2

More information

Strings

Strings Polymorphism and Virtual Functions Cheng-Chin Chiang Virtual Function Basics 多 型 (Polymorphism) 賦 予 一 個 函 數 多 種 意 涵, 存 在 於 同 一 類 別 之 內 祖 先 類 別 與 後 代 類 別 間 物 件 導 向 程 式 設 計 基 本 原 理 虛 擬 函 數 (Virtual Function)

More information

Chapter 9: Objects and Classes

Chapter 9: Objects and Classes Fortran Algol Pascal Modula-2 BCPL C Simula SmallTalk C++ Ada Java C# C Fortran 5.1 message A B 5.2 1 class Vehicle subclass Car object mycar public class Vehicle extends Object{ public int WheelNum

More information

新版 明解C++入門編

新版 明解C++入門編 511!... 43, 85!=... 42 "... 118 " "... 337 " "... 8, 290 #... 71 #... 413 #define... 128, 236, 413 #endif... 412 #ifndef... 412 #if... 412 #include... 6, 337 #undef... 413 %... 23, 27 %=... 97 &... 243,

More information

C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1 1 PERSON 1 Person 题目描述 编写程序, 定义一个基类 Person, 包含 name 和 age 两个数据成员 ; 再由它派生出学生类 Student 和教师类 Teacher, 其中学生类添加学号 no 数据, 教师类添加职称 title 数据 ; 要求每个类均有构造函数 析构函数和显示数据的函数

More information

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf (%d, & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9 201 201 21 ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp Compilation Error long long cin scanf Time Limit Exceeded 1: A 1 B 1 C 5 D RPG 10 E 10 F 1 G II 1 1 201 201 C 1 # include 2 int main ( void

More information

Microsoft PowerPoint - string_kruse [兼容模式]

Microsoft PowerPoint - string_kruse [兼容模式] Strings Strings in C not encapsulated Every C-string has type char *. Hence, a C-string references an address in memory, the first of a contiguous set of bytes that store the characters making up the string.

More information

C/C++ - 函数

C/C++ - 函数 C/C++ Table of contents 1. 2. 3. & 4. 5. 1 2 3 # include # define SIZE 50 int main ( void ) { float list [ SIZE ]; readlist (list, SIZE ); sort (list, SIZE ); average (list, SIZE ); bargragh

More information

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票 复习 创建对象 构造函数 函数重载 : 函数 = 函数名 + 参数列表 public class MyType { int i; double d; char c; void set(double x)

More information

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 月 3 日 1 1 INPUTOUTPUT 1 InputOutput 题目描述 用 cin 输入你的姓名 ( 没有空格 ) 和年龄 ( 整数 ), 并用 cout 输出 输入输出符合以下范例 输入 master 999 输出 I am master, 999 years old. 注意 "," 后面有一个空格,"." 结束,

More information

IO

IO 1 C/C++ C FILE* fscanf fgets fread fprintf fputs fwrite C++ ifstream ofstream >>

More information

untitled

untitled (encapsulation) 例 類 說 類 料 來 料 information hiding 念 (inheritance) 來說 類 類 類 類 類 類 行 利 來 (polymorphism) 不 類 數 不 1 2 3 4 類 類 不 類 不 類 5 6 7 // virtual 不見了 #include #include using namespace

More information

Microsoft PowerPoint - 3. 函数Functionl.ppt [兼容模式]

Microsoft PowerPoint - 3. 函数Functionl.ppt [兼容模式] 函数 Function 如何重用代码 How to reuse code 3 4 = 3*3*3*3 3 4,6 5 : 拷贝 - 粘帖代码 (Copy-paste code) 3 4,6 5,12 10 : 拷贝 - 粘帖代码 (Copy-paste code) Bad! 使用函数 (with a function) 使用函数 (with a function) 使用函数 (with a function)

More information

提纲 1 联编与多态性 2 用虚函数实现多态 3 虚函数的工作原理 4 纯虚函数与抽象类 5 总结与思考 Dongke Sun (Southeast University) C++ Programming and Algorithms April 12, / 36

提纲 1 联编与多态性 2 用虚函数实现多态 3 虚函数的工作原理 4 纯虚函数与抽象类 5 总结与思考 Dongke Sun (Southeast University) C++ Programming and Algorithms April 12, / 36 程序设计与算法语言 虚函数 C/C++ Programming and Algorithms Virtual Function Dongke Sun ( 孙东科 ) dksun@seu.edu.cn 东南大学机械工程学院 School of Mechanical Engineering Southeast University April 12, 2018 提纲 1 联编与多态性 2 用虚函数实现多态

More information

<4D6963726F736F667420506F776572506F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

<4D6963726F736F667420506F776572506F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074> 程 序 设 计 实 习 INFO130048 3-2.C++ 面 向 对 象 程 序 设 计 重 载 继 承 多 态 和 聚 合 复 旦 大 学 计 算 机 科 学 与 工 程 系 彭 鑫 pengxin@fudan.edu.cn 内 容 摘 要 方 法 重 载 类 的 继 承 对 象 引 用 和 拷 贝 构 造 函 数 虚 函 数 和 多 态 性 类 的 聚 集 复 旦 大 学 计 算 机 科 学

More information

程序设计语言及基础

程序设计语言及基础 Chapter 10 Classes: A Deeper Look, Part 2 http://jssec.seu.edu.cn 杨明 yangming2002@seu.edu.cn OBJECTIVES To specify const (constant) objects and const member functions. To create objects composed of other

More information

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 复习 Java 包 创建包 : package 语句, 包结构与目录结构一致 使用包 : import restaurant/ - people/ - Cook.class - Waiter.class - tools/ - Fork.class - Table.class

More information

CHAPTER VC#

CHAPTER VC# 1. 2. 3. 4. CHAPTER 2-1 2-2 2-3 2-4 VC# 2-5 2-6 2-7 2-8 Visual C# 2008 2-1 Visual C# 0~100 (-32768~+32767) 2 4 VC# (Overflow) 2-1 2-2 2-1 2-1.1 2-1 1 10 10!(1 10) 2-3 Visual C# 2008 10! 32767 short( )

More information

Microsoft PowerPoint - 07 派生数据类型

Microsoft PowerPoint - 07 派生数据类型 能源与动力工程学院 目录 派生类型 陈 斌 固有数据类型 数值型 (numerical) 整型 INTEGER 实型 REAL 复数型 COMPLEX 非数值型 字符型 CHARACTER 逻辑型 ( 布尔型 )LOGICAL 自定义数据类型 ( 派生类型, derived type) 派生类型是指用户利用 Fortran 系统内部类型, 如整型 实型 复数型 逻辑型 字符型等的组合自行创建出一个新的数据类型,

More information

第3章.doc

第3章.doc 3 3 3 3.1 3 IT Trend C++ Java SAP Advantech ERPCRM C++ C++ Synopsys C++ NEC C C++PHP C++Java C++Java VIA C++ 3COM C++ SPSS C++ Sybase C++LinuxUNIX Motorola C++ IBM C++Java Oracle Java HP C++ C++ Yahoo

More information

C++ 程序设计 实验 3 - 参考答案 MASTER 2017 年 5 月 21 日 1

C++ 程序设计 实验 3 - 参考答案 MASTER 2017 年 5 月 21 日 1 C++ 程序设计 实验 3 - 参考答案 MASTER 2017 年 5 月 21 日 1 1 圆 1 圆 设计圆类 包含 包含基本属性和基本属性访问接口 计算面积和周长接口 2 1 圆 1 #include 2 using namespace std ; 3 c l a s s CCircle 4 { 5 p r i v a t e : 6 double r ; 7 const

More information

C 1

C 1 C homepage: xpzhangme 2018 5 30 C 1 C min(x, y) double C // min c # include # include double min ( double x, double y); int main ( int argc, char * argv []) { double x, y; if( argc!=

More information

Microsoft Word - 物件導向編程精要.doc

Microsoft Word - 物件導向編程精要.doc Essential Object-Oriented Programming Josh Ko 2007.03.11 object-oriented programming C++ Java OO class object OOP Ruby duck typing complexity abstraction paradigm objects objects model object-oriented

More information

1 下列类头定义中, 正确的是 面向对象程序设计网络课程 A class x { } B public x extends y { } C public class x extends y {.} D class x extends y implements y1 { } 2 现有两个类 A B,

1 下列类头定义中, 正确的是 面向对象程序设计网络课程 A class x { } B public x extends y { } C public class x extends y {.} D class x extends y implements y1 { } 2 现有两个类 A B, 1 下列类头定义中, 正确的是 A class x B public x extends y C public class x extends y. D class x extends y implements y1 2 现有两个类 A B, 以下描述中表示 B 继承自 A 的是 (D ) A) class A extends B B) class B implements A C) class A

More information

模板

模板 制作人 : 张刚 目录 类和对象 面向对象程序设计基本特征 类的声明 构造方法 成员变量和方法 封装 继承 多态 包 访问控制 final static 抽象类和接口 内部类 沈阳工业大学软件学院 Java 课程教研组 Page 2 核心知识点 类 对象 三个基本特征 类的基本结构 成员变量 构造方法 成员方法 类实例 对象创建和操作 沈阳工业大学软件学院 Java 课程教研组 Page 3 1.

More information

OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点

OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点 复习 Protected 可以被子类 / 同一包中的类访问, 不能被其他类访问 弱化的 private 同时赋予 package access class MyType { public int i; public double d; public

More information

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

Lorem ipsum dolor sit amet, consectetuer adipiscing elit English for Study in Australia 留 学 澳 洲 英 语 讲 座 Lesson 3: Make yourself at home 第 三 课 : 宾 至 如 归 L1 Male: 各 位 朋 友 好, 欢 迎 您 收 听 留 学 澳 洲 英 语 讲 座 节 目, 我 是 澳 大 利 亚 澳 洲 广 播 电 台 的 节 目 主 持 人 陈 昊 L1 Female: 各 位

More information

Microsoft PowerPoint - 10 模板 Template.pptx

Microsoft PowerPoint - 10 模板 Template.pptx 模板 Tempalte 泛型编程的需要 Why Templates? 设想你对整数类型实现了一个排序算法 : void sort(int *is,int n); 用该函数可以对实 复数或工资单排序吗? 模板可以复用源代码 - 泛型编程. inline void Swap( int &x, int &y){ int t = x; x = y; y =t; inline void Swap(double

More information

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 5 月 2 日晚 9 点 复习 Protected 可以被子类 / 同一包中的类访问, 不能被其他类访问 弱化的 private 同时赋予 package access class MyType { public int i; public double d; public char

More information

C/C++ - 文件IO

C/C++ - 文件IO C/C++ IO Table of contents 1. 2. 3. 4. 1 C ASCII ASCII ASCII 2 10000 00100111 00010000 31H, 30H, 30H, 30H, 30H 1, 0, 0, 0, 0 ASCII 3 4 5 UNIX ANSI C 5 FILE FILE 6 stdio.h typedef struct { int level ;

More information

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 复习 Protected 可以被子类 / 同一包中的类访问, 不能被其他类访问 弱化的 private 同时赋予 package access class MyType { public int i; public double d; public char

More information

untitled

untitled 1 Outline 類别 欄 (1) 類 類 狀 更 易 類 理 若 類 利 來 利 using 來 namespace 類 ; (2) namespace IBM class Notebook namespace Compaq class Notebook 類别 類 來 類 列 欄 (field) (property) (method) (event) 類 例 立 來 車 類 類 立 車 欄 料

More information

程序设计与算法语言 静态与友元 C/C++ Programming and Algorithms Static, Friend and Const Dongke Sun ( 孙东科 ) 东南大学机械工程学院 School of Mechanical Engineer

程序设计与算法语言 静态与友元 C/C++ Programming and Algorithms Static, Friend and Const Dongke Sun ( 孙东科 ) 东南大学机械工程学院 School of Mechanical Engineer 程序设计与算法语言 静态与友元 C/C++ Programming and Algorithms Static, Friend and Const Dongke Sun ( 孙东科 ) dksun@seu.edu.cn 东南大学机械工程学院 School of Mechanical Engineering Southeast University Spring semester, 2019 牢记 80/20

More information

C++ 程序设计 实验 1 - 参考答案 MASTER 2017 年 5 月 21 日 1

C++ 程序设计 实验 1 - 参考答案 MASTER 2017 年 5 月 21 日 1 C++ 程序设计 实验 1 - 参考答案 MASTER 2017 年 5 月 21 日 1 1 简单图形 1 简单图形 输入图形的行数 ( 如下图 7 行 ), 输出如下图所示图形 * *** ***** ******* ***** *** * 2 1 简单图形 1 #inc lude 2 using namespace std ; 3 4 // 注意变量命名的方式 5 //

More information

02

02 Thinking in C++: Volume One: Introduction to Standard C++, Second Edition & Volume Two: Practical Programming C++ C C++ C++ 3 3 C C class C++ C++ C++ C++ string vector 2.1 interpreter compiler 2.1.1 BASIC

More information

新・解きながら学ぶJava

新・解きながら学ぶJava 481! 41, 74!= 40, 270 " 4 % 23, 25 %% 121 %c 425 %d 121 %o 121 %x 121 & 199 && 48 ' 81, 425 ( ) 14, 17 ( ) 128 ( ) 183 * 23 */ 3, 390 ++ 79 ++ 80 += 93 + 22 + 23 + 279 + 14 + 124 + 7, 148, 16 -- 79 --

More information

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf(%d, &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf(% 2013 ( 28 ) ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp Compilation Error long long cin scanf Time Limit Exceeded 1: A 10 B 1 C 1 D 5 E 5 F 1 G II 5 H 30 1 2013 C 1 #include 2 int main(void) 3

More information

C

C C 2017 3 14 1. 2. 3. 4. 2/95 C 1. 3/95 C I 1 // talkback.c: 2 #include 3 #include 4 #define DENSITY 62.4 5 int main(void) 6 { 7 float weight, volume; 8 int size; 9 unsigned long letters;

More information

运算符重载 为什么要 运算符重载 那些运算符可以重载, 哪些不可以 如何实现运算符重载 实现方式 : 成员函数与非成员函数 类型转换 怎样实现对象与基本数据类型数据的运算 2

运算符重载 为什么要 运算符重载 那些运算符可以重载, 哪些不可以 如何实现运算符重载 实现方式 : 成员函数与非成员函数 类型转换 怎样实现对象与基本数据类型数据的运算 2 第十一讲 运算符重载 与类型转换 运算符重载 为什么要 运算符重载 那些运算符可以重载, 哪些不可以 如何实现运算符重载 实现方式 : 成员函数与非成员函数 类型转换 怎样实现对象与基本数据类型数据的运算 2 为什么要运算符重载 预定义的运算符只针对基本数据类型, 若要对类的对象进行类似的运算, 需要重新定义运算符的功能 运算符重载实质就是函数重载 : 对已有的运算符赋予多重含义, 使得同一个运算符作用于不同类型的数据时导致不同的行为

More information

Microsoft PowerPoint - ch6 [相容模式]

Microsoft PowerPoint - ch6 [相容模式] UiBinder wzyang@asia.edu.tw UiBinder Java GWT UiBinder XML UI i18n (widget) 1 2 UiBinder HelloWidget.ui.xml: UI HelloWidgetBinder HelloWidget.java XML UI Owner class ( Composite ) UI XML UiBinder: Owner

More information

Microsoft PowerPoint - CPP-Ch Print.ppt [兼容模式]

Microsoft PowerPoint - CPP-Ch Print.ppt [兼容模式] Chapter 13 Object-Oriented Programming: Polymorphism http://jssec.seu.edu.cn 杨明 yangming2002@seu.edu.cn OBJECTIVES What polymorphism( 多态 ) is, how it makes programming more convenient, and how it makes

More information

ebook39-5

ebook39-5 5 3 last-in-first-out, LIFO 3-1 L i n e a r L i s t 3-8 C h a i n 3 3. 8. 3 C + + 5.1 [ ] s t a c k t o p b o t t o m 5-1a 5-1a E D 5-1b 5-1b E E 5-1a 5-1b 5-1c E t o p D t o p D C C B B B t o p A b o

More information

untitled

untitled http://idc.hust.edu.cn/~rxli/ 1.1 1.2 1.3 1.4 1.5 1.6 2 1.1 1.1.1 1.1.2 1.1.3 3 1.1.1 Data (0005794, 601,, 1, 1948.03.26, 01) (,,,,,) 4 1.1.1 Database DB 5 1.1.1 (DBMS) DDL ( Create, Drop, Alter) DML(

More information

chap07.key

chap07.key #include void two(); void three(); int main() printf("i'm in main.\n"); two(); return 0; void two() printf("i'm in two.\n"); three(); void three() printf("i'm in three.\n"); void, int 标识符逗号分隔,

More information

C/C++语言 - 运算符、表达式和语句

C/C++语言 - 运算符、表达式和语句 C/C++ Table of contents 1. 2. 3. 4. C C++ 5. 6. 7. 1 i // shoe1.c: # include # define ADJUST 7. 64 # define SCALE 0. 325 int main ( void ) { double shoe, foot ; shoe = 9. 0; foot = SCALE * shoe

More information

untitled

untitled 不 料 料 例 : ( 料 ) 串 度 8 年 數 串 度 4 串 度 數 數 9- ( ) 利 數 struct { ; ; 數 struct 數 ; 9-2 數 利 數 C struct 數 ; C++ 數 ; struct 省略 9-3 例 ( 料 例 ) struct people{ char name[]; int age; char address[4]; char phone[]; int

More information

untitled

untitled A, 3+A printf( ABCDEF ) 3+ printf( ABCDEF ) 2.1 C++ main main main) * ( ) ( ) [ ].* ->* ()[] [][] ** *& char (f)(int); ( ) (f) (f) f (int) f int char f char f(int) (f) char (*f)(int); (*f) (int) (

More information

ebook39-6

ebook39-6 6 first-in-first-out, FIFO L i n e a r L i s t 3-1 C h a i n 3-8 5. 5. 3 F I F O L I F O 5. 5. 6 5. 5. 6.1 [ ] q u e n e ( r e a r ) ( f r o n t 6-1a A 6-1b 6-1b D C D 6-1c a) b) c) 6-1 F I F O L I F ADT

More information

Microsoft Word - 97.01.30軟體設計第二部份範例試題_C++_ _1_.doc

Microsoft Word - 97.01.30軟體設計第二部份範例試題_C++_ _1_.doc 電 腦 軟 體 設 計 乙 級 技 術 士 技 能 檢 定 術 科 測 試 範 例 試 題 (C++) 試 題 編 號 :11900-920201-4 審 定 日 期 : 94 年 7 月 1 日 修 訂 日 期 : 96 年 2 月 1 日 97 年 1 月 30 日 ( 第 二 部 份 ) 電 腦 軟 體 設 計 乙 級 技 術 士 技 能 檢 定 術 科 測 試 應 檢 參 考 資 料 壹 試

More information

Microsoft Word - 第3章.doc

Microsoft Word - 第3章.doc Java C++ Pascal C# C# if if if for while do while foreach while do while C# 3.1.1 ; 3-1 ischeck Test() While ischeck while static bool ischeck = true; public static void Test() while (ischeck) ; ischeck

More information

程序设计与算法语言 拷贝构造 C/C++ Programming and Algorithms Copy Constructor Dongke Sun ( 孙东科 ) 东南大学机械工程学院 School of Mechanical Engineering South

程序设计与算法语言 拷贝构造 C/C++ Programming and Algorithms Copy Constructor Dongke Sun ( 孙东科 ) 东南大学机械工程学院 School of Mechanical Engineering South 程序设计与算法语言 拷贝构造 C/C++ Programming and Algorithms Copy Constructor Dongke Sun ( 孙东科 ) dksun@seu.edu.cn 东南大学机械工程学院 School of Mechanical Engineering Southeast University Spring semester, 2019 视 C++ 为一个语言联邦

More information

C C

C C C C 2017 3 8 1. 2. 3. 4. char 5. 2/101 C 1. 3/101 C C = 5 (F 32). 9 F C 4/101 C 1 // fal2cel.c: Convert Fah temperature to Cel temperature 2 #include 3 int main(void) 4 { 5 float fah, cel; 6 printf("please

More information

C/C++ - 字符输入输出和字符确认

C/C++ - 字符输入输出和字符确认 C/C++ Table of contents 1. 2. getchar() putchar() 3. (Buffer) 4. 5. 6. 7. 8. 1 2 3 1 // pseudo code 2 read a character 3 while there is more input 4 increment character count 5 if a line has been read,

More information

NOWOER.OM m/n m/=n m/n m%=n m%n m%=n m%n m/=n 4. enum string x1, x2, x3=10, x4, x5, x; 函数外部问 x 等于什么? 随机值 5. unsigned char *p1; unsigned long *p

NOWOER.OM m/n m/=n m/n m%=n m%n m%=n m%n m/=n 4. enum string x1, x2, x3=10, x4, x5, x; 函数外部问 x 等于什么? 随机值 5. unsigned char *p1; unsigned long *p NOWOER.OM /++ 程师能 评估. 单项选择题 1. 下 描述正确的是 int *p1 = new int[10]; int *p2 = new int[10](); p1 和 p2 申请的空间 的值都是随机值 p1 和 p2 申请的空间 的值都已经初始化 p1 申请的空间 的值是随机值,p2 申请的空间 的值已经初始化 p1 申请的空间 的值已经初始化,p2 申请的空间 的值是随机值 2.

More information

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf (%d, & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf (%d %d 2013 18 ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp, Compilation Error cin scanf Time Limit Exceeded 1: A 5 B 5 C 5 D 5 E 5 F 5 1 2013 C 1 # include 2 int main ( void ) 3 { 4 int cases, a, b,

More information

2

2 40 2 3 4 5 ^ ^ 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 PLEASE AFFIX STAMP HERE Diabetes Hongkong Unit 1802, 18/F., Tung Hip Commercial Bldg., 244-252 Des Voeux Rd C, HK. Diabetes Hongkong membership

More information

3.1 num = 3 ch = 'C' 2

3.1 num = 3 ch = 'C' 2 Java 1 3.1 num = 3 ch = 'C' 2 final 3.1 final : final final double PI=3.1415926; 3 3.2 4 int 3.2 (long int) (int) (short int) (byte) short sum; // sum 5 3.2 Java int long num=32967359818l; C:\java\app3_2.java:6:

More information

C/C++ 语言 - 循环

C/C++ 语言 - 循环 C/C++ Table of contents 7. 1. 2. while 3. 4. 5. for 6. 8. (do while) 9. 10. (nested loop) 11. 12. 13. 1 // summing.c: # include int main ( void ) { long num ; long sum = 0L; int status ; printf

More information

C++ 程式設計

C++ 程式設計 C C 料, 數, - 列 串 理 列 main 數串列 什 pointer) 數, 數, 數 數 省 不 不, 數 (1) 數, 不 數 * 料 * 數 int *int_ptr; char *ch_ptr; float *float_ptr; double *double_ptr; 數 (2) int i=3; int *ptr; ptr=&i; 1000 1012 ptr 數, 數 1004

More information

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40 C C trio@seu.edu.cn C C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40 Week3 C Week5 Week5 Memory & Pointer

More information

Microsoft Word - 第7章 类与对象.docx

Microsoft Word - 第7章 类与对象.docx 第 7 章类与对象 案例 1 地址类 #include class Address char Name[21]; char Street[51]; char City[51]; char Postcode[10]; SetAddress(char *name,char *street,char *city,char *postcode); void ChangeName(char

More information

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii 前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii C# 7 More Effective C# C# C# C# C# C# Common Language Runtime CLR just-in-time

More information

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc 聖 保 羅 男 女 中 學 學 年 中 一 入 學 申 請 申 請 須 知 申 請 程 序 : 請 將 下 列 文 件 交 回 本 校 ( 麥 當 勞 道 33 號 ( 請 以 A4 紙 張 雙 面 影 印, 並 用 魚 尾 夾 夾 起 : 填 妥 申 請 表 並 貼 上 近 照 小 學 五 年 級 上 下 學 期 成 績 表 影 印 本 課 外 活 動 表 現 及 服 務 的 證 明 文 件 及

More information

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡 Tips of the Week 课 堂 上 的 英 语 习 语 教 学 ( 二 ) 2015-04-19 吴 倩 MarriottCHEI 大 家 好! 欢 迎 来 到 Tips of the Week! 这 周 我 想 和 老 师 们 分 享 另 外 两 个 课 堂 上 可 以 开 展 的 英 语 习 语 教 学 活 动 其 中 一 个 活 动 是 一 个 充 满 趣 味 的 游 戏, 另 外

More information

Microsoft Word - 970617cppFinalSolution.doc

Microsoft Word - 970617cppFinalSolution.doc 國 立 台 灣 海 洋 大 學 資 訊 工 程 系 C++ 程 式 設 計 期 末 考 參 考 答 案 姓 名 : 系 級 : 學 號 : 97/06/17 考 試 時 間 :10:00 12:10 試 題 敘 述 蠻 多 的, 看 清 楚 題 目 問 什 麼, 針 對 重 點 回 答 是 很 重 要 的 ; 不 確 定 的 請 一 定 要 當 場 提 出 來, 不 要 白 花 力 氣 在 誤 會

More information

提问袁小兵:

提问袁小兵: C++ 面 试 试 题 汇 总 柯 贤 富 管 理 软 件 需 求 分 析 篇 1. STL 类 模 板 标 准 库 中 容 器 和 算 法 这 部 分 一 般 称 为 标 准 模 板 库 2. 为 什 么 定 义 虚 的 析 构 函 数? 避 免 内 存 问 题, 当 你 可 能 通 过 基 类 指 针 删 除 派 生 类 对 象 时 必 须 保 证 基 类 析 构 函 数 为 虚 函 数 3.

More information

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F 1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET 2.0 2.0.NET Framework.NET Framework 2.0 ( 3).NET Framework 2.0.NET Framework ( System ) o o o o o o Boxing UnBoxing() o

More information

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1 C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1 1 CRECT 类 1 CRect 类 设计矩形类, 包含 长度和宽度信息 基本构造函数 基础属性的访问接口 ( 读 / 写, Read/Write, Get/Set) 计算周长和面积 ( 注 : 基本构造函数, 一个无参数的默认构造函数, 以及一个初始化数据成员的构造函数如果数据成员的初始化有多种形式, 就提供多个构造函数

More information

三維空間之機械手臂虛擬實境模擬

三維空間之機械手臂虛擬實境模擬 VRML Model of 3-D Robot Arm VRML Model of 3-D Robot Arm MATLAB VRML MATLAB Simulink i MATLAB Simulink V-Realm Build Joystick ii Abstract The major purpose of this thesis presents the procedure of VRML

More information