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
92 Time Class Case Study--流程 1. Edit 2,. Preprocess宏、文件包含、条件编译 3. Compile编译缙误(语法等) 4. Link 5. Load 6. Execute Runtime error(运行的增说,Ftml致命错误 logic逻辑错误 0 2018, SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 1. Edit 2. Preprocess 宏、文件包含、条件编译 3. Compile 编译错误(语法等) 4. Link 5. Load 6. Execute Runtime Error(运行时错误),Fatal致命错误,Logic逻辑错误 9.2 Time Class Case Study --- 流程
9.2 Time Class case stud Preprocessor Wrapper 现象:头文件被重复引用 est. cpp预处理 include结果 ∥/ah //test. cpp class af int a, class Af #include"a.h" #include"bh class af int a 1 int a class b double b:1 } int maino ∥/b.h int maino #include a h Aa class Bo Bb. Aa double b: return 0: Bb } return UR error C2011:A:class' type redefinition 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 9.2 Time Class Case Study --- Preprocessor Wrapper 现象:头文件被重复引用 // a.h class A{ int a; }; // b.h #include "a.h" class B{ double b; }; // test.cpp #include "a.h" #include "b.h" int main() { A a; B b; return 0; } class A{ int a;} class A{ int a;} class B{ double b;} int main() { A a; B b; return 0; } Test.cpp预处理include结果 error C2011: 'A' : 'class' type redefinition
92 Time Class Case Study-预处理器指令 #define #define pl 3.1415 #define flag 预处理器定义 o#ifdef /#ifndef o #else o #endif 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 9.2 Time Class Case Study --- 预处理器指令 #define #define PI 3.1415 #define FLAG 预处理器定义 #ifdef / #ifndef #else #endif
9.2 Time Class case stud 条件编译 般情况下,源程序中所有的行都参加编译;但 是有时希望其中一部分内容只在满足一定条件才 进行编译,也就是对一部分内容指定编译的条件, 这就是“条件编译”。 #ifndef flag #ifdef flag cout<<“代码1″<< endl cout<<“代码1"<<endl #endif #else cout<<“代码2”<<endl #ifdef flag #endif cout<<“代码2”<endl #endif 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 9.2 Time Class Case Study --- 条件编译 一般情况下,源程序中所有的行都参加编译;但 是有时希望其中一部分内容只在满足一定条件才 进行编译,也就是对一部分内容指定编译的条件, 这就是“条件编译” 。 #ifndef FLAG cout << “代码1” << endl; #endif #ifdef FLAG cout << “代码2” << endl; #endif #ifdef FLAG cout << “代码1” << endl; #else cout << “代码2” << endl; #endif
9.2 Time Class Case Study Preprocessor Wrapper Test. cpp预处理 include结果 ∥/ah //test. cpp #ifndef h #ifndef h #include ah #define h #define h #include"bh" class af int a; 1 class Af #endif int a. int maino } #ifndef h #endif A #define h class af int a 1 Ibh Bb #endif #includeh return 0: class bi double b] class bi double b } 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 9.2 Time Class Case Study --- Preprocessor Wrapper // a.h #ifndef A_H #define A_H class A{ int a; }; #endif // b.h #include "a.h" class B{ double b; }; // test.cpp #include "a.h" #include "b.h" int main() { A a; B b; return 0; } #ifndef A_H #define A_H class A{ int a;} #endif #ifndef A_H #define A_H class A{ int a;} #endif class B{ double b;} Test.cpp预处理include结果