Beginning C++ (eBook)

(Autor)

eBook Download: PDF
2014 | 4th ed.
XXV, 594 Seiten
Apress (Verlag)
978-1-4842-0007-0 (ISBN)

Lese- und Medienproben

Beginning C++ - Ivor Horton
Systemvoraussetzungen
62,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen

Beginning C++ 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.

Beginning C++ is based on and supersedes Ivor Horton’s previous book, Beginning ANSI C++.



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.
Beginning C++ 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.Beginning C++ is based on and supersedes Ivor Horton’s previous book, Beginning ANSI C++.

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.

Contents at a Glance 3
Contents 603
About the Author 619
About the Technical Reviewer 620
Introduction 5
Chapter 1: Basic Ideas 7
Modern C++ 7
C++ Program Concepts 8
Comments and Whitespace 8
Preprocessing Directives and Header Files 9
Functions 9
Statements 10
Data Input and Output 11
return Statements 11
Namespaces 11
Names and Keywords 12
Classes and Objects 13
Templates 13
Program Files 13
Standard Libraries 13
Code Presentation Style 13
Creating an Executable 14
Representing Numbers 15
Binary Numbers 15
Hexadecimal Numbers 17
Negative Binary Numbers 18
Octal Values 20
Big-Endian and Little-Endian Systems 20
Floating-Point Numbers 21
Representing Characters 22
ASCII Codes 22
UCS and Unicode 23
C++ Source Characters 23
Trigraph Sequences 24
Escape Sequences 24
Procedural and Object-Oriented Programming 26
Summary 27
Chapter 2: Introducing Fundamental Types of Data 29
Variables, Data, and Data Types 29
Defining Integer Variables 30
Defining Variables with Fixed Values 32
Integer Literals 32
Decimal Integer Literals 32
Hexadecimal Literals 33
Octal Literals 33
Binary Literals 34
Calculations with Integers 34
More on Assignment Operations 36
The op= Assignment Operators 38
using Declarations and Directives 40
The sizeof Operator 40
Incrementing and Decrementing Integers 41
Postfix Increment and Decrement Operations 41
Defining Floating-Point Variables 42
Floating-Point Literals 43
Floating-Point Calculations 44
Mathematical Functions 46
Formatting Stream Output 49
Mixed Expressions and Type Conversion 51
Explicit Type Conversion 53
Old-Style Casts 55
Finding the Limits 55
Working with Character Variables 56
Working with Unicode Characters 57
The auto Keyword 58
Lvalues and Rvalues 58
Summary 59
Chapter 3: Working with Fundamental Data Types 61
Operator Precedence and Associativity 61
Bitwise Operators 63
The Bitwise Shift Operators 64
Shifting Signed Integers 66
Logical Operations on Bit Patterns 66
Using the Bitwise AND 67
Using the Bitwise OR 68
Using the Bitwise Exclusive OR 69
Enumerated Data Types 73
Old-Style Enumerations 76
Synonyms for Data Types 76
The Lifetime of a Variable 77
Positioning Variable Definitions 77
Global Variables 77
Static Variables 81
External Variables 81
Summary 82
Chapter 4: Making Decisions 84
Comparing Data Values 84
Applying the Comparison Operators 85
Comparing Floating Point Values 87
The if Statement 87
Nested if Statements 90
Code-Neutral Character Handling 91
The if-else Statement 93
Nested if-else Statements 94
Understanding Nested ifs 95
Logical Operators 97
Logical AND 97
Logical OR 98
Logical Negation 98
The Conditional Operator 100
The switch Statement 102
Unconditional Branching 105
Statement Blocks and Variable Scope 106
Summary 107
Chapter 5: Arrays and Loops 109
Arrays 109
Using an Array 110
Understanding Loops 111
The for Loop 112
Avoiding Magic Numbers 114
Defining the Array Size with the Initializer List 115
Determining the Size of an Array 115
Controlling a for Loop with Floating-Point Values 117
More Complex for Loop Control Expressions 118
The Comma Operator 120
The Ranged-based for Loop 120
The while Loop 121
Allocating an Array at Runtime 123
The do-while Loop 125
Nested Loops 127
Skipping Loop Iterations 129
Breaking Out of a Loop 131
Indefinite Loops 131
Arrays of Characters 135
Multidimensional Arrays 138
Initializing Multidimensional Arrays 140
Setting Dimensions by Default 141
Multidimensional Character Arrays 142
Alternatives to Using an Array 144
Using array< T,N>
Using std::vector< T>
The Capacity and Size of a Vector 149
Deleting Elements from a Vector container 151
Summary 152
Chapter 6: Pointers and References 154
What Is a Pointer ? 154
The Address-Of Operator 156
The Indirection Operator 157
Why Use Pointers ? 159
Pointers to Type char 159
Arrays of Pointers 161
Constant Pointers and Pointers to Constants 163
Pointers and Arrays 165
Pointer Arithmetic 165
The Difference between Pointers 167
Using Pointer Notation with an Array Name 167
Dynamic Memory Allocation 170
The Stack and the Heap 170
Using the new and delete Operators 171
Dynamic Allocation of Arrays 172
Member Selection through a Pointer 174
Hazards of Dynamic Memory Allocation 174
Memory Leaks 175
Fragmentation of the Free Store 175
Raw Pointers and Smart Pointers 176
Using unique_ptr< T>
Using shared_ptr< T>
Comparing shared_ptr< T>
weak_ptr< T>
Understanding References 183
Defining lvalue References 184
Using a Reference Variable in a Range-Based for Loop 184
Defining rvalue References 185
Summary 186
Chapter 7: Working with Strings 188
A Better Class of String 188
Defining string Objects 189
Operations with String Objects 191
Concatenating Strings 191
Accessing Characters in a String 193
Accessing Substrings 195
Comparing Strings 196
The compare(?) Function 198
Comparisons Using substr(?) 200
Searching Strings 201
Searching for any of a Set of Characters 203
Searching a String Backwards 206
Modifying a String 207
Inserting a String 207
Replacing a Substring 208
Removing Characters from a String 210
Strings of International Characters 211
Strings of wchar_t Characters 211
Objects that contain Unicode Strings 212
Raw String Literals 212
Summary 213
Chapter 8: Defining Functions 215
Segmenting Your Programs 215
Functions in Classes 216
Characteristics of a Function 216
Defining Functions 216
The Function Body 218
Return Values 219
How the return Statement Works 219
Function Declarations 219
Function Prototypes 220
Passing Arguments to a Function 221
Pass-by-Value 221
Passing a Pointer to a Function 223
Passing an Array to a Function 224
const Pointer Parameters 225
Passing a Multidimensional Array to a Function 226
Pass-by-Reference 227
References Can Be Risky 228
Improving the Program 230
Simplifying Code using Type Aliases 231
References versus Pointers 231
Arguments to main() 232
Default Argument Values 233
Multiple Default Parameter Values 233
Returning Values from a Function 235
Returning a Pointer 235
Returning a Reference 240
Inline Functions 241
Static Variables 241
Function Overloading 243
Overloading and Pointer Parameters 245
Overloading and Reference Parameters 245
Overloading and const Parameters 247
Overloading with const Pointer Parameters 247
Overloading and const Reference Parameters 248
Overloading and Default Argument Values 248
A Sausage Machine for Functions 249
Creating Instances of a Function Template 250
Explicit Template Argument 251
Function Template Specialization 252
Function Templates and Overloading 253
Function Templates with Multiple Parameters 254
Non-Type Template Parameters 255
Trailing Return Types 256
Pointers to Functions 257
Defining Pointers to Functions 258
Recursion 260
Applying Recursion 263
The Quicksort Algorithm 263
The main() Function 264
The extract_words() Function 265
The swap() Function 266
The sort() function 266
The max_word_length() Function 267
The show_words() Function 268
Summary 269
Chapter 9: Lambda Expressions 273
Introducing Lambda Expressions 273
Defining a Lambda Expression 274
Naming a Lambda Expression 274
Passing a Lambda Expression to a Function 276
Function Templates that Accept Lambda Expression Arguments 276
A Function Parameter Type for Lambda Arguments 277
Using the std::function Template Type 278
The Capture Clause 281
Capturing Specific Variables 282
Using Lambda Expressions in a Template 283
Recursion in Lambda Expressions 285
Summary 286
Chapter 10: Program Files and Preprocessing Directives 288
Understanding Translation Units 288
The “One Definition” Rule 289
Program Files and Linkage 289
Determining Linkage for a Name 289
External Names 290
const Variables with External Linkage 290
Preprocessing Your Source Code 290
Defining Preprocessing Identifiers 292
Undefining an Identifier 293
Including Header Files 293
Preventing Duplication of Header File Contents 294
Namespaces 295
The Global Namespace 296
Defining a Namespace 296
Applying using Declarations 299
Functions and Namespaces 299
Unnamed Namespaces 302
Namespace Aliases 302
Nested Namespaces 303
Logical Preprocessing Directives 304
The Logical #if Directive 304
Testing for Specific Identifier Values 304
Multiple Choice Code Selection 305
Standard Preprocessing Macros 306
Debugging Methods 307
Integrated Debuggers 307
Preprocessing Directives in Debugging 308
Using the assert(?) Macro 312
Switching Off assert() Macros 312
Static Assertions 313
Summary 314
Chapter 11: Defining Your Own Data Types 316
Classes and Object-Oriented Programming 316
Encapsulation 317
Data Hiding 318
Inheritance 318
Polymorphism 319
Terminology 320
Defining a Class 321
Constructors 323
Defining Constructors Outside the Class 325
Default Constructor Parameter Values 327
Using a Constructor Initialization List 327
Use of the explicit Keyword 328
Delegating Constructors 330
The Copy Constructor 331
Implementing the Copy Constructor 332
Reference Parameters 332
Accessing Private Class Members 332
Friends 334
The Friend Functions of a Class 334
Friend Classes 337
The this Pointer 337
Returning this from a Function 338
const Objects and const Member Functions 339
Casting Away const 340
Arrays of Class Objects 341
The Size of a Class Object 342
Static Members of a Class 343
Static Data Members 343
Accessing Static Data Members 346
A Static Data Member of the Class Type 347
Static Function Members 347
Destructors 348
Pointers and References to Class Objects 351
Using Pointers As Class Members 351
Defining the Package Class 354
Defining the Truckload Class 355
Implementing the Truckload Class 356
Nested Classes 360
Summary 363
Chapter 12: Operator Overloading 366
Implementing Operators for a Class 366
Operator Overloading 367
Operators That Can Be Overloaded 367
Implementing an Overloaded Operator 368
Global Operator Functions 370
Implementing Full Support for an Operator 371
Implementing All Comparison Operators in a Class 372
Operator Function Idioms 374
Default Class Members 375
Defining the Destructor 376
When to Define a Copy Constructor 378
Implementing the Assignment Operator 378
Implementing Move Operations 380
Overloading the Arithmetic Operators 381
Improving Output Operations 385
Implementing One Operator in Terms of Another 387
Overloading the Subscript Operator 388
Lvalues and the Overloaded Subscript Operator 393
Potential Ambiguities with Conversions 396
Overloading Type Conversions 395
Overloading the Increment and Decrement Operators 396
Function Objects 397
Summary 398
Chapter 13: Inheritance 400
Classes and Object-Oriented Programming 400
Hierarchies 401
Inheritance in Classes 402
Inheritance vs. Aggregation 402
Deriving Classes 403
protected Members of a Class 406
The Access Level of Inherited Class Members 406
Choosing Access Specifiers in Class Hierarchies 408
Changing the Access Specification of Inherited Members 409
Constructor Operation in a Derived Class 410
The Copy Constructor in a Derived Class 413
The Default Constructor in a Derived Class 415
Inheriting Constructors 416
Destructors Under Inheritance 417
The Order in Which Destructors Are Called 418
Duplicate Data Member Names 419
Duplicate Function Member Names 420
Multiple Inheritance 420
Multiple Base Classes 421
Inherited Member Ambiguity 422
Repeated Inheritance 425
Virtual Base Classes 426
Converting Between Related Class Types 427
Summary 427
Chapter 14: Polymorphism 429
Understanding Polymorphism 429
Using a Base Class Pointer 429
Calling Inherited Functions 431
Virtual Functions 434
Requirements for Virtual Function Operation 438
Virtual Functions and Class Hierarchies 438
Using override 440
Using final 440
Access Specifiers and Virtual Functions 441
Default Argument Values in Virtual Functions 442
Virtual Function Calls with Smart Pointers 444
Using References to Call Virtual Functions 444
Calling the Base Class Version of a Virtual Function 445
Converting Between Pointers to Class Objects 446
Dynamic Casts 448
Casting Pointers Dynamically 448
Converting References 450
Determining the Polymorphic Type 450
The Cost of Polymorphism 451
Pure Virtual Functions 452
Abstract Classes 453
Abstract Classes As Interfaces 455
Indirect Abstract Base Classes 456
Destroying Objects Through a Pointer 458
Virtual Destructors 460
Summary 460
Chapter 15: Runtime Errors and Exceptions 462
Handling Errors 462
Understanding Exceptions 463
Throwing an Exception 464
The Exception Handling Process 466
Unhandled Exceptions 467
Code That Causes an Exception to Be Thrown 468
Nested try Blocks 470
How It Works 472
Class Objects as Exceptions 473
Matching a Catch Handler to an Exception 474
How It Works 476
Catching Derived Class Exceptions with a Base Class Handler 477
Rethrowing Exceptions 479
Catching All Exceptions 482
Functions That Throw Exceptions 484
Function try Blocks 484
Functions That Don’t Throw Exceptions 485
Constructor try Blocks 485
Exceptions and Destructors 486
Standard Library Exceptions 486
The Exception Class Definitions 488
Using Standard Exceptions 489
Deriving your own Exception Classes 489
Summary 492
Chapter 16: Class Templates 494
Understanding Class Templates 494
Defining Class Templates 495
Template Parameters 496
A Simple Class Template 497
Defining Function Members of a Class Template 499
Constructor Templates 499
The Destructor Template 500
Subscript Operator Templates 501
The Assignment Operator Template 501
Instantiating a Class Template 502
Static Members of a Class Template 507
Non-Type Class Template Parameters 508
Templates for Function Members with Non-Type Parameters 511
Arguments for Non-Type Parameters 515
Pointers and Arrays as Non-Type Parameters 515
Default Values for Template Parameters 516
Explicit Template Instantiation 517
Special Cases 517
Using static_assert(?) in a Class Template 518
Defining a Class Template Specialization 519
Partial Template Specialization 520
Choosing between Multiple Partial Specializations 520
Friends of Class Templates 521
Class Templates with Nested Classes 523
Function Templates for Stack Members 525
Summary 529
Chapter 17: File Input and Output 531
Input and Output in C++ 531
Understanding Streams 532
Data Transfer Modes 532
Text Mode Operations 532
Binary Mode Operations 532
Advantages of Using Streams 533
Stream Classes 534
Standard Stream Objects 535
Stream Insertion and Extraction Operations 535
Stream Extraction Operations 536
Stream Insertion Operations 536
Stream Manipulators 537
Manipulators with Arguments 539
File Streams 540
Writing a File in Text Mode 541
Reading a File in Text Mode 543
Checking the State of a File Stream 543
Reading the File 544
Setting the Stream Open Mode 546
Managing the Current Stream Position 551
Unformatted Stream Operations 553
Unformatted Stream Input 554
Unformatted Stream Output 556
Errors in Stream Input/Output 556
Input/Output Errors and Exceptions 558
Stream Operations in Binary Mode 559
Writing Numeric Data in Binary 561
File Read/Write Operations 569
Random Access to a File 570
Random Access to a Binary Stream 570
Random File Operations in Practice 570
Implementing the Constructor 571
Checking for the Existence of a File 572
Finding a Prime that follows a Prime 573
Checking for a Prime 574
Implementing the Subscript Operator 574
Using the Primes Class 575
String Streams 576
Objects and Streams 577
Using the Insertion Operator with Objects 577
Using the Extraction Operator with Objects 578
Object I/O in Binary Mode 580
More Complex Objects in Streams 583
Summary 588
Index 590

Erscheint lt. Verlag 12.11.2014
Zusatzinfo XXV, 594 p. 89 illus.
Verlagsort Berkeley
Sprache englisch
Themenwelt Informatik Programmiersprachen / -werkzeuge C / C++
Informatik Software Entwicklung Objektorientierung
ISBN-10 1-4842-0007-1 / 1484200071
ISBN-13 978-1-4842-0007-0 / 9781484200070
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 7,2 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich
Das umfassende Handbuch

von Torsten T. Will

eBook Download (2024)
Rheinwerk Computing (Verlag)
34,93
C++ lernen – professionell anwenden – Lösungen nutzen

von Ulrich Breymann

eBook Download (2023)
Carl Hanser Verlag GmbH & Co. KG
49,99
Das umfassende Handbuch

von Jürgen Wolf; René Krooß

eBook Download (2023)
Rheinwerk Computing (Verlag)
34,93