CPlusPlusNotesForProfessionals.pdf

(4759 KB) Pobierz
Notes for Professionals
C++
C++
Notes for Professionals
of professional hints and tricks
600+ pages
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial C++ group(s) or company(s).
All trademarks and registered trademarks are
the property of their respective owners
Contents
About
................................................................................................................................................................................... 1
Chapter 1: Getting started with C++
.................................................................................................................... 2
Section 1.1: Hello World
................................................................................................................................................. 2
Section 1.2: Comments
.................................................................................................................................................. 3
Section 1.3: The standard C++ compilation process
.................................................................................................. 5
Section 1.4: Function
...................................................................................................................................................... 5
Section 1.5: Visibility of function prototypes and declarations
................................................................................. 8
Section 1.6: Preprocessor
.............................................................................................................................................. 9
Chapter 2: Literals
...................................................................................................................................................... 11
Section 2.1: this
............................................................................................................................................................. 11
Section 2.2: Integer literal
........................................................................................................................................... 11
Section 2.3: true
........................................................................................................................................................... 12
Section 2.4: false
.......................................................................................................................................................... 13
Section 2.5: nullptr
....................................................................................................................................................... 13
Chapter 3: Inline variables
..................................................................................................................................... 14
Section 3.1: Defining a static data member in the class definition
......................................................................... 14
Chapter 4: operator precedence
....................................................................................................................... 15
Section 4.1: Logical && and || operators: short-circuit
............................................................................................. 15
Section 4.2: Unary Operators
.................................................................................................................................... 16
Section 4.3: Arithmetic operators
.............................................................................................................................. 16
Section 4.4: Logical AND and OR operators
............................................................................................................ 17
Chapter 5: Floating Point Arithmetic
................................................................................................................ 18
Section 5.1: Floating Point Numbers are Weird
........................................................................................................ 18
Chapter 6: Bit Operators
........................................................................................................................................ 19
Section 6.1: | - bitwise OR
............................................................................................................................................ 19
Section 6.2: ^ - bitwise XOR (exclusive OR)
.............................................................................................................. 19
Section 6.3: & - bitwise AND
....................................................................................................................................... 21
Section 6.4: << - left shift
............................................................................................................................................. 21
Section 6.5: >> - right shift
.......................................................................................................................................... 22
Chapter 7: Bit Manipulation
................................................................................................................................... 24
Section 7.1: Remove rightmost set bit
....................................................................................................................... 24
Section 7.2: Set all bits
................................................................................................................................................ 24
Section 7.3: Toggling a bit
.......................................................................................................................................... 24
Section 7.4: Checking a bit
......................................................................................................................................... 24
Section 7.5: Counting bits set
..................................................................................................................................... 25
Section 7.6: Check if an integer is a power of 2
....................................................................................................... 26
Section 7.7: Setting a bit
............................................................................................................................................. 26
Section 7.8: Clearing a bit
........................................................................................................................................... 26
Section 7.9: Changing the nth bit to x
....................................................................................................................... 26
Section 7.10: Bit Manipulation Application: Small to Capital Letter
........................................................................ 27
Chapter 8: Bit fields
................................................................................................................................................... 28
Section 8.1: Declaration and Usage
........................................................................................................................... 28
Chapter 9: Arrays
....................................................................................................................................................... 29
Section 9.1: Array initialization
.................................................................................................................................... 29
Section 9.2: A fixed size raw array matrix (that is, a 2D raw array)
...................................................................... 30
Section 9.3: Dynamically sized raw array
................................................................................................................. 30
Section 9.4: Array size: type safe at compile time
................................................................................................... 31
Section 9.5: Expanding dynamic size array by using std::vector
........................................................................... 32
Section 9.6: A dynamic size matrix using std::vector for storage
........................................................................... 33
Chapter 10: Iterators
................................................................................................................................................. 36
Section 10.1: Overview
................................................................................................................................................. 36
Section 10.2: Vector Iterator
....................................................................................................................................... 39
Section 10.3: Map Iterator
........................................................................................................................................... 39
Section 10.4: Reverse Iterators
................................................................................................................................... 40
Section 10.5: Stream Iterators
.................................................................................................................................... 41
Section 10.6: C Iterators (Pointers)
............................................................................................................................ 41
Section 10.7: Write your own generator-backed iterator
........................................................................................ 42
Chapter 11: Basic input/output in c++
.............................................................................................................. 44
Section 11.1: user input and standard output
............................................................................................................ 44
Chapter 12: Loops
....................................................................................................................................................... 45
Section 12.1: Range-Based For
................................................................................................................................... 45
Section 12.2: For loop
.................................................................................................................................................. 47
Section 12.3: While loop
............................................................................................................................................... 49
Section 12.4: Do-while loop
......................................................................................................................................... 50
Section 12.5: Loop Control statements : Break and Continue
................................................................................. 51
Section 12.6: Declaration of variables in conditions
................................................................................................. 52
Section 12.7: Range-for over a sub-range
................................................................................................................ 53
Chapter 13: File I/O
.................................................................................................................................................... 55
Section 13.1: Writing to a file
....................................................................................................................................... 55
Section 13.2: Opening a file
......................................................................................................................................... 55
Section 13.3: Reading from a file
................................................................................................................................ 56
Section 13.4: Opening modes
..................................................................................................................................... 58
Section 13.5: Reading an ASCII file into a std::string
................................................................................................. 59
Section 13.6: Writing files with non-standard locale settings
.................................................................................. 60
Section 13.7: Checking end of file inside a loop condition, bad practice?
.............................................................. 61
Section 13.8: Flushing a stream
.................................................................................................................................. 62
Section 13.9: Reading a file into a container
............................................................................................................. 62
Section 13.10: Copying a file
........................................................................................................................................ 63
Section 13.11: Closing a file
.......................................................................................................................................... 63
Section 13.12: Reading a `struct` from a formatted text file
.................................................................................... 64
Chapter 14: C++ Streams
........................................................................................................................................ 66
Section 14.1: String streams
........................................................................................................................................ 66
Section 14.2: Printing collections with iostream
........................................................................................................ 67
Chapter 15: Stream manipulators
...................................................................................................................... 69
Section 15.1: Stream manipulators
............................................................................................................................. 69
Section 15.2: Output stream manipulators
............................................................................................................... 74
Section 15.3: Input stream manipulators
................................................................................................................... 76
Chapter 16: Flow Control
......................................................................................................................................... 78
Section 16.1: case
......................................................................................................................................................... 78
Section 16.2: switch
...................................................................................................................................................... 78
Section 16.3: catch
....................................................................................................................................................... 78
Section 16.4: throw
....................................................................................................................................................... 79
Section 16.5: default
..................................................................................................................................................... 80
Section 16.6: try
............................................................................................................................................................ 80
Section 16.7: if
............................................................................................................................................................... 80
Section 16.8: else
.......................................................................................................................................................... 81
Section 16.9: Conditional Structures: if, if..else
........................................................................................................... 81
Section 16.10: goto
....................................................................................................................................................... 82
Section 16.11: Jump statements : break, continue, goto, exit
................................................................................... 82
Section 16.12: return
..................................................................................................................................................... 85
Chapter 17: Metaprogramming
........................................................................................................................... 87
Section 17.1: Calculating Factorials
............................................................................................................................ 87
Section 17.2: Iterating over a parameter pack
......................................................................................................... 89
Section 17.3: Iterating with std::integer_sequence
................................................................................................... 90
Section 17.4: Tag Dispatching
.................................................................................................................................... 91
Section 17.5: Detect Whether Expression is Valid
..................................................................................................... 91
Section 17.6: If-then-else
............................................................................................................................................. 93
Section 17.7: Manual distinction of types when given any type T
.......................................................................... 93
Section 17.8: Calculating power with C++11 (and higher)
......................................................................................... 94
Section 17.9: Generic Min/Max with variable argument count
............................................................................... 95
Chapter 18: const keyword
.................................................................................................................................... 96
Section 18.1: Avoiding duplication of code in const and non-const getter methods
............................................ 96
Section 18.2: Const member functions
...................................................................................................................... 97
Section 18.3: Const local variables
............................................................................................................................. 98
Section 18.4: Const pointers
........................................................................................................................................ 98
Chapter 19: mutable keyword
............................................................................................................................ 100
Section 19.1: mutable lambdas
................................................................................................................................. 100
Section 19.2: non-static class member modifier
.................................................................................................... 100
Chapter 20: Friend keyword
............................................................................................................................... 102
Section 20.1: Friend function
.................................................................................................................................... 102
Section 20.2: Friend method
.................................................................................................................................... 103
Section 20.3: Friend class
......................................................................................................................................... 103
Chapter 21: Type Keywords
................................................................................................................................. 105
Section 21.1: class
....................................................................................................................................................... 105
Section 21.2: enum
..................................................................................................................................................... 106
Section 21.3: struct
..................................................................................................................................................... 107
Section 21.4: union
..................................................................................................................................................... 107
Chapter 22: Basic Type Keywords
................................................................................................................... 109
Section 22.1: char
....................................................................................................................................................... 109
Section 22.2: char16_t
............................................................................................................................................... 109
Section 22.3: char32_t
.............................................................................................................................................. 109
Section 22.4: int
......................................................................................................................................................... 109
Section 22.5: void
...................................................................................................................................................... 109
Section 22.6: wchar_t
............................................................................................................................................... 110
Section 22.7: float
...................................................................................................................................................... 110
Section 22.8: double
.................................................................................................................................................. 110
Section 22.9: long
...................................................................................................................................................... 110
Section 22.10: short
................................................................................................................................................... 111
Section 22.11: bool
...................................................................................................................................................... 111
Chapter 23: Variable Declaration Keywords
.............................................................................................. 112
Section 23.1: decltype
................................................................................................................................................ 112
Section 23.2: const
.................................................................................................................................................... 112
Section 23.3: volatile
................................................................................................................................................. 113
Section 23.4: signed
.................................................................................................................................................. 113
Section 23.5: unsigned
.............................................................................................................................................. 113
Chapter 24: Keywords
........................................................................................................................................... 115
Section 24.1: asm
....................................................................................................................................................... 115
Section 24.2: Dierent keywords
............................................................................................................................. 115
Section 24.3: typename
............................................................................................................................................ 119
Section 24.4: explicit
.................................................................................................................................................. 120
Section 24.5: sizeof
.................................................................................................................................................... 120
Section 24.6: noexcept
.............................................................................................................................................. 121
Chapter 25: Returning several values from a function
........................................................................ 123
Section 25.1: Using std::tuple
.................................................................................................................................... 123
Section 25.2: Structured Bindings
............................................................................................................................ 124
Section 25.3: Using struct
......................................................................................................................................... 125
Section 25.4: Using Output Parameters
.................................................................................................................. 126
Section 25.5: Using a Function Object Consumer
.................................................................................................. 127
Section 25.6: Using std::pair
..................................................................................................................................... 128
Section 25.7: Using std::array
................................................................................................................................... 128
Section 25.8: Using Output Iterator
......................................................................................................................... 128
Section 25.9: Using std::vector
................................................................................................................................. 129
Chapter 26: Polymorphism
.................................................................................................................................. 130
Section 26.1: Define polymorphic classes
............................................................................................................... 130
Section 26.2: Safe downcasting
............................................................................................................................... 131
Section 26.3: Polymorphism & Destructors
............................................................................................................ 132
Chapter 27: References
......................................................................................................................................... 134
Section 27.1: Defining a reference
........................................................................................................................... 134
Chapter 28: Value and Reference Semantics
............................................................................................ 135
Section 28.1: Definitions
............................................................................................................................................ 135
Section 28.2: Deep copying and move support
..................................................................................................... 135
Chapter 29: C++ function "call by value" vs. "call by reference"
..................................................... 139
Section 29.1: Call by value
........................................................................................................................................ 139
Chapter 30: Copying vs Assignment
............................................................................................................... 141
Section 30.1: Assignment Operator
......................................................................................................................... 141
Section 30.2: Copy Constructor
............................................................................................................................... 141
Section 30.3: Copy Constructor Vs Assignment Constructor
............................................................................... 142
Chapter 31: Pointers
................................................................................................................................................ 144
Section 31.1: Pointer Operations
............................................................................................................................... 144
Section 31.2: Pointer basics
...................................................................................................................................... 144
Section 31.3: Pointer Arithmetic
................................................................................................................................ 146
Chapter 32: Pointers to members
.................................................................................................................... 148
Section 32.1: Pointers to static member functions
................................................................................................. 148
Section 32.2: Pointers to member functions
.......................................................................................................... 148
Section 32.3: Pointers to member variables
........................................................................................................... 149
Section 32.4: Pointers to static member variables
................................................................................................ 149
Chapter 33: The This Pointer
............................................................................................................................... 151
Section 33.1: this Pointer
........................................................................................................................................... 151
Section 33.2: Using the this Pointer to Access Member Data
............................................................................... 153
Section 33.3: Using the this Pointer to Dierentiate Between Member Data and Parameters
........................ 153
Section 33.4: this Pointer CV-Qualifiers
................................................................................................................... 154
Section 33.5: this Pointer Ref-Qualifiers
.................................................................................................................. 157
Chapter 34: Smart Pointers
................................................................................................................................ 159
Zgłoś jeśli naruszono regulamin