9.2 Time Class Case Study ---Object Size class test∥/1 class test∥/2 public int num char c. void displayMessage O;); private int num; class test∥/3 } int num char c.t. } class testt∥5 static char s class test∥/4 int num. char s int num char c t } 在默认情况下,VC规定各成员变量存放的起始地址相对于结构的起始地址 的偏移量必须为该变量的类型所占用的字节数的倍数。 vC为了确保结构的大小为结构的字节边界数(即该结构中占用最大空间的 类型所占用的字节数)的倍数,所以在为最后一个成员变量申请空间后, 还会根据需要自动填充空缺的字节。 0 2018, SEU. All rights reserved. 16
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 16 9.2 Time Class Case Study --- Object Size class test{ // 1 public: void displayMessage(); private: int num; }; class test{ // 2 int num; char c; }; class test{ // 3 int num; char c, t; }; class test{ // 4 char s; int num; char c, t; }; class test{ // 5 static char s; int num; }; 4 8 8 12 4 在默认情况下,VC规定各成员变量存放的起始地址相对于结构的起始地址 的偏移量必须为该变量的类型所占用的字节数的倍数。 VC为了确保结构的大小为结构的字节边界数(即该结构中占用最大空间的 类型所占用的字节数)的倍数,所以在为最后一个成员变量申请空间后, 还会根据需要自动填充空缺的字节
Topics 9.1 Introduction o 9.2 Time Class Case Study o 9.3 Class Scope and Accessing Class Members o 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 99.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 9.10 Default Memberwise Assignment 0 2018, SEU. All rights reserved. 17
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 17 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.3 Class Scope and Accessing Class Members Class Scope o Function Scope o File scope Block Scope(Local Scope) Function-prototype Scope Class Scope The name of a class member(数据成员/成员函数) ●类内: accessible by all of that classs member functions and can be referenced by name 类外: public class members are referenced througl one of the handles(句柄) on an object an object name(对象名) a reference to an object(对象引用) a pointer to an object(对象指针) 0 2018, SEU. All rights reserved. 18
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 18 9.3 Class Scope and Accessing Class Members --- Class Scope Function Scope File Scope Block Scope (Local Scope) Function-prototype Scope Class Scope The name of a class member (数据成员/成员函数) 类内:accessible by all of that class's member functions and can be referenced by name 类外:public class members are referenced through one of the handles(句柄) on an object an object name (对象名) a reference to an object (对象引用) a pointer to an object (对象指针)
#include <iostream> using namespace std, How to Access int a=0: File Scope class Test public Test (int a t int b: b= 99 Block Scope a= a 2=b;1 a= a a=100; void display cout <<a <<<<b<<<<:a; 1 private int a, b; Class Scope } int maino i Test test(98); test display Message 0 9899100 eturn 0: 心∠U1,。EU. All ngns reserved 19
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 19 #include <iostream> using namespace std; int a = 0; class Test { public: Test (int a){ int b; b = 99; Test::a = a; Test::b = b; this->a = a; this->b = b; ::a = 100; } void displayMessage(){ cout << a << " " << b << " " << ::a; } private: int a, b; }; int main() { Test test(98); test.displayMessage(); return 0; } 98 99 100 File Scope Block Scope Class Scope How to Access
类的存储与ths指针 类中定义的数据成员对该类的每个对象都有一个拷贝 类中的成员函数对该类来说只有一个拷贝 class a 全局区 代码区 i public: share shared void fO: 成员函数代码 void g(int i) ix=l; f0) b private static to ax int x, y,z: ay bbb xyz } az Aa b 栈区 0 2018, SEU. All rights reserved. 20
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 20 类的存储与this指针 类中定义的数据成员对该类的每个对象都有一个拷贝 类中的成员函数对该类来说只有一个拷贝。 class A { public: void f(); void g(int i) {x=I; f()} private: static t; int x,y,z; }; A a,b; a.x a.y a.z b.x b.y b.z a b 栈区 t shared 全局区 成员函数代码 shared 代码区