Team LiB
Previous Section Next Section

Part III: Classes and Data Abstraction


Classes are central to most C++ programs: Classes let us define our own types that are customized for the problems we need to solve, resulting in applications that are easier to write and understand. Well-designed class types can be as easy to use as the built-in types.

在大多数 C++ 程序中,类都是至关重要的:我们能够使用类来定义为要解决的问题定制的数据类型,从而得到更加易于编写和理解的应用程序。设计良好的类类型可以像内置类型一样容易使用。

A class defines data and function members: The data members store the state associated with objects of the class type, and the functions perform operations that give meaning to the data. Classes let us separate implementation and interface. The interface specifies the operations that the class supports. Only the implementor of the class need know or care about the details of the implementation. This separation reduces the bookkeeping aspects that make programming tedious and error-prone.


Class types often are referred to as abstract data types. An abstract data type treats the data (state) and operations on that state as a single unit. We can think abstractly about what the class does, rather than always having to be aware of how the class operates. Abstract data types are fundamental to both object-oriented and generic programming.

类类型常被称为抽象数据类型(abstract data types)。抽象数据类型将数据(即状态)和作用于状态的操作视为一个单元。我们可以抽象地考虑类该做什么,而无须知道类如何去完成这些操作。抽象数据类型是面向对象编程和泛型编程的基础。

Chapter 12 begins our detailed coverage of how classes are defined. This chapter covers topics fundamental to any use of classes: class scope, data hiding, and constructors. It also introduces some new class features: friends, uses of the implicit this pointer, and the role of static and mutable members.

第十二章开始详细地介绍如何定义类,包括类的使用中非常基本的主题:类作用域、数据隐藏和构造函数。此外,还介绍了类的一些新特征:友元、使用隐含的 this 指针,以及静态(static)可变(mutable)成员的作用。

Classes in C++ control what happens when objects are initialized, copied, assigned, and destroyed. In this respect, C++ differs from many other languages, many of which do not give class designers the ability to control these operations. Chapter 13 covers these topics.

C++ 中的类能够控制在初始化、复制、赋值和销毁对象时发生的操作。在此方面,C++ 不同于许多其他语言,它们大多没有赋予类设计者控制这些操作的能力。第十三章讨论了这些主题。

Chapter 14 looks at operator overloading, which allows operands of class types to be used with the built-in operators. Operator over-loading is one of the ways whereby C++ lets us create new types that are as intuitive to use as are the built-in types. This chapter also presents another special kind of class member functionconversion functionswhich define implicit conversions from objects of class type. The compiler applies these conversions in the same contextsand for the same reasonsas it does with conversions among the built-in types.

第十四章考察了操作符重载,允许将类类型的操作数与内置操作符一起使用。利用操作符重载,在 C++ 中创建新的类型,就像创建内置类型一样。此外,还介绍了另一种特殊的类成员函数——转换函数,这种函数定义了类类型对象之间的隐式转换。编译器应用这些转换就像它们是在内置类型之间发生的转换一样。


Chapter 12 Classes



Chapter 13 Copy Control

第十三章 复制控制


Chapter 14 Overloaded Operations and Conversions

第十四章 重载操作符与转换

Team LiB
Previous Section Next Section