Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Beginning C++17
Nội dung xem thử
Mô tả chi tiết
Beginning
C++17
From Novice to Professional
—
Fifth Edition
—
Ivor Horton
Peter Van Weert
Beginning C++17
From Novice to Professional
Fifth Edition
Ivor Horton
Peter Van Weert
Beginning C++17: From Novice to Professional
Ivor Horton Peter Van Weert
Stratford-upon-Avon, Warwickshire, United Kingdom Kessel-Lo, Belgium
ISBN-13 (pbk): 978-1-4842-3365-8 ISBN-13 (electronic): 978-1-4842-3366-5
https://doi.org/10.1007/978-1-4842-3366-5
Library of Congress Control Number: 2018936369
Copyright © 2018 by Ivor Horton and Peter Van Weert
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage
and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or
hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material
contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).
SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or audio
rights, please email [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions
and licenses are also available for most titles. For more information, reference our Print and eBook Bulk
Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484233658. For more
detailed information, please visit www.apress.com/source-code.
Printed on acid-free paper
This is for Alexander and Henry who are both going to learn programming soon.
If their amazing expertise with Minecraft is anything to go by,
they will be brilliant at it.
—Ivor Horton
For my wonderful family. For all your love and support. For putting up with
me never having the time to help out around the house or to play
with the train set I got you for Christmas.
—Peter Van Weert
v
Contents
About the Authors�������������������������������������������������������������������������������������������������xxiii
About the Technical Reviewer �������������������������������������������������������������������������������xxv
Introduction���������������������������������������������������������������������������������������������������������xxvii
■Chapter 1: Basic Ideas������������������������������������������������������������������������������������������� 1
Modern C++��������������������������������������������������������������������������������������������������������������������� 1
Standard Libraries������������������������������������������������������������������������������������������������������������ 2
C++ Program Concepts���������������������������������������������������������������������������������������������������� 3
Source Files and Header Files ���������������������������������������������������������������������������������������������������������������� 3
Comments and Whitespace �������������������������������������������������������������������������������������������������������������������� 4
Preprocessing Directives and Standard Library Headers ����������������������������������������������������������������������� 4
Functions������������������������������������������������������������������������������������������������������������������������������������������������ 5
Statements ��������������������������������������������������������������������������������������������������������������������������������������������� 5
Data Input and Output����������������������������������������������������������������������������������������������������������������������������� 6
return Statements����������������������������������������������������������������������������������������������������������������������������������� 7
Namespaces������������������������������������������������������������������������������������������������������������������������������������������� 7
Names and Keywords����������������������������������������������������������������������������������������������������������������������������� 8
Classes and Objects ��������������������������������������������������������������������������������������������������������� 9
Templates������������������������������������������������������������������������������������������������������������������������� 9
Code Appearance and Programming Style����������������������������������������������������������������������� 9
Creating an Executable �������������������������������������������������������������������������������������������������� 10
Procedural and Object-Oriented Programming �������������������������������������������������������������� 12
■ Contents
vi
Representing Numbers��������������������������������������������������������������������������������������������������� 13
Binary Numbers������������������������������������������������������������������������������������������������������������������������������������ 13
Hexadecimal Numbers ������������������������������������������������������������������������������������������������������������������������� 14
Negative Binary Numbers��������������������������������������������������������������������������������������������������������������������� 16
Octal Values ������������������������������������������������������������������������������������������������������������������������������������������ 17
Bi-Endian and Little-Endian Systems ��������������������������������������������������������������������������������������������������� 18
Floating-Point Numbers������������������������������������������������������������������������������������������������������������������������ 19
Representing Characters������������������������������������������������������������������������������������������������ 20
ASCII Codes ������������������������������������������������������������������������������������������������������������������������������������������ 21
UCS and Unicode ���������������������������������������������������������������������������������������������������������������������������������� 21
C++ Source Characters�������������������������������������������������������������������������������������������������� 22
Escape Sequences ������������������������������������������������������������������������������������������������������������������������������� 22
Summary������������������������������������������������������������������������������������������������������������������������ 24
■Chapter 2: Introducing Fundamental Types of Data �������������������������������������������� 27
Variables, Data, and Data Types ������������������������������������������������������������������������������������� 27
Defining Integer Variables��������������������������������������������������������������������������������������������������������������������� 28
Zero Initialization ���������������������������������������������������������������������������������������������������������������������������������� 31
Defining Variables with Fixed Values���������������������������������������������������������������������������������������������������� 32
Integer Literals ��������������������������������������������������������������������������������������������������������������� 32
Decimal Integer Literals������������������������������������������������������������������������������������������������������������������������ 32
Hexadecimal Literals ���������������������������������������������������������������������������������������������������������������������������� 33
Octal Literals����������������������������������������������������������������������������������������������������������������������������������������� 34
Binary Literals��������������������������������������������������������������������������������������������������������������������������������������� 34
Calculations with Integers���������������������������������������������������������������������������������������������� 35
Compound Arithmetic Expressions������������������������������������������������������������������������������������������������������� 36
Assignment Operations �������������������������������������������������������������������������������������������������� 37
The op= Assignment Operators������������������������������������������������������������������������������������������������������������ 40
The sizeof Operator�������������������������������������������������������������������������������������������������������� 42
■ Contents
vii
Incrementing and Decrementing Integers���������������������������������������������������������������������� 42
Postfix Increment and Decrement Operations�������������������������������������������������������������������������������������� 43
Defining Floating-Point Variables ����������������������������������������������������������������������������������� 44
Floating-Point Literals ���������������������������������������������������������������������������������������������������� 45
Floating-Point Calculations �������������������������������������������������������������������������������������������� 46
Pitfalls��������������������������������������������������������������������������������������������������������������������������������������������������� 46
Invalid Floating-Point Results��������������������������������������������������������������������������������������������������������������� 47
Mathematical Functions ����������������������������������������������������������������������������������������������������������������������� 48
Formatting Stream Output���������������������������������������������������������������������������������������������� 51
Mixed Expressions and Type Conversion������������������������������������������������������������������������ 53
Explicit Type Conversion������������������������������������������������������������������������������������������������� 54
Old-Style Casts ������������������������������������������������������������������������������������������������������������������������������������� 56
Finding the Limits����������������������������������������������������������������������������������������������������������� 57
Finding Other Properties of Fundamental Types����������������������������������������������������������������������������������� 58
Working with Character Variables ���������������������������������������������������������������������������������� 59
Working with Unicode Characters �������������������������������������������������������������������������������������������������������� 60
The auto Keyword ���������������������������������������������������������������������������������������������������������� 61
Summary������������������������������������������������������������������������������������������������������������������������ 62
■Chapter 3: Working with Fundamental Data Types���������������������������������������������� 65
Operator Precedence and Associativity�������������������������������������������������������������������������� 65
Bitwise Operators����������������������������������������������������������������������������������������������������������� 67
The Bitwise Shift Operators������������������������������������������������������������������������������������������������������������������ 68
Logical Operations on Bit Patterns ������������������������������������������������������������������������������������������������������� 71
Enumerated Data Types�������������������������������������������������������������������������������������������������� 77
Aliases for Data Types ���������������������������������������������������������������������������������������������������� 80
The Lifetime of a Variable����������������������������������������������������������������������������������������������� 81
Global Variables�������������������������������������������������������������������������������������������������������������� 82
Summary������������������������������������������������������������������������������������������������������������������������ 85
■ Contents
viii
■Chapter 4: Making Decisions������������������������������������������������������������������������������� 89
Comparing Data Values �������������������������������������������������������������������������������������������������� 89
Applying the Comparison Operators����������������������������������������������������������������������������������������������������� 90
Comparing Floating-Point Values ��������������������������������������������������������������������������������������������������������� 92
The if Statement������������������������������������������������������������������������������������������������������������� 92
Nested if Statements���������������������������������������������������������������������������������������������������������������������������� 96
Character Classification and Conversion���������������������������������������������������������������������������������������������� 97
The if-else Statement����������������������������������������������������������������������������������������������������� 99
Nested if-else Statements������������������������������������������������������������������������������������������������������������������ 101
Understanding Nested ifs ������������������������������������������������������������������������������������������������������������������� 102
Logical Operators ��������������������������������������������������������������������������������������������������������� 103
Logical AND ���������������������������������������������������������������������������������������������������������������������������������������� 104
Logical OR������������������������������������������������������������������������������������������������������������������������������������������� 104
Logical Negation��������������������������������������������������������������������������������������������������������������������������������� 105
Combining Logical Operators ������������������������������������������������������������������������������������������������������������� 105
Logical Operators on Integer Operands ���������������������������������������������������������������������������������������������� 107
Logical Operators vs. Bitwise Operators �������������������������������������������������������������������������������������������� 108
The Conditional Operator���������������������������������������������������������������������������������������������� 110
The switch Statement �������������������������������������������������������������������������������������������������� 112
Fallthrough ����������������������������������������������������������������������������������������������������������������������������������������� 116
Statement Blocks and Variable Scope�������������������������������������������������������������������������� 118
Initialization Statements��������������������������������������������������������������������������������������������������������������������� 119
Summary���������������������������������������������������������������������������������������������������������������������� 120
■Chapter 5: Arrays and Loops ����������������������������������������������������������������������������� 123
Arrays ��������������������������������������������������������������������������������������������������������������������������� 123
Using an Array ������������������������������������������������������������������������������������������������������������������������������������ 123
Understanding Loops ��������������������������������������������������������������������������������������������������� 125
The for Loop ����������������������������������������������������������������������������������������������������������������� 126
Avoiding Magic Numbers ��������������������������������������������������������������������������������������������� 128
Defining the Array Size with the Braced Initializer������������������������������������������������������� 130
■ Contents
ix
Determining the Size of an Array ��������������������������������������������������������������������������������� 130
Controlling a for Loop with Floating-Point Values �������������������������������������������������������� 132
More Complex for Loop Control Expressions ��������������������������������������������������������������� 135
The Comma Operator�������������������������������������������������������������������������������������������������������������������������� 136
The Range-Based for Loop������������������������������������������������������������������������������������������� 137
The while Loop������������������������������������������������������������������������������������������������������������� 138
The do-while Loop�������������������������������������������������������������������������������������������������������� 140
Nested Loops���������������������������������������������������������������������������������������������������������������� 142
Skipping Loop Iterations����������������������������������������������������������������������������������������������� 145
Breaking Out of a Loop������������������������������������������������������������������������������������������������� 146
Indefinite Loops ���������������������������������������������������������������������������������������������������������������������������������� 146
Controlling a for Loop with Unsigned Integers ������������������������������������������������������������� 150
Arrays of Characters����������������������������������������������������������������������������������������������������� 152
Multidimensional Arrays����������������������������������������������������������������������������������������������� 155
Initializing Multidimensional Arrays���������������������������������������������������������������������������������������������������� 158
Multidimensional Character Arrays ���������������������������������������������������������������������������������������������������� 160
Allocating an Array at Runtime������������������������������������������������������������������������������������� 161
Alternatives to Using an Array�������������������������������������������������������������������������������������� 164
Using array<T,N> Containers ������������������������������������������������������������������������������������������������������������� 164
Using std::vector<T> Containers�������������������������������������������������������������������������������������������������������� 169
Summary���������������������������������������������������������������������������������������������������������������������� 174
■Chapter 6: Pointers and References ������������������������������������������������������������������ 177
What Is a Pointer? �������������������������������������������������������������������������������������������������������� 177
The Address-Of Operator���������������������������������������������������������������������������������������������� 180
The Indirection Operator����������������������������������������������������������������������������������������������� 181
Why Use Pointers? ������������������������������������������������������������������������������������������������������� 183
Pointers to Type char���������������������������������������������������������������������������������������������������� 183
Arrays of Pointers ������������������������������������������������������������������������������������������������������������������������������� 186
Constant Pointers and Pointers to Constants��������������������������������������������������������������� 188
■ Contents
x
Pointers and Arrays ������������������������������������������������������������������������������������������������������ 190
Pointer Arithmetic������������������������������������������������������������������������������������������������������������������������������� 191
Using Pointer Notation with an Array Name ��������������������������������������������������������������������������������������� 193
Dynamic Memory Allocation����������������������������������������������������������������������������������������� 196
The Stack and the Free Store ������������������������������������������������������������������������������������������������������������� 196
Using the new and delete Operators �������������������������������������������������������������������������������������������������� 197
Dynamic Allocation of Arrays�������������������������������������������������������������������������������������������������������������� 199
Member Selection Through a Pointer��������������������������������������������������������������������������� 203
Hazards of Dynamic Memory Allocation����������������������������������������������������������������������� 203
Dangling Pointers and Multiple Deallocations������������������������������������������������������������������������������������ 204
Allocation/Deallocation Mismatch ������������������������������������������������������������������������������������������������������ 204
Memory Leaks ������������������������������������������������������������������������������������������������������������������������������������ 204
Fragmentation of the Free Store��������������������������������������������������������������������������������������������������������� 205
Golden Rule of Dynamic Memory Allocation ���������������������������������������������������������������� 206
Raw Pointers and Smart Pointers �������������������������������������������������������������������������������� 206
Using unique_ptr<T> Pointers ����������������������������������������������������������������������������������������������������������� 208
Using shared_ptr<T> Pointers ����������������������������������������������������������������������������������������������������������� 211
Understanding References������������������������������������������������������������������������������������������� 214
Defining References ��������������������������������������������������������������������������������������������������������������������������� 214
Using a Reference Variable in a Range-Based for Loop ��������������������������������������������������������������������� 216
Summary���������������������������������������������������������������������������������������������������������������������� 217
■Chapter 7: Working with Strings ����������������������������������������������������������������������� 219
A Better Class of String ������������������������������������������������������������������������������������������������ 219
Defining string Objects ����������������������������������������������������������������������������������������������������������������������� 220
Operations with String Objects����������������������������������������������������������������������������������������������������������� 223
Accessing Characters in a String�������������������������������������������������������������������������������������������������������� 228
Accessing Substrings ������������������������������������������������������������������������������������������������������������������������� 230
Comparing Strings������������������������������������������������������������������������������������������������������������������������������ 230
Searching Strings ������������������������������������������������������������������������������������������������������������������������������� 237
Modifying a String ������������������������������������������������������������������������������������������������������������������������������ 243
std::string vs. std::vector<char>�������������������������������������������������������������������������������������������������������� 248
■ Contents
xi
Converting Strings into Numbers ��������������������������������������������������������������������������������� 248
String Streams ������������������������������������������������������������������������������������������������������������� 249
Strings of International Characters ������������������������������������������������������������������������������ 250
Strings of wchar_t Characters������������������������������������������������������������������������������������������������������������ 251
Objects That Contain Unicode Strings ������������������������������������������������������������������������������������������������ 252
Raw String Literals������������������������������������������������������������������������������������������������������� 252
Summary���������������������������������������������������������������������������������������������������������������������� 254
■Chapter 8: Defining Functions ��������������������������������������������������������������������������� 257
Segmenting Your Programs������������������������������������������������������������������������������������������ 257
Functions in Classes��������������������������������������������������������������������������������������������������������������������������� 258
Characteristics of a Function�������������������������������������������������������������������������������������������������������������� 258
Defining Functions ������������������������������������������������������������������������������������������������������� 258
The Function Body������������������������������������������������������������������������������������������������������������������������������ 260
Return Values�������������������������������������������������������������������������������������������������������������������������������������� 262
Function Declarations������������������������������������������������������������������������������������������������������������������������� 263
Passing Arguments to a Function��������������������������������������������������������������������������������� 264
Pass-by-Value������������������������������������������������������������������������������������������������������������������������������������� 265
Pass-by-Reference ����������������������������������������������������������������������������������������������������������������������������� 273
String Views: The New Reference-to-const-string ����������������������������������������������������������������������������� 280
Default Argument Values ���������������������������������������������������������������������������������������������� 283
Multiple Default Parameter Values ����������������������������������������������������������������������������������������������������� 283
Arguments to main()����������������������������������������������������������������������������������������������������� 285
Returning Values from a Function �������������������������������������������������������������������������������� 286
Returning a Pointer����������������������������������������������������������������������������������������������������������������������������� 286
Returning a Reference������������������������������������������������������������������������������������������������������������������������ 290
Returning vs. Output Parameters ������������������������������������������������������������������������������������������������������� 291
Return Type Deduction������������������������������������������������������������������������������������������������������������������������ 292
Working with Optional Values��������������������������������������������������������������������������������������� 293
std::optional���������������������������������������������������������������������������������������������������������������������������������������� 294
Static Variables������������������������������������������������������������������������������������������������������������� 297
■ Contents
xii
Inline Functions������������������������������������������������������������������������������������������������������������ 297
Function Overloading ��������������������������������������������������������������������������������������������������� 298
Overloading and Pointer Parameters�������������������������������������������������������������������������������������������������� 300
Overloading and Reference Parameters��������������������������������������������������������������������������������������������� 301
Overloading and const Parameters ���������������������������������������������������������������������������������������������������� 302
Overloading and Default Argument Values ����������������������������������������������������������������������������������������� 304
Recursion ��������������������������������������������������������������������������������������������������������������������� 305
Basic Examples ���������������������������������������������������������������������������������������������������������������������������������� 306
Recursive Algorithms�������������������������������������������������������������������������������������������������������������������������� 307
Summary���������������������������������������������������������������������������������������������������������������������� 314
■Chapter 9: Function Templates�������������������������������������������������������������������������� 319
Function Templates ������������������������������������������������������������������������������������������������������ 319
Creating Instances of a Function Template ������������������������������������������������������������������ 320
Template Type Parameters ������������������������������������������������������������������������������������������� 322
Explicit Template Arguments ���������������������������������������������������������������������������������������� 323
Function Template Specialization��������������������������������������������������������������������������������� 323
Function Templates and Overloading ��������������������������������������������������������������������������� 324
Function Templates with Multiple Parameters ������������������������������������������������������������� 326
Return Type Deduction for Templates��������������������������������������������������������������������������� 328
decltype() and Trailing Return Types��������������������������������������������������������������������������������������������������� 328
decltype(auto) and decltype() vs. auto������������������������������������������������������������������������������������������������ 330
Default Values for Template Parameters ���������������������������������������������������������������������� 330
Nontype Template Parameters ������������������������������������������������������������������������������������� 331
Templates for Functions with Fixed-Size Array Arguments���������������������������������������������������������������� 332
Summary���������������������������������������������������������������������������������������������������������������������� 334
■Chapter 10: Program Files and Preprocessing Directives��������������������������������� 337
Understanding Translation Units����������������������������������������������������������������������������������� 337
The One Definition Rule ���������������������������������������������������������������������������������������������������������������������� 338
Program Files and Linkage����������������������������������������������������������������������������������������������������������������� 339
■ Contents
xiii
Determining Linkage for a Name�������������������������������������������������������������������������������������������������������� 339
External Functions������������������������������������������������������������������������������������������������������������������������������ 339
External Variables������������������������������������������������������������������������������������������������������������������������������� 341
Internal Names ����������������������������������������������������������������������������������������������������������������������������������� 343
Preprocessing Your Source Code ��������������������������������������������������������������������������������� 344
Defining Preprocessor Macros ������������������������������������������������������������������������������������� 345
Defining Function-Like Macros����������������������������������������������������������������������������������������������������������� 346
Undefining Macros������������������������������������������������������������������������������������������������������������������������������ 349
Including Header Files�������������������������������������������������������������������������������������������������� 350
Preventing Duplication of Header File Contents ��������������������������������������������������������������������������������� 350
Your First Header File ������������������������������������������������������������������������������������������������������������������������� 352
Namespaces����������������������������������������������������������������������������������������������������������������� 353
The Global Namespace����������������������������������������������������������������������������������������������������������������������� 353
Defining a Namespace������������������������������������������������������������������������������������������������������������������������ 354
Applying using Declarations ��������������������������������������������������������������������������������������������������������������� 356
Functions and Namespaces ��������������������������������������������������������������������������������������������������������������� 357
Unnamed Namespaces����������������������������������������������������������������������������������������������������������������������� 359
Nested Namespaces��������������������������������������������������������������������������������������������������������������������������� 360
Namespace Aliases ���������������������������������������������������������������������������������������������������������������������������� 362
Logical Preprocessing Directives ��������������������������������������������������������������������������������� 362
The Logical #if Directive ��������������������������������������������������������������������������������������������������������������������� 362
Testing for Specific Identifier Values �������������������������������������������������������������������������������������������������� 363
Multiple-Choice Code Selection���������������������������������������������������������������������������������������������������������� 363
Standard Preprocessing Macros �������������������������������������������������������������������������������������������������������� 365
Testing for Available Headers ������������������������������������������������������������������������������������������������������������� 366
Debugging Methods ����������������������������������������������������������������������������������������������������� 367
Integrated Debuggers������������������������������������������������������������������������������������������������������������������������� 367
Preprocessing Directives in Debugging���������������������������������������������������������������������������������������������� 368
Using the assert() Macro �������������������������������������������������������������������������������������������������������������������� 371
Static Assertions ���������������������������������������������������������������������������������������������������������� 373
Summary���������������������������������������������������������������������������������������������������������������������� 375
■ Contents
xiv
■Chapter 11: Defining Your Own Data Types ������������������������������������������������������� 379
Classes and Object-Oriented Programming ����������������������������������������������������������������� 379
Encapsulation ������������������������������������������������������������������������������������������������������������������������������������� 380
Inheritance������������������������������������������������������������������������������������������������������������������������������������������ 383
Polymorphism������������������������������������������������������������������������������������������������������������������������������������� 384
Terminology������������������������������������������������������������������������������������������������������������������ 385
Defining a Class ����������������������������������������������������������������������������������������������������������� 386
Constructors����������������������������������������������������������������������������������������������������������������� 388
Default Constructors��������������������������������������������������������������������������������������������������������������������������� 388
Defining a Class Constructor�������������������������������������������������������������������������������������������������������������� 389
Using the default Keyword ����������������������������������������������������������������������������������������������������������������� 391
Defining Functions and Constructors Outside the Class��������������������������������������������������������������������� 391
Default Constructor Parameter Values������������������������������������������������������������������������������������������������ 393
Using a Member Initializer List����������������������������������������������������������������������������������������������������������� 394
Using the explicit Keyword ����������������������������������������������������������������������������������������������������������������� 394
Delegating Constructors ��������������������������������������������������������������������������������������������������������������������� 397
The Copy Constructor ������������������������������������������������������������������������������������������������������������������������� 398
Accessing Private Class Members ������������������������������������������������������������������������������� 400
The this Pointer������������������������������������������������������������������������������������������������������������ 402
Returning this from a Function����������������������������������������������������������������������������������������������������������� 402
const Objects and const Member Functions ���������������������������������������������������������������� 404
const Member Functions�������������������������������������������������������������������������������������������������������������������� 404
const Correctness������������������������������������������������������������������������������������������������������������������������������� 406
Overloading on const�������������������������������������������������������������������������������������������������������������������������� 407
Casting Away const ���������������������������������������������������������������������������������������������������������������������������� 409
Using the mutable Keyword���������������������������������������������������������������������������������������������������������������� 409
Friends ������������������������������������������������������������������������������������������������������������������������� 410
The Friend Functions of a Class ��������������������������������������������������������������������������������������������������������� 411
Friend Classes ������������������������������������������������������������������������������������������������������������������������������������ 413
Arrays of Class Objects ������������������������������������������������������������������������������������������������ 413
■ Contents
xv
The Size of a Class Object�������������������������������������������������������������������������������������������� 415
Static Members of a Class ������������������������������������������������������������������������������������������� 416
Static Member Variables��������������������������������������������������������������������������������������������������������������������� 416
Accessing Static Member Variables ��������������������������������������������������������������������������������������������������� 420
Static Constants ��������������������������������������������������������������������������������������������������������������������������������� 421
Static Member Variables of the Class Type����������������������������������������������������������������������������������������� 422
Static Member Functions�������������������������������������������������������������������������������������������������������������������� 423
Destructors������������������������������������������������������������������������������������������������������������������� 424
Using Pointers as Class Members�������������������������������������������������������������������������������� 427
The Truckload Example����������������������������������������������������������������������������������������������������������������������� 427
Nested Classes������������������������������������������������������������������������������������������������������������� 440
Nested Classes with Public Access ���������������������������������������������������������������������������������������������������� 441
Summary���������������������������������������������������������������������������������������������������������������������� 445
■Chapter 12: Operator Overloading��������������������������������������������������������������������� 449
Implementing Operators for a Class����������������������������������������������������������������������������� 449
Operator Overloading�������������������������������������������������������������������������������������������������������������������������� 450
Implementing an Overloaded Operator����������������������������������������������������������������������������������������������� 450
Nonmember Operator Functions �������������������������������������������������������������������������������������������������������� 453
Implementing Full Support for an Operator���������������������������������������������������������������������������������������� 453
Implementing All Comparison Operators in a Class���������������������������������������������������������������������������� 456
Operators That Can Be Overloaded ������������������������������������������������������������������������������ 458
Restrictions and Key Guideline����������������������������������������������������������������������������������������������������������� 459
Operator Function Idioms��������������������������������������������������������������������������������������������� 461
Overloading the << Operator for Output Streams�������������������������������������������������������� 462
Overloading the Arithmetic Operators �������������������������������������������������������������������������� 463
Implementing One Operator in Terms of Another ������������������������������������������������������������������������������� 467
Member vs. Nonmember Functions ����������������������������������������������������������������������������� 469
Operator Functions and Implicit Conversions������������������������������������������������������������������������������������� 470
Overloading Unary Operators ��������������������������������������������������������������������������������������� 471
Overloading the Increment and Decrement Operators������������������������������������������������� 473