Chapter 12. Classes第十二章 类目录
In C++ we use classes to define our own abstract data types. By defining types that mirror concepts in the problems we are trying to solve, we can make our programs easier to write, debug, and modify. 在 C++ 中,用类来定义自己的抽象数据类型(abstract data types)。通过定义类型来对应所要解决的问题中的各种概念,可以使我们更容易编写、调试和修改程序。 This chapter continues the coverage of classes begun in Chapters 2 and 5. We'll cover in more detail the importance of data abstraction, which lets us hide the internal representation of an object while still allowing public operations to be performed on the object. 本章进一步讨论类,并将更详细地阐述数据抽象的重要性。数据抽象能够隐藏对象的内部表示,同时仍然允许执行对象的公有(public)操作。 We'll also explain more about class scope, constructors, and the this pointer. We also introduce three new class-related features: friends, and mutable and static members. 我们也将进一步解释类作用域、构造函数以及 this 指针。此外,还要介绍与类有关的三个新特征:友元(friend)、可变成员(mutable)、和静态成员(static)。 Classes are the most important feature in C++. Early versions of the language were named "C with Classes," emphasizing the central role of the class facility. As the language evolved, support for building classes increased. A primary goal of the language design has been to provide features that allow programmers to define their own types that are as easy and intuitive to use as the built-in types. This chapter presents many of the basic features of classes. 类是 C++ 中最重要的特征。C++ 语言的早期版本被命名为“带类的 C(C with Classes)”,以强调类机制的中心作用。随着语言的演变,创建类的配套支持也在不断增加。语言设计的主要目标也变成提供这样一些特性:允许程序定义自己的类型,它们用起来与内置类型一样容易和直观。本章将介绍类的许多基本特征。 ![]() |