CHAPTER 2 VARIABLES AND BASICTYPES

2013-10-07 16:15:40 · 作者: · 浏览: 81

CONTENTS

Section 2.1 Primitive Built-in Types . . . . . . . . . . . 32

Section 2.2 Variables . . . . . . . . . . . . . . . . . . . . 41

Section 2.3 Compound Types . . . . . . . . . . . . . . . 50

Section 2.4 constQualifier . . . . . . . . . . . . . . . . 59

Section 2.5 Dealing with Types . . . . . . . . . . . . . . 67

Section 2.6 Defining Our Own Data Structures . . . . 72

ChapterSummary . . . . . . . . . . . . . . . . . . . . . . 78

DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . 78

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

C++(www.cppentry.com) has extensive support for types. The language defines several primitive types (characters, integers, floating-point numbers, etc.) and provides mechanisms that let us define our own data types. The library uses these mechanisms to define more complicated types such as variable-length character strings, vectors, and so on. This chapter covers the built-in types and begins our coverage of how C++(www.cppentry.com) supports more complicated types.
Types determine the meaning of the data and operations in our programs. The meaning of even as simple a statement as

  1. i = i + j;  

depends on the types of i and j. If i and j are integers, this statement has the ordinary, arithmetic meaning of +. However, if i and j are Sales_item objects (§ 1.5.1, p. 20), this statement adds the components of these two objects.