`
`“STROUSTRUP.
`
`ByAtAETV
`Ye ai:(UAE
`THIRD)peur _
`/
`
`Booking, Exh. 1043, Page 1
`
`Booking, Exh. 1043, Page 1
`
`
`
`The
`CH
`Programming
`Language
`Third Edition
`
`Bjarne Stroustrup
`
`AT&T Labs
`Murray Hill, New Jersey
`
`A
`vv
`Addison-Wesley
`An Imprint of Addison Wesley Longman,Inc.
`Reading, Massachusetts + Harlow, England + Menlo Park, California
`Berkeley, California - Don Mills, Ontario - Sydney
`Bonn - Amsterdam + Tokyo + Mexico City
`
`Booking, Exh. 1043, Page 2
`
`Booking, Exh. 1043, Page 2
`
`
`
`Manyof the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where
`those designations appear in this book, and Addison-Wesley was aware of a trademark claim,the designations have been
`printedin initial capital letters or all capital letters
`
`The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any
`kind and assumeno responsibility for errors or omissions. No liability is assumedfor incidentalor consequential damagesin
`connection with or arising outof the use of the information contained herein.
`.
`
`Thepublisheroffers discounts on this book when ordered in quantity for special sales. For more information please contact:
`Corporate & Professional Publishing Group
`:
`Addison-Wesley Publishing Company
`One Jacob Way
`;
`Reading, Massachusetts 01867
`
`Library of Congress Cataloging-in-Publication Data
`
`Stroustrup, Bjarne
`The C++ Programming Language / Bjarne Stroustrup. — 3rd. ed.
`Pp.
`cm.
`Includes index.
`ISBN 0-201-88954-4
`
`1, C++ (Computer Programming Language)I. Title
`QA76.73.C153877
`1997
`005.13°3—dce21
`
`97-20239
`cIP
`
`(i)a
`
`Copyright © 1997 by AT&T
`
`Ali rights reserved. No part of this publication may be reproduced,stored in a retrieval system,or transmitted, in any form or
`by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the
`publisher. Printed in the United States of America.
`
`This book was typeset in Times and Courier by the author.
`
`ISBN 0-20-88954-4
`
`Printed on recycled paper
`53678910 CRS 01 00 99 98
`
`Sth Printing January, 1998
`
`Booking, Exh. 1043, Page 3
`
`Booking, Exh. 1043, Page 3
`
`
`
`
`
`
`
`A
`
`Types and Declarations
`
`Accept nothing short ofperfection!
`— anon
`
`Perfection is achieved
`only on the point ofcollapse.
`— C.N. Parkinson
`
`Types — fundamental types — Booleans — characters — characterliterals — integers
`— integerliterals — floating-point types — floating-point literals — sizes — void —
`enumerations — declarations — names — scope — initialization — objects — typedefs
`— advice — exercises.
`
`4.1 Types
`Consider
`
`“x= yrf(2);
`
`For this to make sense in a C++ program, the names x, y, and f must be suitably declared. That is,
`the programmer must specify that entities named x, y, and f exist and that they are of types for
`which = (assignment), + (addition), and () (function call), respectively, are meaningful.
`Every name (identifier) in a C++ program has a type associated with it. This type determines
`what operations can be applied to the name (thatis, to the entity referred to by the name) and how
`such operations are interpreted. For example, the declarations
`
`float x;
`int y= 7;
`float f(int);
`
`// xis a floating-point variable
`// y is an integer variable with the initial value 7
`// fis afunction taking an argumentoftype int and returning afloating-point number
`
`Booking, Exh. 1043, Page 4
`
`Booking, Exh. 1043, Page 4
`
`
`
`70
`
`Types and Declarations
`
`Chapter 4
`
`would make the example meaningful. Because y is declared to be an int, it can be assignedto, used
`in arithmetic expressions, etc. On the other hand, f is declared to be a function that takes an int as
`its argument, so it can be called given a suitable argument.
`Its examples just
`This chapter presents fundamental types (§4.1.1) and declarations (§4.9).
`demonstrate language features; they are not intended to do anything useful. More extensive and
`realistic examples are saved for later chapters after more of C++ has been described. This chapter
`simply provides the most basic elements from which C++ programs are constructed. You must
`know these elements, plus the terminology and simple syntax that goes with them, in order to com-
`plete a real project in C++ andespecially to read code written by others. However, a thorough
`understanding of every detail mentionedin this chapter is not a requirement for understanding the
`following chapters. Consequently, you may prefer to skim through this chapter, observing the
`major concepts, and return later as the need for understanding of more details arises.
`
`4.1.1 Fundamental Types
`C++ has a set of fundamental types corresponding to the most common basic storage units of a
`computer and the most common ways of using them to hold data:
`$4.2 A Boolean type (bool)
`§4.3 Character types (such as char)
`§4.4 Integer types (such as int)
`§4.5 Floating-point types (such as double)
`In addition, a user can define
`§4.8 Enumeration types for representing specific sets of values (enum)
`There also is
`$4.7 A type, void, used to signify the absence of information
`From these types, we can construct other types:
`§5.1 Pointer types (such as int*)
`§5.2 Array types (such as char[])
`__
`§5.5 Reference types (such as double&)
`§5.7 Data structures and classes (Chapter 10)
`TheBoolean, character, and integer types are collectively called integral types. The integral and
`floating-point types are collectively called arithmetic types. Enumerations and classes (Chapter 10)
`are called user-defined types because they must be defined by users rather than being available for
`use withoutprevious declaration, the way fundamental typesare. In contrast, other types are called
`built-in types.
`The integral and floating-point types are provided in a variety of sizes to give the programmer a
`choice of the amountof storage consumed, the precision, and the range available for computations
`($4.6). The assumption is that a computer provides bytes for holding characters, words for holding
`and computing integer values, some entity most suitable for floating-point computation, and
`addresses for referring to those entities. The C++ fundamental types together with pointers and
`arrays present these machine-level notions to the programmerin a reasonably implementation-
`independent manner.
`For most applications, one could simply use boolfor logical values, char for characters, int for
`integer values, and double for floating-point values. The remaining fundamental
`types are
`
`Booking, Exh. 1043, Page 5
`
`Booking, Exh. 1043, Page 5
`
`
`
`Section 4.1.1
`
`Fundamental Types
`
`71
`
`variations for optimizations and special needs that are best ignored until such needs arise. They
`must be known, however, to read old C and C++ code.
`
`4.2 Booleans
`
`A Boolean, bool, can have one of the two values true or false. A Boolean is used to express the
`results of logical operations. For example:
`void flint a, int b)
`{
`
`bool bl = a==b;
`ff.
`
`// = is assignment, == is equality
`
`}
`
`If a and b havethe same value, b] becomes true; otherwise, b! becomesfalse.
`A commonuse of bool is as the type of the resultof afunction that tests some condition (a
`predicate). For example:
`
`bool is_open(File* );
`
`bool greater (int a, int b)
`
`{ return a>b; }
`
`By definition, true has the value J when converted to an integer andfalse bihas the value 0. Con-
`versely, integers can be implicitly converted to bool values: nonzero integers convert to trueand 0
`converts to false. For example:
`
`bool b=7;
`int i = true;
`
`// bool(7) is true, so b becomes true
`// int(true) is 1, so i becomes I
`
`In arithmetic and logical expressions, bools are converted to ints; integer arithmetic and logical
`operations are performed on the converted values. If the result is converted back to bool; a 0 is
`converted tofalse and a nonzero value is converted to true.
`
`void g()
`{
`
`bool a-= true;
`bool b = true;
`
`bool x=a+b;
`bool y=a\|b;
`
`// at+bis 2, so x becomes true
`// albis 1, so y becomes true
`
`}
`
`A pointer can be implicitly converted to a bool (SC.6.2.5}. A nonzero pointer converts to true;
`
`zero-valued pointers convert to false.
`
`4.3 Character Types
`A variable of type char can hold a character of the implementation’s character set. For example:
`char ch = ‘a’
`
`Booking, Exh. 1043, Page 6
`
`Booking, Exh. 1043, Page 6
`
`
`
`72
`
`‘Types and Declarations
`
`Chapter 4
`
`Almost universally, a char has8 bits so that it can hold one of 256 different values. Typically, the
`character set is a variant of ISO-646, for example ASCII, thus providing the characters appearing
`on your keyboard. Many problemsarise from the fact that this set of characters is only partially
`standardized (§C.3).
`Serious variations occur between character sets supporting different natural languages and also
`between different character sets supporting the same natural languagein different ways. However,
`here we are interested only in how such differences affect the rules of C++. The larger and more
`interesting issue of how to program in a multi-lingual, multi-character-set environment is beyond
`the scope of this book, although it is alluded to in several places (§20.2, §21.7, §C.3.3).
`It is safe to assume that the implementation character set includes the decimal digits, the 26
`alphabetic characters of English, and some of the basic punctuation characters.
`It is not safe to
`assume that there are no more than 127 characters in an 8-bit characterset(e.g., some sets provide
`255 characters), that there are no more alphabetic characters than English provides (most European
`languages provide more), that the alphabetic characters are contiguous (EBCDIC leaves a gap
`between “i” and “j), or that every character used to write C++ is available (e.g., some national
`character sets do not provide {
`}
`[
`]
`|
`\; §C.3.1)..- Whenever possible, we should avoid making
`assumptions about the representation of objects. This general rule applies even to characters.
`Each character constant has an integer value. For example, the value of “b’ is 98 in the ASCII
`character set. Here is a small program that will tell you the integer value of any character you care
`to input:
`#include <iostream>
`
`int main ({)
`{
`
`char c;
`Std::cin >> c;
`std::cout << "the value of “" <<c <<" is " << int(c) << ‘Wn’;
`
`}
`
`The notation int(c) gives the integer value for a character c. The possibility of converting a char
`to an integer raises the question: is a char signed or unsigned? The 256 values represented by an
`8-bit byte can be interpreted as the values 0 to 255 or as the values -127 to 127. Unfortunately,
`which choice is made for a plain char is implementation-defined ($C.1, §C.3.4). C++ provides two
`types for which the answeris definite; signed char, which can hold at least the values - 127 to 127,
`and unsigned char, which can holdatleast the values 0 to 255. Fortunately, the difference matters
`only for values outside the 0 to 127 range, and the most common characters are within that range,
`Values outside that range stored in a plain char can lead to subtle portability problems. See
`§C.3.4 if you need to use more than onetype of charorif you store integers in char variables.
`A type wehar_t is provided to hold characters of a larger character set such as Unicode. It is a
`distinct type. The size of wchar_t is implementation-defined and large enough to hold the largest
`character set supported by the implementation’s locale (see §21.7, §C.3.3). The strange name is a
`leftover from C. In C, wchar_t is a typedef(§4.9.7) rather than a built-in type. The suffix _t was
`addedto distinguish standard typedefs.
`Note that the character types are integral types (§4.1.1) so thatarithmetic and logical operations
`($6.2) apply.
`
`Booking, Exh. 1043, Page 7
`
`Booking, Exh. 1043, Page 7
`
`
`
`Section 4.3.1
`
`Character Literals
`
`73
`
`4.3.1 Character Literals
`
`A characterliteral, often called a character constant, is a character enclosed in single quotes, for
`example, ‘a’ and ‘0’. The type of a character literal is char. Such characterliterals are really
`symbolic constants for the integer value of the characters in the character set of the machine on
`which the C++ program is to run. For example, if you are running on a machine using the ASCII
`character set, the value of “0° is 48. The use of character literals rather than decimal notation
`makes programs more portable. A few characters also have standard namesthat use the backslash\
`as an escape character. For example, \n is a newline and \r is a horizontal tab. See §C.3.2 for
`details about escape characters.
`Wide characterliterals are of the form L’ab‘’, where the number of characters between the
`quotes and their meanings is implementation-defined to match the wehar_t type. A wide character
`literal has type wehar_t.
`,
`
`4.4 Integer Types
`Like char, each integer type comes in three forms: ‘‘plain’’ int, signed int, and unsigned int. In
`addition, integers come in three sizes: short int, ‘‘plain’’ int, and long int. A long int can be
`referred to as plain long. Similarly, short is a synonym for short int, unsigned for unsigned int,
`and signed for signed int.
`The unsigned integer types are ideal for uses that treat storage as a bit array. Using an
`unsigned instead of an int to gain one more bit to represent positive integers is almost never a good
`idea. Attempts to ensure that some values are positive by declaring variables unsigned will typi-
`cally be defeated by the implicit conversion rules (§C.6.1, §C.6.2.1).
`Unlike plain chars, plain ints are always signed. The signed int types are simply more explicit
`synonymsfortheir plain int counterparts.
`
`4.4.1 Integer Literals
`
`Integerliterals come in four guises: decimal, octal, hexadecimal, and character literals. Decimal lit-
`erals are the most commonly used and look as you would expect them to:
`
`12345678901234567890
`976
`0 1234
`The compiler ought to warn aboutliterals that are too long to represent.
`A literal starting with zero followed by x (Ox) 1s a hexadecimal (base 16) number. A literal
`starting with zero followed by a digit is an octal (base 8) number. For example:
`decimal :
`0
`2
`63
`83
`octal
`00
`02
`077
`0123
`hexadecimal:
`0x0
`Ox2
`Ox3f
`Ox53
`
`Theletters a, b, c, d, e, and f, or their uppercase. equivalents, are used to represent 10, 11, 12, 13,
`14, and 15, respectively. Octal and hexadecimal notations are most useful for expressing bit pat-
`terns. Using these notations to express genuine numbers can lead to surprises. For example, on a
`machine on which an int is represented as a two’s complement 16-bit integer, Oxffff is the negative
`decimal number -/. Had more bits been used to represent an integer, it would have been 65535.
`
`Booking, Exh. 1043, Page 8
`
`Booking, Exh. 1043, Page 8
`
`
`
`74
`
`Typesand Declarations
`
`_
`
`Chapter 4
`
`The suffix U can be used to write explicitly unsignedliterals. Similarly, the suffix L can be
`used to write explicitly long literals. For example, 3 is an int, 3U is an unsigned int, and 3L is‘a
`long int. If no suffix is provided, the compiler gives an integerliteral a suitable type based onits
`value and the implementation’s integer sizes (§C.4).
`It is a good ideato limit the use of nonobviousconstants to a few well-commented const (85.4)
`or enumerator (§4.8) initializers.
`
`4.5 Floating-Point Types
`
`The floating-point types represent floating-point numbers, Like integers, floating-point types come
`in three sizes: float (single-precision), double (double-precision), and long double (extended-—
`precision).
`The exact meaning of single-, double-, and extended-precision is implementation-defined.
`Choosing the right precision for a problem where the choice matters requires significant under-
`standing of floating-point computation. If you don’t have that understanding, get advice, take the
`time to learn, or use double and hope for the best.
`
`4.5.1 Floating-Point Literals
`
`By default, a floating-point literal is of type double. Again, a compiler ought to warn about
`floating-pointliterals that are too large to be represented. Here are somefloating-pointliterals:
`1.23)
`.23, 0.23 2.
`1.0 1.2e10 1.23e-15
`
`Note that a space cannotoccurin the middle ofa floating-pointliteral. For example, 65.43 e-21
`is not a floating-pointliteral but rather four separate lexical tokens (causing a syntax error):
`65.43 e
`-
`2]
`
`If you wanta floating-pointliteral of type float, you can define oneusing the suffixforF:_
`3.14159265f 2.0f 2.997925F
`
`4.6 Sizes
`
`Some of the aspects of Ct++’s fundamental types, such as the size of an int, are implementation-
`defined (§C.2). I point out these dependencies and often recommendavoiding them ortaking steps
`to minimize their impact. Why should you bother? People who program ona variety of systemsor
`use a variety of compilers care a lot becauseif they don’t, they are forced to waste time finding and
`fixing obscure bugs. People who claim they don’t care aboutportability usually do so because they
`use only a single system andfeel they can afford the attitude that ‘‘the language is what my com-
`piler implements.’’ This is a narrow and shortsighted view.
`If your program is a success,it is
`likely to be ported, so someone will have to find and fix problems related to implementation-
`dependentfeatures.
`In addition, programs often need to be compiled with other compilers for the
`same system, and even a future release of your favorite compiler may do some things differently
`from the current one. It is far easier to know and limit the impact of implementation dependencies
`
`Booking, Exh. 1043, Page 9
`
`Booking, Exh. 1043, Page 9
`
`
`
`Section 4.6
`
`;
`
`Sizes
`
`75
`
`when a program is written than to try to untangle the mess afterwards.
`It is relatively easy to limit the impact of implementation-dependent language features. Limit-
`ing the impact of system-dependentlibrary facilities is far harder. Using standard library facilities
`whereverfeasible is one approach.
`The reason for providing more than one integer type, more than one unsigned type, and more
`than one floating-point type is to allow the programmerto take advantage of hardware characteris-
`tics. On many machines, there are significant differences in memory requirements, memory access
`times, and computation speed between the different varieties of fundamental types. If you know a
`machine, it is usually easy to choose, for example, the appropriate integer type for a particular vari-
`able. Writing truly portable low-level code is harder.
`.
`Sizes of C++ objects are expressed in terms of multiples of the size of a char, so by definition
`the size of a char is J. The size of an object or type can be obtained using the sizeof operator
`($6.2). This is what is guaranteed aboutsizes of fundamental types:
`,
`1 = sizeof(char) = sizeof(short) = sizeof(int) < sizeof(long)
`
`1S sizeof(bool) = sizeof{long)
`
`sizeof(char) < sizeof{(wchar_t) < sizeof{long)
`
`sizeof(float) < sizeof(double) < sizeof(long double)
`
`sizeof(N) = sizeof(signed N) = sizeof(unsigned N)
`
`In addition, it is guaranteed that a char has at least
`where N can be char, short int, int, or long int.
`8 bits, a short at least 16 bits, and a long at least 32 bits. A char can hold a character of the
`machine’s characterset.
`Hereis a graphical representation ofa plausible set of fundamental types and a samplestring:
`
`[aj
`
`char:
`
`bool:
`
`short:
`
`756
`
`int*:
`
`&cl
`
`chat:
`
`On the same scale (.2 inch to a byte), a megabyte of memory wouldstretch about three miles (five
`km)to the right.
`
`Booking, Exh. 1043, Page 10
`
`Booking, Exh. 1043, Page 10
`
`
`
`76
`
`Types and Declarations
`
`.
`
`Chapter 4
`
`The chartype is supposed to be chosen by the implementation to be the most suitable type for
`holding and manipulating characters on a given computer;it is typically an 8-bit byte. Similarly,
`the int type is supposed to be chosen to be the most suitable for holding and manipulating integers
`on a given computer;it is typically a 4-byte (32-bit) word. It is unwise to assume more. For exam-
`ple, there are machines with 32 bit chars.
`When needed, implementation-dependent aspects about an implementation canbe found in
`<limits> (§22.2). For example:
`#include <limits>
`
`int main ()
`{
`
`cout << "largest float == " << numeric_limits<float> ::max()
`<<", char is signed == " << numeric_limits<char>::is_signed << ‘\n‘;
`
`}
`
`Thefundamental types can be mixed freely in assignments and expressions. Wherever Possible,
`values are converted so as notto lose information (§C.6).
`If a value v can be represented exactly in a variable of type T, a conversion of v to T is value-
`preserving and no problem. The cases where conversions are not value-preserving are best avoided
`(§C.6.2.6).
`You need to understand implicit conversion in somedetail in order to complete a majorproject.
`and especially to understand real code written by others. However, such understanding is not
`required to read the following chapters.
`
`4.7 Void
`
`The type. void is syntactically a fundamental type. It can, however, be used only as part of a more
`complicated type; there are no objects of type void. It is used either to specify that a function does
`not return a value oras the base type for pointers to objects of unknown type. For example:
`void x;
`// error: there are no void objects
`void f();
`// functionf does not return a value ($7.3)
`void* pv;
`// pointer to object of unknown type (§5.6)
`
`-
`
`When declaring a function, you mustspecify the type of the value returned. Logically, you would
`expect to be able to indicate that a function didn’t return a value by omitting the return type. How-
`ever, that would make the grammar (Appendix A) less regular and clash with C usage. Conse-
`quently, void is used as a “‘pseudo return type’’ to indicate that a function doesn’t return a value.
`
`4.8 Enumerations
`
`An enumeration is a type that can hold a set of values specified by the user. Once defined, an enu-
`meration is used very much like an integertype.
`Namedinteger constants can be defined as members of an enumeration. For example,
`enum { ASM, AUTO, BREAK };
`
`Booking, Exh. 1043, Page 11
`
`Booking, Exh. 1043, Page 11
`
`