Horton | Beginning C++ | E-Book | sack.de
E-Book

E-Book, Englisch, 620 Seiten

Horton Beginning C++


4th Auflage
ISBN: 978-1-4842-0007-0
Verlag: APRESS
Format: PDF
Kopierschutz: 1 - PDF Watermark

E-Book, Englisch, 620 Seiten

ISBN: 978-1-4842-0007-0
Verlag: APRESS
Format: PDF
Kopierschutz: 1 - PDF Watermark



is a tutorial for beginners in C++ and discusses a subset of C++ that is suitable for beginners. The language syntax corresponds to the C++14 standard. This book is environment neutral and does not presume any specific operating system or program development system. There is no assumption of prior programming knowledge.

All language concepts that are explained in the book are illustrated with working program examples. Most chapters include exercises for you to test your knowledge. Code downloads are provided for examples from the text and solutions to the exercises and there is an additional download for a more substantial project for you to try when you have finished the book.

This book introduces the elements of the C++ standard library that provide essential support for the language syntax that is discussed. While the Standard Template Library (STL) is not discussed to a significant extent, a few elements from the STL that are important to the notion of modern C++ are introduced and applied.

is based on and supersedes Ivor Horton’s previous book,

Horton Beginning C++ jetzt bestellen!

Zielgruppe


Popular/general


Autoren/Hrsg.


Weitere Infos & Material


1;Contents at a Glance;3
2;Contents;603
3;About the Author;619
4;About the Technical Reviewer;620
5;Introduction;5
6;Chapter 1: Basic Ideas;7
6.1;Modern C++;7
6.2;C++ Program Concepts;8
6.2.1;Comments and Whitespace;8
6.2.2;Preprocessing Directives and Header Files;9
6.2.3;Functions;9
6.2.4;Statements;10
6.2.5;Data Input and Output;11
6.2.6;return Statements;11
6.2.7;Namespaces;11
6.2.8;Names and Keywords;12
6.3;Classes and Objects;13
6.4;Templates;13
6.5;Program Files;13
6.6;Standard Libraries;13
6.7;Code Presentation Style;13
6.8;Creating an Executable;14
6.9;Representing Numbers;15
6.9.1;Binary Numbers;15
6.9.2;Hexadecimal Numbers;17
6.9.3;Negative Binary Numbers;18
6.9.4;Octal Values;20
6.9.5;Big-Endian and Little-Endian Systems;20
6.9.6;Floating-Point Numbers;21
6.10;Representing Characters;22
6.10.1;ASCII Codes;22
6.10.2;UCS and Unicode;23
6.11;C++ Source Characters;23
6.11.1;Trigraph Sequences;24
6.11.2;Escape Sequences;24
6.12;Procedural and Object-Oriented Programming;26
6.13;Summary;27
7;Chapter 2: Introducing Fundamental Types of Data;29
7.1;Variables, Data, and Data Types;29
7.1.1;Defining Integer Variables;30
7.1.2;Defining Variables with Fixed Values;32
7.2;Integer Literals;32
7.2.1;Decimal Integer Literals;32
7.2.1.1;Hexadecimal Literals;33
7.2.1.2;Octal Literals;33
7.2.1.3;Binary Literals;34
7.3;Calculations with Integers;34
7.3.1;More on Assignment Operations;36
7.4;The op= Assignment Operators;38
7.5;using Declarations and Directives;40
7.6;The sizeof Operator;40
7.7;Incrementing and Decrementing Integers;41
7.7.1;Postfix Increment and Decrement Operations;41
7.8;Defining Floating-Point Variables;42
7.9;Floating-Point Literals;43
7.10;Floating-Point Calculations;44
7.10.1;Mathematical Functions;46
7.11;Formatting Stream Output;49
7.12;Mixed Expressions and Type Conversion;51
7.13;Explicit Type Conversion;53
7.13.1;Old-Style Casts;55
7.14;Finding the Limits;55
7.15;Working with Character Variables;56
7.15.1;Working with Unicode Characters;57
7.16;The auto Keyword;58
7.17;Lvalues and Rvalues;58
7.18;Summary;59
8;Chapter 3: Working with Fundamental Data Types;61
8.1;Operator Precedence and Associativity;61
8.2;Bitwise Operators;63
8.2.1;The Bitwise Shift Operators;64
8.2.1.1;Shifting Signed Integers;66
8.2.1.2;Logical Operations on Bit Patterns;66
8.2.2;Using the Bitwise AND;67
8.2.3;Using the Bitwise OR;68
8.2.4;Using the Bitwise Exclusive OR;69
8.3;Enumerated Data Types;73
8.3.1;Old-Style Enumerations;76
8.4;Synonyms for Data Types;76
8.5;The Lifetime of a Variable;77
8.6;Positioning Variable Definitions;77
8.6.1;Global Variables;77
8.6.2;Static Variables;81
8.7;External Variables;81
8.8;Summary;82
9;Chapter 4: Making Decisions;84
9.1;Comparing Data Values;84
9.1.1;Applying the Comparison Operators;85
9.1.2;Comparing Floating Point Values;87
9.2;The if Statement;87
9.2.1;Nested if Statements;90
9.2.2;Code-Neutral Character Handling;91
9.3;The if-else Statement;93
9.3.1;Nested if-else Statements;94
9.3.2;Understanding Nested ifs;95
9.4;Logical Operators;97
9.4.1;Logical AND;97
9.4.2;Logical OR;98
9.4.3;Logical Negation;98
9.5;The Conditional Operator;100
9.6;The switch Statement;102
9.7;Unconditional Branching;105
9.8;Statement Blocks and Variable Scope;106
9.9;Summary;107
10;Chapter 5: Arrays and Loops;109
10.1;Arrays;109
10.1.1;Using an Array;110
10.2;Understanding Loops;111
10.3;The for Loop;112
10.4;Avoiding Magic Numbers;114
10.5;Defining the Array Size with the Initializer List;115
10.6;Determining the Size of an Array;115
10.7;Controlling a for Loop with Floating-Point Values;117
10.8;More Complex for Loop Control Expressions;118
10.9;The Comma Operator;120
10.10;The Ranged-based for Loop;120
10.11;The while Loop;121
10.12;Allocating an Array at Runtime;123
10.13;The do-while Loop;125
10.14;Nested Loops;127
10.15;Skipping Loop Iterations;129
10.16;Breaking Out of a Loop;131
10.16.1;Indefinite Loops;131
10.17;Arrays of Characters;135
10.18;Multidimensional Arrays;138
10.18.1;Initializing Multidimensional Arrays;140
10.18.1.1;Setting Dimensions by Default;141
10.18.2;Multidimensional Character Arrays;142
10.19;Alternatives to Using an Array;144
10.19.1;Using array Containers;144
10.19.2;Using std::vector Containers;148
10.19.3;The Capacity and Size of a Vector;149
10.19.4;Deleting Elements from a Vector container;151
10.20;Summary;152
11;Chapter 6: Pointers and References;154
11.1;What Is a Pointer ?;154
11.2;The Address-Of Operator;156
11.3;The Indirection Operator;157
11.3.1;Why Use Pointers ?;159
11.4;Pointers to Type char;159
11.4.1;Arrays of Pointers;161
11.5;Constant Pointers and Pointers to Constants;163
11.6;Pointers and Arrays;165
11.6.1;Pointer Arithmetic;165
11.6.1.1;The Difference between Pointers;167
11.6.2;Using Pointer Notation with an Array Name;167
11.7;Dynamic Memory Allocation;170
11.7.1;The Stack and the Heap;170
11.7.2;Using the new and delete Operators;171
11.7.3;Dynamic Allocation of Arrays;172
11.7.4;Member Selection through a Pointer;174
11.8;Hazards of Dynamic Memory Allocation;174
11.8.1;Memory Leaks;175
11.8.2;Fragmentation of the Free Store;175
11.9;Raw Pointers and Smart Pointers;176
11.9.1;Using unique_ptr Pointers;177
11.9.2;Using shared_ptr Pointers;178
11.9.3;Comparing shared_ptr Objects;181
11.9.4;weak_ptr Pointers;182
11.10;Understanding References;183
11.10.1;Defining lvalue References;184
11.10.2;Using a Reference Variable in a Range-Based for Loop;184
11.10.3;Defining rvalue References;185
11.11;Summary;186
12;Chapter 7: Working with Strings;188
12.1;A Better Class of String;188
12.1.1;Defining string Objects;189
12.1.2;Operations with String Objects;191
12.1.2.1;Concatenating Strings;191
12.1.3;Accessing Characters in a String;193
12.1.4;Accessing Substrings;195
12.1.5;Comparing Strings;196
12.1.5.1;The compare(?) Function;198
12.1.5.2;Comparisons Using substr(?);200
12.1.6;Searching Strings;201
12.1.6.1;Searching for any of a Set of Characters;203
12.1.7;Searching a String Backwards;206
12.1.8;Modifying a String;207
12.1.8.1;Inserting a String;207
12.1.8.2;Replacing a Substring;208
12.1.8.3;Removing Characters from a String;210
12.2;Strings of International Characters;211
12.2.1;Strings of wchar_t Characters;211
12.3;Objects that contain Unicode Strings;212
12.4;Raw String Literals;212
12.5;Summary;213
13;Chapter 8: Defining Functions;215
13.1;Segmenting Your Programs;215
13.1.1;Functions in Classes;216
13.1.2;Characteristics of a Function;216
13.2;Defining Functions;216
13.2.1;The Function Body;218
13.2.1.1;Return Values;219
13.2.1.1.1;How the return Statement Works;219
13.2.2;Function Declarations;219
13.2.2.1;Function Prototypes;220
13.3;Passing Arguments to a Function;221
13.3.1;Pass-by-Value;221
13.3.1.1;Passing a Pointer to a Function;223
13.3.1.2;Passing an Array to a Function;224
13.3.1.2.1;const Pointer Parameters;225
13.3.1.2.2;Passing a Multidimensional Array to a Function;226
13.3.2;Pass-by-Reference;227
13.3.2.1;References Can Be Risky;228
13.3.2.1.1;Improving the Program;230
13.3.2.1.2;Simplifying Code using Type Aliases;231
13.3.2.2;References versus Pointers;231
13.3.3;Arguments to main();232
13.4;Default Argument Values;233
13.4.1;Multiple Default Parameter Values;233
13.5;Returning Values from a Function;235
13.5.1;Returning a Pointer;235
13.5.2;Returning a Reference;240
13.6;Inline Functions;241
13.7;Static Variables;241
13.8;Function Overloading;243
13.8.1;Overloading and Pointer Parameters;245
13.8.2;Overloading and Reference Parameters;245
13.8.3;Overloading and const Parameters;247
13.8.3.1;Overloading with const Pointer Parameters;247
13.8.3.2;Overloading and const Reference Parameters;248
13.8.4;Overloading and Default Argument Values;248
13.9;A Sausage Machine for Functions;249
13.9.1;Creating Instances of a Function Template;250
13.9.2;Explicit Template Argument;251
13.9.3;Function Template Specialization;252
13.9.4;Function Templates and Overloading;253
13.9.5;Function Templates with Multiple Parameters;254
13.9.6;Non-Type Template Parameters;255
13.10;Trailing Return Types;256
13.11;Pointers to Functions;257
13.11.1;Defining Pointers to Functions;258
13.12;Recursion;260
13.12.1;Applying Recursion;263
13.12.2;The Quicksort Algorithm;263
13.12.3;The main() Function;264
13.12.4;The extract_words() Function;265
13.12.5;The swap() Function;266
13.12.6;The sort() function;266
13.12.7;The max_word_length() Function;267
13.12.8;The show_words() Function;268
13.13;Summary;269
14;Chapter 9: Lambda Expressions;273
14.1;Introducing Lambda Expressions;273
14.2;Defining a Lambda Expression;274
14.3;Naming a Lambda Expression;274
14.4;Passing a Lambda Expression to a Function;276
14.4.1;Function Templates that Accept Lambda Expression Arguments;276
14.4.2;A Function Parameter Type for Lambda Arguments;277
14.4.3;Using the std::function Template Type;278
14.5;The Capture Clause;281
14.5.1;Capturing Specific Variables;282
14.6;Using Lambda Expressions in a Template;283
14.7;Recursion in Lambda Expressions;285
14.8;Summary;286
15;Chapter 10: Program Files and Preprocessing Directives;288
15.1;Understanding Translation Units;288
15.1.1;The “One Definition” Rule;289
15.1.2;Program Files and Linkage;289
15.1.3;Determining Linkage for a Name;289
15.1.4;External Names;290
15.1.5;const Variables with External Linkage;290
15.2;Preprocessing Your Source Code;290
15.3;Defining Preprocessing Identifiers;292
15.3.1;Undefining an Identifier;293
15.4;Including Header Files;293
15.4.1;Preventing Duplication of Header File Contents;294
15.5;Namespaces;295
15.5.1;The Global Namespace;296
15.5.2;Defining a Namespace;296
15.5.3;Applying using Declarations;299
15.5.4;Functions and Namespaces;299
15.5.5;Unnamed Namespaces;302
15.5.6;Namespace Aliases;302
15.5.7;Nested Namespaces;303
15.6;Logical Preprocessing Directives;304
15.6.1;The Logical #if Directive;304
15.6.2;Testing for Specific Identifier Values;304
15.6.3;Multiple Choice Code Selection;305
15.6.4;Standard Preprocessing Macros;306
15.7;Debugging Methods;307
15.7.1;Integrated Debuggers;307
15.7.2;Preprocessing Directives in Debugging;308
15.7.3;Using the assert(?) Macro;312
15.7.4;Switching Off assert() Macros;312
15.8;Static Assertions;313
15.9;Summary;314
16;Chapter 11: Defining Your Own Data Types;316
16.1;Classes and Object-Oriented Programming;316
16.1.1;Encapsulation;317
16.1.1.1;Data Hiding;318
16.1.2;Inheritance;318
16.1.3;Polymorphism;319
16.2;Terminology;320
16.3;Defining a Class;321
16.4;Constructors;323
16.4.1;Defining Constructors Outside the Class;325
16.4.2;Default Constructor Parameter Values;327
16.4.3;Using a Constructor Initialization List;327
16.4.4;Use of the explicit Keyword;328
16.4.5;Delegating Constructors;330
16.4.6;The Copy Constructor;331
16.4.6.1;Implementing the Copy Constructor;332
16.4.6.2;Reference Parameters;332
16.5;Accessing Private Class Members;332
16.6;Friends;334
16.6.1;The Friend Functions of a Class;334
16.6.2;Friend Classes;337
16.7;The this Pointer;337
16.7.1;Returning this from a Function;338
16.8;const Objects and const Member Functions;339
16.8.1;Casting Away const;340
16.9;Arrays of Class Objects;341
16.10;The Size of a Class Object;342
16.11;Static Members of a Class;343
16.11.1;Static Data Members;343
16.11.2;Accessing Static Data Members;346
16.11.3;A Static Data Member of the Class Type;347
16.11.4;Static Function Members;347
16.12;Destructors;348
16.13;Pointers and References to Class Objects;351
16.14;Using Pointers As Class Members;351
16.14.1;Defining the Package Class;354
16.14.2;Defining the Truckload Class;355
16.14.3;Implementing the Truckload Class;356
16.15;Nested Classes;360
16.16;Summary;363
17;Chapter 12: Operator Overloading;366
17.1;Implementing Operators for a Class;366
17.1.1;Operator Overloading;367
17.1.2;Operators That Can Be Overloaded;367
17.1.3;Implementing an Overloaded Operator;368
17.1.4;Global Operator Functions;370
17.1.5;Implementing Full Support for an Operator;371
17.1.6;Implementing All Comparison Operators in a Class;372
17.2;Operator Function Idioms;374
17.3;Default Class Members;375
17.3.1;Defining the Destructor;376
17.3.2;When to Define a Copy Constructor;378
17.3.3;Implementing the Assignment Operator;378
17.3.4;Implementing Move Operations;380
17.4;Overloading the Arithmetic Operators;381
17.4.1;Improving Output Operations;385
17.4.2;Implementing One Operator in Terms of Another;387
17.5;Overloading the Subscript Operator;388
17.5.1;Lvalues and the Overloaded Subscript Operator;393
17.5.1.1;Potential Ambiguities with Conversions;396
17.6;Overloading Type Conversions;395
17.7;Overloading the Increment and Decrement Operators;396
17.8;Function Objects;397
17.9;Summary;398
18;Chapter 13: Inheritance;400
18.1;Classes and Object-Oriented Programming;400
18.1.1;Hierarchies;401
18.2;Inheritance in Classes;402
18.2.1;Inheritance vs. Aggregation;402
18.2.2;Deriving Classes;403
18.3;protected Members of a Class;406
18.4;The Access Level of Inherited Class Members;406
18.4.1;Choosing Access Specifiers in Class Hierarchies;408
18.4.2;Changing the Access Specification of Inherited Members;409
18.5;Constructor Operation in a Derived Class;410
18.5.1;The Copy Constructor in a Derived Class;413
18.5.2;The Default Constructor in a Derived Class;415
18.5.3;Inheriting Constructors;416
18.6;Destructors Under Inheritance;417
18.6.1;The Order in Which Destructors Are Called;418
18.7;Duplicate Data Member Names;419
18.8;Duplicate Function Member Names;420
18.9;Multiple Inheritance;420
18.9.1;Multiple Base Classes;421
18.9.2;Inherited Member Ambiguity;422
18.9.3;Repeated Inheritance;425
18.9.4;Virtual Base Classes;426
18.10;Converting Between Related Class Types;427
18.11;Summary;427
19;Chapter 14: Polymorphism;429
19.1;Understanding Polymorphism;429
19.1.1;Using a Base Class Pointer;429
19.1.2;Calling Inherited Functions;431
19.1.3;Virtual Functions;434
19.1.3.1;Requirements for Virtual Function Operation;438
19.1.3.2;Virtual Functions and Class Hierarchies;438
19.1.3.3;Using override;440
19.1.3.4;Using final;440
19.1.3.5;Access Specifiers and Virtual Functions;441
19.1.4;Default Argument Values in Virtual Functions;442
19.1.5;Virtual Function Calls with Smart Pointers;444
19.1.6;Using References to Call Virtual Functions;444
19.1.7;Calling the Base Class Version of a Virtual Function;445
19.1.8;Converting Between Pointers to Class Objects;446
19.1.9;Dynamic Casts;448
19.1.9.1;Casting Pointers Dynamically;448
19.1.10;Converting References;450
19.1.11;Determining the Polymorphic Type;450
19.2;The Cost of Polymorphism;451
19.3;Pure Virtual Functions;452
19.3.1;Abstract Classes;453
19.3.1.1;Abstract Classes As Interfaces;455
19.3.2;Indirect Abstract Base Classes;456
19.4;Destroying Objects Through a Pointer;458
19.4.1;Virtual Destructors;460
19.5;Summary;460
20;Chapter 15: Runtime Errors and Exceptions;462
20.1;Handling Errors;462
20.2;Understanding Exceptions;463
20.2.1;Throwing an Exception;464
20.2.2;The Exception Handling Process;466
20.2.2.1;Unhandled Exceptions;467
20.2.3;Code That Causes an Exception to Be Thrown;468
20.2.4;Nested try Blocks;470
20.2.5;How It Works;472
20.3;Class Objects as Exceptions;473
20.3.1;Matching a Catch Handler to an Exception;474
20.3.2;How It Works;476
20.3.3;Catching Derived Class Exceptions with a Base Class Handler;477
20.3.4;Rethrowing Exceptions;479
20.3.5;Catching All Exceptions;482
20.4;Functions That Throw Exceptions;484
20.4.1;Function try Blocks;484
20.4.2;Functions That Don’t Throw Exceptions;485
20.4.3;Constructor try Blocks;485
20.4.4;Exceptions and Destructors;486
20.5;Standard Library Exceptions;486
20.5.1;The Exception Class Definitions;488
20.5.2;Using Standard Exceptions;489
20.5.2.1;Deriving your own Exception Classes;489
20.6;Summary;492
21;Chapter 16: Class Templates;494
21.1;Understanding Class Templates;494
21.2;Defining Class Templates;495
21.2.1;Template Parameters;496
21.2.2;A Simple Class Template;497
21.2.3;Defining Function Members of a Class Template;499
21.2.3.1;Constructor Templates;499
21.2.3.2;The Destructor Template;500
21.2.3.3;Subscript Operator Templates;501
21.2.3.4;The Assignment Operator Template;501
21.3;Instantiating a Class Template;502
21.4;Static Members of a Class Template;507
21.5;Non-Type Class Template Parameters;508
21.5.1;Templates for Function Members with Non-Type Parameters;511
21.5.2;Arguments for Non-Type Parameters;515
21.5.3;Pointers and Arrays as Non-Type Parameters;515
21.6;Default Values for Template Parameters;516
21.7;Explicit Template Instantiation;517
21.8;Special Cases;517
21.8.1;Using static_assert(?) in a Class Template;518
21.8.2;Defining a Class Template Specialization;519
21.8.3;Partial Template Specialization;520
21.8.4;Choosing between Multiple Partial Specializations;520
21.9;Friends of Class Templates;521
21.10;Class Templates with Nested Classes;523
21.10.1;Function Templates for Stack Members;525
21.11;Summary;529
22;Chapter 17: File Input and Output;531
22.1;Input and Output in C++;531
22.1.1;Understanding Streams;532
22.1.1.1;Data Transfer Modes;532
22.1.1.2;Text Mode Operations;532
22.1.1.3;Binary Mode Operations;532
22.1.2;Advantages of Using Streams;533
22.2;Stream Classes;534
22.2.1;Standard Stream Objects;535
22.2.2;Stream Insertion and Extraction Operations;535
22.2.2.1;Stream Extraction Operations;536
22.2.2.2;Stream Insertion Operations;536
22.2.3;Stream Manipulators;537
22.2.3.1;Manipulators with Arguments;539
22.3;File Streams;540
22.3.1;Writing a File in Text Mode;541
22.3.2;Reading a File in Text Mode;543
22.3.2.1;Checking the State of a File Stream;543
22.3.2.2;Reading the File;544
22.4;Setting the Stream Open Mode;546
22.4.1;Managing the Current Stream Position;551
22.5;Unformatted Stream Operations;553
22.5.1;Unformatted Stream Input;554
22.5.2;Unformatted Stream Output;556
22.6;Errors in Stream Input/Output;556
22.6.1;Input/Output Errors and Exceptions;558
22.7;Stream Operations in Binary Mode;559
22.7.1;Writing Numeric Data in Binary;561
22.8;File Read/Write Operations;569
22.8.1;Random Access to a File;570
22.8.1.1;Random Access to a Binary Stream;570
22.8.1.2;Random File Operations in Practice;570
22.8.1.2.1;Implementing the Constructor;571
22.8.1.2.2;Checking for the Existence of a File;572
22.8.1.2.3;Finding a Prime that follows a Prime;573
22.8.1.2.4;Checking for a Prime;574
22.8.1.2.5;Implementing the Subscript Operator;574
22.8.1.2.6;Using the Primes Class;575
22.9;String Streams;576
22.10;Objects and Streams;577
22.10.1;Using the Insertion Operator with Objects;577
22.10.2;Using the Extraction Operator with Objects;578
22.10.3;Object I/O in Binary Mode;580
22.10.4;More Complex Objects in Streams;583
22.11;Summary;588
23;Index;590


Ivor Horton is self-employed in consultancy and writes programming tutorials. He worked for IBM for many years and holds a bachelor's degree, with honors, in mathematics. Horton's experience at IBM includes programming in most languages (like assembler and high-level languages on a variety of machines), real-time programming, and designing and implementing real-time closed loop industrial control systems. He has extensive experience teaching programming to engineers and scientists (Fortran, PL/1, APL, etc.). Horton is an expert in mechanical, process, and electronic CAD systems; mechanical CAM systems; and DNC/CNC systems.



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.