Chapter 9 Classes: A Deeper Look Part 1 东方 fdongaseu.edu.cn 0 2018, SEU. All rights reserved. 1
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 1 Classes: A Deeper Look, Part 1 东方 fdong@seu.edu.cn Chapter 9
OBJECTIVES low to use a preprocessor wrapper to prevent multiple definition errors caused by including more than one copy of a header fille in a source -code file (使用预处理操作避免重复定义) o To understand class scope and accessing class members via the name of an object, a reference to an object or a pointer to an object.(访问类的成员) To define constructors with default arguments.(定义具有默认实 参的构造函数) How destructors are used to perform"termination housekeeping on an object before it is destroyed.(析构函数的使用) When they are called.(构造函数和析构函数的调用顺分 derin constructors and destructors are callled and the o which The logic errors that may occur when a public member function of a class returns a reference to private data.(访问权限引起的逻 辑错误) To assign the data members of one object to those of another object6 y default memberwise assignment.(通过成员赋值将一个 对象的数据成员赋给另一个对象的相应成员) 0 2018, SEU. All rights reserved. 2
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 2 OBJECTIVES How to use a preprocessor wrapper to prevent multiple definition errors caused by including more than one copy of a header file in a source-code file. (使用预处理操作避免重复定义) To understand class scope and accessing class members via the name of an object, a reference to an object or a pointer to an object.(访问类的成员) To define constructors with default arguments.(定义具有默认实 参的构造函数) How destructors are used to perform “termination housekeeping” on an object before it is destroyed.(析构函数的使用) When constructors and destructors are called and the order in which they are called.(构造函数和析构函数的调用顺序) The logic errors that may occur when a public member function of a class returns a reference to private data.(访问权限引起的逻 辑错误) To assign the data members of one object to those of another object by default memberwise assignment.(通过成员赋值将一个 对象的数据成员赋给另一个对象的相应成员)
9.1 Introduction Preprocessor wrapper(预处理器封套) access functions(访问函数)& utility function 工具函数, also called a helper function) 类 o arguments default arguments for constructors(使用默认实参定义构造函数)包装 Destructor(析构函数) o Reference to private data 访问 °如何从类现有的对象生成新的对象? 拷贝 default memberwise assignment 0 2018, SEU. All rights reserved. 3
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 3 9.1 Introduction Preprocessor wrapper(预处理器封套) access functions(访问函数) & utility function (工具函数, also called a helper function) arguments & default arguments for constructors(使用默认实参定义构造函数) Destructor(析构函数) Reference to private data 如何从类现有的对象生成新的对象? default memberwise assignment 包装 访问 拷贝 类
Topics 9.1 Introduction o 9.2 Time Class Case Study 9.3 Class Scope and accessing class members 9.4 Separating Interface from Implementation o 9.5 Access Functions and Utility Functions o 9.6 Time Class Case Study: Constructors with Default Arguments o 9.7 Destructors o 9. 8 When Constructors and destructors are called 9.9 Time Class Case Study: A Subtle Trap-Returning a Reference to a private Data Member o 9.10 Default Memberwise Assignment 0 2018, SEU. All rights reserved. 4
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 4 Topics 9.1 Introduction 9.2 Time Class Case Study 9.3 Class Scope and Accessing Class Members 9.4 Separating Interface from Implementation 9.5 Access Functions and Utility Functions 9.6 Time Class Case Study: Constructors with Default Arguments 9.7 Destructors 9.8 When Constructors and Destructors Are Called 9.9 Time Class Case Study: A Subtle Trap - Returning a Reference to a private Data Member 9.10 Default Memberwise Assignment
9.2 Time Class Case Study Preprocessor wrapper(预处理器封套) 成员函数定义的两种方法 How to use class o The size of class 序解读( Textbook P370F91) 0 2018, SEU. All rights reserved. 5
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 5 9.2 Time Class Case Study Preprocessor wrapper(预处理器封套) 成员函数定义的两种方法 How to use class The size of class