1 / 61

C++ Programming Lecture 15

C++ Programming Lecture 15. Wei Liu ( 刘威 ) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology Feb. 2014. The separation of Interfaces and implementations . Interfaces (class definitions) in header file, e.g., myClass.h.

taniel
Télécharger la présentation

C++ Programming Lecture 15

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ ProgrammingLecture 15 Wei Liu (刘威) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology Feb. 2014

  2. The separation of Interfaces and implementations Interfaces (class definitions) in header file, e.g., myClass.h Implementation (member function definitions) in new source file, e.g., myClass.cpp Single source file e.g., try.cpp New source file e.g., myTry.cpp

  3. 面向对象的编程 vs. 面向过程的编程 面向对象的编程 面向过程的编程 函数的设计 类的设计 类的调用: 类的实例化获得对象 对象可以维持自身数 据,同时还有方法 函数的调用: 主调函数需要 维持数据

  4. Lecture 15 • Chapter 17. Classes: A Deeper Look, I • 17.1 Introduction • 17.2 Time Class Case Study • 17.3 Class Scope and Accessing Class Members • 17.4 Separating Interface from Implementation • 17.5 Access Functions and Utility Functions • 17.6 Time Class Case Study: Constructors with Default Arguments • 17.7 Destructors • 17.8 When Constructors and Destructors Are Called • 17.9 Time Class Case Study: A Subtle Trap Returning a Reference to a private Data Member • 17.10 Default Memberwise Assignment

  5. 17.3  Class Scope and Accessing Class Members • Class Scope 类的作用域 • A class’s data members and member functions belong to that class’s scope. 类的数据成员与成员函数属于该类的作用域 • Within a class’s scope, class members are immediately accessible by all of that class’s member functions and can be referenced by name. 类的作用域内,各种成员可以通过名称直接访问 • Outside a class’s scope, public class members are referenced through one of the handles on an object—an object name, a reference to an object or a pointer to an object. 类的作用域外,只有public的成员可以通过某个对象(形式可以是名称、引用或者指针)来访问

  6. 17.3  Class Scope and Accessing Class Members • Scopes 作用域 • Global namespace scope 全局名称空间作用域 • Class scope 类的作用域 • Function scope 函数的作用域 • Relationships among scopes作用域的关系 • Nonmember functions are defined at global namespace scope. 其他的函数属于全局名称空间的作用域 • The same name function-scope variable in member function will hide the class-scope variable 类似于局部变量与全局变量的关系,在成员函数内定义的同名变量拥有比同名数据成员更高的可见性 • We can visit the class-scope variable with the class name followed by the scope resolution operator (::) 可以通过类的作用域解析符显式访问类的数据成员

  7. 17.3  Class Scope and Accessing Class Members • Accessing Class Members 访问类的成员 • The dot member selection operator (.) is preceded by an object’s name or with a reference to an object to access the object’s members. 逗号成员选择符在某个对象的名称或者引用之后,可以访问该对象的各种成员 • The arrow member selection operator (->) is preceded by a pointer to an object to access the object’s members. 箭头成员选择符在某个对象的指针之后,可以访问该对象的各种成员 Time a, *b; a.printUniversal(); b->printUniversal();

  8. 创建类的指针、引用的语法与创建基本数据类型的指针、引用的语法类似。创建类的指针、引用的语法与创建基本数据类型的指针、引用的语法类似。

  9. Lecture 15 • Chapter 17. Classes: A Deeper Look, I • 17.1 Introduction • 17.2 Time Class Case Study • 17.3 Class Scope and Accessing Class Members • 17.4 Separating Interface from Implementation • 17.5 Access Functions and Utility Functions • 17.6 Time Class Case Study: Constructors with Default Arguments • 17.7 Destructors • 17.8 When Constructors and Destructors Are Called • 17.9 Time Class Case Study: A Subtle Trap Returning a Reference to a private Data Member • 17.10 Default Memberwise Assignment

  10. 17.4  Separating Interface from Implementation 接口与实现的分离 • Separating classes into two files • a header file for the class definition (i.e., the class’s interface) 头文件 • a source code file for the class’s member-function definitions (i.e., the class’s implementation) 源文件 • Benefits • this encourages ISVs(independent software vendors), they only provide the header files(readable text file) and the object modules(un-readable binary files) 便于软件厂商发布他们的软件产品 • Information only internally used in the class will not appear in header files, following the principle of least privilege 体现了软件设计的最小权限原则

  11. Separating Interface from Implementation 接口与实现的分离 Interfaces (class definitions) in header file, e.g., myClass.h Implementation (member function definitions) in new source file, e.g., myClass.cpp source file e.g., myTry.cpp

  12. Lecture 15 • Chapter 17. Classes: A Deeper Look, I • 17.1 Introduction • 17.2 Time Class Case Study • 17.3 Class Scope and Accessing Class Members • 17.4 Separating Interface from Implementation • 17.5 Access Functions and Utility Functions • 17.6 Time Class Case Study: Constructors with Default Arguments • 17.7 Destructors • 17.8 When Constructors and Destructors Are Called • 17.9 Time Class Case Study: A Subtle Trap Returning a Reference to a private Data Member • 17.10 Default Memberwise Assignment

  13. 17.5  Access Functions and Utility Functions • Member functions 成员函数的作用有几种 • Access function 访问函数 • Access functions can read or display data. 通常是public访问权限,提供对类的数据成员的访问,常见的例子有测试条件是真是假,例如Time类里面的isAM()、isPM()等 • Utility function 工具函数 • A utility function is a private member function that supports the operation of the class’s public member functions. 通常是private访问权限,是public函数的辅助函数,例如SalesPerson类里面的totalAnnualSales()函数

  14. Chapter 15 • Topic 3: What is inside the Class: Creation and Destroy • 17.6 Time Class Case Study: Constructors with Default Arguments • How default arguments can be used in a constructor. • 17.7 Destructors • Destructors that perform “termination housekeeping” on objects before they are destroyed. • 17.8 When Constructors and Destructors Are Called • The order in which constructors and destructors are called. • 17.10 Default Memberwise Assignment • To assign the data members of one object to those of another by default memberwise assignment

  15. 17.6  Constructors with Default Arguments • Constructors with default argument 默认实参的构造函数 • Even if no values are provided in a constructor call, the constructor still initializes the data members 默认实参可以帮助构造函数在无输入时初始化数据成员 • A constructor that defaults all its arguments is also a default constructor—i.e., a constructor that can be invoked with no arguments. 对所有实参都有默认值的时候,该构造函数即为默认构造函数,这种函数一个类只能有一个

  16. 在类的定义中,构造函数的所有参数都具有默认实参,即为默认构造函数在类的定义中,构造函数的所有参数都具有默认实参,即为默认构造函数

  17. 构造函数中设置数据成员的工作通过调用setXXX函数完成,减少重复代码、提高程序的可靠性构造函数中设置数据成员的工作通过调用setXXX函数完成,减少重复代码、提高程序的可靠性 构造函数可以调用其他工具函数,但是需要注意数据成员的初始化问题

  18. Chapter 15 • Topic 3: What is inside the Class: Creation and Destroy • 17.6 Time Class Case Study: Constructors with Default Arguments • How default arguments can be used in a constructor. • 17.7 Destructors • Destructors that perform “termination housekeeping” on objects before they are destroyed. • 17.8 When Constructors and Destructors Are Called • The order in which constructors and destructors are called. • 17.10 Default Memberwise Assignment • To assign the data members of one object to those of another by default memberwise assignment

  19. 17.7  Destructors • Destructor析构函数 • The name of the destructor for a class is the tilde character (~) followed by the class name. 函数名即为类名加上~ • Called implicitly when an object is destroyed. 在对象销毁时被隐式调用 • Receives no parameters and returns no value.析构函数无形式参数、无返回值 • A class may have only one destructor. 一个类只能有一个析构函数 • A destructor must be public. 析构函数必须是public的

  20. 17.7  Destructors 析构函数 • Destructor • The destructor itself does not actually release the object’s memory—it performs termination housekeeping before the object’s memory is reclaimed. 析构函数不能释放对象的内存,但是可以对对象的各种数据成员进行清理 • If you do not explicitly provide a destructor, the compiler creates an “empty” destructor. 如果用户没有提供析构函数,编译器将自动创建一个空的析构函数

  21. 17.8  When Constructors and Destructors Are Called • Constructors and destructors are called implicitly. 构造函数和析构函数都是被隐式调用的 • The order in which these function calls occur depends on the order in which execution enters and leaves the scopes where the objects are instantiated. The storage classes of objects can alter the order in which destructors are called.其被调用的顺序与对象的作用域、存储类型、中止方式都有关

  22. 17.8  When Constructors and Destructors Are Called • Issues on scope • Objects in global scope 具有全局作用域的对象 • Constructors are called before any other function (including main) in that file begins execution. The corresponding destructors are called when main terminates. 构造函数在main()函数之前调用,析构函数在main函数结束时调用 • Objects in local scope 具有本地作用域的对象 • Constructors and destructors for automatic objects are called each time execution enters and leaves the scope of the object. 构造函数和析构函数在进入和离开该对象的作用域时调用

  23. 17.8  When Constructors and Destructors Are Called • Issues on storage type • Static object 静态对象 • The constructor is called only once, when execution first reaches the point where the object is defined—the corresponding destructor is called when main terminates or the program calls function exit. 构造函数在碰到该对象时被调用一次,析构函数在main()结束时调用 • Global and static objects are destroyed in the reverse order of their creation. 全局以及静态对象的析构函数调用次序与构造函数相反

  24. 17.8  When Constructors and Destructors Are Called • Issues on termination method • exit() • Function exit (keyword) forces a program to terminate immediately and does not execute the destructors of automatic objects. Exit 函数强迫函数中止,不会调用自动对象析构函数 • abort() • Function abort (keyword) performs similarly to function exit without allowing the destructors of any objects to be called. Abort 函数亦强迫函数中止,且不允许调用任何对象的析构函数

  25. Global object Static objects

  26. Experiment • 改进日期类,观察对象的创建与销毁 main.cpp date.h date.cpp

  27. Chapter 15 • Topic 3: What is inside the Class: Creation and Destroy • 17.6 Time Class Case Study: Constructors with Default Arguments • How default arguments can be used in a constructor. • 17.7 Destructors • Destructors that perform “termination housekeeping” on objects before they are destroyed. • 17.8 When Constructors and Destructors Are Called • The order in which constructors and destructors are called. • 17.10 Default Memberwise Assignment • To assign the data members of one object to those of another by default memberwise assignment

  28. 17.10 Default Memberwise Assignment • Memberwise assignment 逐个成员赋值 • The assignment operator (=) can be used to assign an object to another object of the same type. 同类型的对象可以通过赋值表达式进行复制 • Each data member of the object on the right of the assignment operator is assigned individually to the same data member in the object on the left of the assignment operator. 此时对象的每个数据成员将被赋值给另外一个对象的数据成员(如果数据成员是指针类型,则存在一定风险) • Objects may be passed as function arguments and may be returned from functions using pass-by-value by default. C++ creates a new object and uses a copy constructor to copy the original object’s values into the new object. 对象作为函数形参或者返回值时,C++会启用复制构造函数来实现逐个成员的赋值

  29. 同类型的对象可以通过赋值表达式进行复制, • 此时对象的每个数据成员将被赋值给另外一个 • 对象的数据成员

  30. Chapter 18. Classes: A Deeper Look, II • 18.2 const (Constant) Objects and const Member Functions • 18.6 static Class Members • 18.3 Composition: Objects as Members of Classes • 18.4 friend Functions and friend Classes • 18.7 Data Abstraction and Information Hiding • 18.5 Using the this Pointer

  31. 18.2 const (Constant) Objects and const Member Functions • const object • 声明一个对象为常量,本质上与声明常量变量没有区别 const Time noon(12,0,0); • const member function • 声明一个类的成员函数为常量成员函数,即约定该函数不能修改类的数据成员 • prototype 声明 void display(int) const ; • definition 定义 void display(int a) const { cout << “testing ” << a << endl; }

  32. A member function is specified as constboth in its prototype and in its definition.

  33. A member function is specified as constboth in its prototype and in its definition.

  34. const member function 常量成员函数 • 声明一个类的成员函数为常量成员函数,即约定该函数不能修改类的数据成员 • prototype 声明 void display(int) const ; • const data members 常量数据成员 • 声明一个类的数据成员为常量,即约定该数据成员运行期间不允许被修改 • 这种数据成员的初始化必须通过“成员初始化器” (member initializers)来实现 const intincrement;

  35. member initializer • member initializer for const data member • 常量数据成员的初始化器 Increment::Increment(int c, inti) : count( c ), increment( I ) { ... } • 在构造函数参数列表之后、构造函数体之前 • 以:开头,以类似函数的形式书写,其中每个数据成员的名称为“函数名”,所需要初始化的值为“函数的参数”

  36. Member initializers appear between a constructor’s parameter list and the left brace that begins the constructor’s body.

  37. 初始化列表 • 构造函数+初始化列表 vs. 构造函数内赋值 • 初始化列表在构造体之前执行 • 对内置类型的数据成员没有什么大的区别 • 对用户自定义类型的数据成员,推荐使用类构造函数初始化列表 • 下列必须用带有初始化列表的构造函数: • 成员类型是没有默认构造函数的类。若类没有默认构造函数,则编译器尝试使用默认构造函数将会失败。 • const成员或引用类型的成员。因为const对象或引用类型只能初始化,不能对他们赋值。

  38. Chapter 18. Classes: A Deeper Look, II • 18.2 const (Constant) Objects and const Member Functions • 18.6 static Class Members • 18.3 Composition: Objects as Members of Classes • 18.4 friend Functions and friend Classes • 18.7 Data Abstraction and Information Hiding • 18.5 Using the this Pointer

  39. 18.6  static Class Members • A static data member静态数据成员 • In certain cases, only one copy of a variable should be shared by all objects of a class, such a variable represents “class-wide” information. 静态数据成员是在类的所有对象之间所共享的一个变量的唯一副本 • A class’s static members exist even when no objects of that class exist. 静态的数据成员不依赖于任何对象的创建,在程序开始运行后、没有对象被创建时就已经存在 • To access a public static class member when no objects of the class exist, prefix the class name and the binary scope resolution operator (::) to the name of the data member. 为了访问类的静态数据成员,需要采用类的作用域声明符::

  40. 18.6  static Class Members • A static data member 静态数据成员的初始化 • A fundamental-type staticdata member is initialized by default to 0. 缺省初始化为0 • If you want a different initial value, a staticdata member can be initialized once. • A staticconst data member of int or enumtype can be initialized in its declaration in the class definition. 整数类型的静态数据成员可以在类的定义中初始化 • All other staticdata members must be defined at global namespace scope and can be initialized only in those definitions. 其它静态数据成员需要在全局作用域内初始化 • If a staticdata member is an object of a class that provides a default constructor, the staticdata member need not be initialized because its default constructor will be called.

  41. 18.6  static Class Members • A static member function 静态的成员函数 • To access a private or protected static class member when no objects of the class exist, provide a public static member function and call the function by prefixing its name with the class name and binary scope resolution operator. • 为了访问静态的数据成员,需要通过公共的静态成员函数实现 • A static member function is a service of the class, not of a specific object of the class. • 静态的成员函数属于整个类,而非某个具体的对象

  42. A member function should be declared static if it does not access non-static data members or non-static member functions of the class.

  43. Experiment • 观察常量成员/静态成员/静态常量数据成员的区别 class Count { private: int a_; const int b_; static int c_; static const int d_ = 4; public: Count(int a = 1, int b = 2): a_(a), b_(b) { } void display ( void ) { cout << a_ << "," << b_ << "," << c_ << "," << d_ << endl; } void update ( void ) { a_ += 100; c_ += 100; } }; int Count::c_ = 3; void create( void ); int main() { Count countA; countA.display(); countA.update(); countA.display(); Count countB( 11, 22 ); countB.display(); countB.update(); countB.display(); return 0; }

  44. Chapter 18. Classes: A Deeper Look, II • 18.2 const (Constant) Objects and const Member Functions • 18.6 static Class Members • 18.3 Composition: Objects as Members of Classes • 18.4 friend Functions and friend Classes • 18.7 Data Abstraction and Information Hiding • 18.5 Using the this Pointer

  45. 18.4  friend Functions and friend Classes • friend function友元函数 • defined outside that class’s scope, yet has the right to access the non-public (and public) members of the class. 定义在类的范围之外的,可以访问类的非公共数据的函数 • Standalone functions, entire classes or member functions of other classes may be declared to be friends of another class. 可以声明为友元的包括函数、类、类的成员函数等 • The friendship relation is neither symmetric nor transitive. 友元的关系不是对称的,也不能传递 • Friendship is granted, not taken. 友元的关系是以在类的声明中授权方式进行的,函数不能自动获得 • Using friendfunctions can enhance performance.因为减少了数据的传递,友元函数的执行效率要高一些

More Related