Team LiB
Previous Section Next Section

Chapter 2. Variables and Basic Types

第二章 变量和基本类型

CONTENTS

Section 2.1 Primitive Built-in Types

34

Section 2.2 Literal Constants

37

Section 2.3 Variables

43

Section 2.4 const Qualifier

56

Section 2.5 References

58

Section 2.6 Typedef Names

61

Section 2.7 Enumerations

62

Section 2.8 Class Types

63

Section 2.9 Writing Our Own Header Files

67

Chapter Summary

73

Defined Terms

73


Types are fundamental to any program. They tell us what our data mean and what operations we can perform on our data.

类型是所有程序的基础。类型告诉我们数据代表什么意思以及可以对数据执行哪些操作。

C++ defines several primitive types: characters, integers, floating-point numbers, and so on. The language also provides mechanisms that let us define our own data types. The library uses these mechanisms to define more complex types such as variable-length character strings, vectors, and so on. Finally, we can modify existing types to form compound types. This chapter covers the built-in types and begins our coverage of how C++ supports more complicated types.

C++ 语言定义了几种基本类型:字符型、整型、浮点型等。C++ 还提供了可用于自定义数据类型的机制,标准库正是利用这些机制定义了许多更复杂的类型,比如可变长字符串 stringvector 等。此外,我们还能修改已有的类型以形成复合类型。本章介绍内置类型,并开始介绍 C++ 如何支持更复杂的类型。

Types determine what the data and operations in our programs mean. As we saw in Chapter 1, the same statement

类型确定了数据和操作在程序中的意义。我们在第一章已经看到,如下的语句

     i =i +j;

can mean different things depending on the types of i and j. If i and j are integers, then this statement has the ordinary, arithmetic meaning of +. However, if i and j are Sales_item objects, then this statement adds the components of these two objects.

有不同的含义,具体含义取决于 ij 的类型。如果 ij 都是整型,则这条语句表示一般的算术“+”运算;如果 ij 都是 Sales_item 对象,则这条语句是将这两个对象的组成成分分别加起来。

In C++ the support for types is extensive: The language itself defines a set of primitive types and ways in which we can modify existing types. It also provides a set of features that allow us to define our own types. This chapter begins our exploration of types in C++ by covering the built-in types and showing how we associate a type with an object. It also introduces ways we can both modify types and can build our own types.

C++ 中对类型的支持是非常广泛的:语言本身定义了一组基本类型和修改已有类型的方法,还提供了一组特征用于自定义类型。本章通过介绍内置类型和如何关联类型与对象来探讨 C++ 中的类型。本章还将介绍更改类型和建立自定义类型的方法。

Team LiB
Previous Section Next Section