
Introduction to Scientific Programming in C++17/Fortran2003 Victor Eijkhout 2017-2021, last formatted September 14, 2021 2 Introduction to Scientific Programming Contents I Introduction 19 1 Introduction 21 1.1 Programming and computational thinking 21 1.1.1 History 21 1.1.2 Is programming science, art, or craft? 22 1.1.3 Computational thinking 23 1.1.4 Hardware 25 1.1.5 Algorithms 25 1.2 About the choice of language 26 2 Warming up 29 2.1 Programming environment 29 2.1.1 Language support in your editor 29 2.2 Compiling 30 2.3 Your environment 30 3 Teachers guide 31 3.1 Justification 31 3.2 Timeline for a C++/F03 course 32 3.2.1 Advanced topics 32 3.2.2 Project-based teaching 32 3.2.3 Choice: Fortran or advanced topics 32 3.3 Scaffolding 35 3.4 Grading guide 35 3.4.1 Code layout and naming 35 3.4.2 Basic elements 36 3.4.3 Conditionals 36 3.4.4 Looping 36 3.4.5 Functions 36 3.4.6 Scope 36 3.4.7 Structures 37 3.4.8 Classes 37 3.4.9 Arrays and vectors 37 3.4.10 Strings 37 3.4.11 Other remarks 37 3 Contents II C++ 39 4 Basic elements of C++ 41 4.1 From the ground up: Compiling C++ 41 4.1.1 A quick word about unix commands 42 4.1.2 C++ is a moving target 43 4.2 Statements 43 4.3 Variables 44 4.3.1 Variable declarations 45 4.3.2 Assignments 46 4.3.3 Terminal input 47 4.3.4 Datatypes 47 4.3.5 Complex numbers 50 4.3.6 Initialization 50 4.4 Input/Output, or I/O as we say 50 4.5 Expressions 50 4.5.1 Numbers 51 4.5.2 Numerical expressions 51 4.5.3 Truth values 52 4.5.4 Type conversions 52 4.6 Advanced topics 54 4.6.1 Integer precision 54 4.6.2 Unsigned types 54 4.6.3 The main program and the return statement 54 4.6.4 Number values and undefined values 55 4.6.5 Constants 55 4.6.6 Numerical precision 56 4.7 Review questions 56 5 Conditionals 57 5.1 Conditionals 57 5.2 Operators 58 5.3 Switch statement 60 5.4 Scopes 61 5.5 Advanced topics 61 5.5.1 Short-circuit evaluation 61 5.5.2 Ternary if 61 5.5.3 Initializer 62 5.6 Review questions 62 6 Looping 65 6.1 The ‘for’ loop 65 6.1.1 Loop syntax 69 6.2 Looping until 70 6.3 Advanced topics 73 6.3.1 Parallelism 73 6.4 Exercises 74 7 Functions 75 7.1 Function definition and call 75 4 Introduction to Scientific Programming Contents 7.1.1 Why use functions? 77 7.2 Anatomy of a function definition and call 79 7.2.1 Another option for defining functions 80 7.3 Void functions 80 7.4 Parameter passing 81 7.4.1 Pass by value 81 7.4.2 Pass by reference 83 7.5 Recursive functions 86 7.6 Other function topics 88 7.6.1 Math functions 88 7.6.2 Default arguments 88 7.6.3 Polymorphic functions 89 7.6.4 Stack overflow 89 7.7 Review questions 90 7.7.1 Eight queens 91 8 Scope 93 8.1 Scope rules 93 8.1.1 Lexical scope 93 8.1.2 Shadowing 93 8.1.3 Lifetime versus reachability 94 8.1.4 Scope subtleties 95 8.2 Static variables 96 8.3 Scope and memory 97 8.4 Review questions 97 9 Classes and objects 99 9.1 What is an object? 99 9.1.1 First examples: points in the plane 99 9.1.2 Constructor 101 9.1.3 Data members, introduction 101 9.1.4 Methods, introduction 102 9.1.5 Initialization 103 9.1.6 Methods 104 9.1.7 Default constructor 106 9.1.8 Accessors 107 9.1.9 Examples 108 9.2 Inclusion relations between classes 109 9.2.1 Literal and figurative has-a 109 9.3 Inheritance 110 9.3.1 Methods of base and derived classes 112 9.3.2 Virtual methods 112 9.3.3 Friend classes 113 9.3.4 Multiple inheritance 114 9.4 Advanced topics 114 9.4.1 Class prototypes 114 9.4.2 Returning by reference 115 9.4.3 Accessor functions 115 Victor Eijkhout 5 Contents 9.4.4 Polymorphism 116 9.4.5 Operator overloading 116 9.4.6 Copy constructor 118 9.4.7 Destructor 119 9.4.8 ‘this’ pointer 121 9.4.9 Static members 122 9.5 Review question 123 10 Arrays 125 10.1 Some simple examples 125 10.1.1 Vector creation 125 10.1.2 Initialization 126 10.1.3 Element access 127 10.1.4 Access out of bounds 127 10.2 Going over all array elements 129 10.2.1 Ranging over a vector 129 10.2.2 Ranging over the indices 129 10.2.3 Ranging by reference 130 10.3 Vector are a class 131 10.3.1 Vector methods 132 10.3.2 Vectors are dynamic 132 10.4 Vectors and functions 133 10.4.1 Pass vector to function 134 10.4.2 Vector as function return 135 10.5 Vectors in classes 136 10.5.1 Timing 137 10.6 Wrapping a vector in an object 138 10.7 Multi-dimensional cases 138 10.7.1 Matrix as vector of vectors 139 10.7.2 A better matrix class 139 10.8 Advanced topics 140 10.8.1 Iterators 140 10.8.2 Old-style arrays 141 10.8.3 Stack and heap allocation 143 10.8.4 The Array class 144 10.8.5 Span 144 10.9 Exercises 145 11 Strings 149 11.1 Characters 149 11.2 Basic string stuff 149 11.3 String streams 153 11.4 Advanced topics 153 11.4.1 Raw string literals 153 11.4.2 Conversation to/from string 153 11.4.3 Unicode 154 11.5 C strings 154 12 Input/output 155 6 Introduction to Scientific Programming Contents 12.1 Screen output 155 12.1.1 Floating point output 158 12.1.2 Saving and restoring settings 159 12.2 File output 160 12.3 Output your own classes 160 12.4 Output buffering 161 12.4.1 The need for flushing 161 12.4.2 Performance considerations 161 12.5 Input 162 12.5.1 File input 162 12.5.2 Input streams 163 13 Lambda functions 165 13.1 Passing lambdas around 166 13.1.1 Lambda members of classes 166 13.2 Captures 167 13.3 Capture by reference 167 13.4 More 168 13.4.1 Generic lambdas 168 13.4.2 Algorithms 168 13.4.3 C-style function pointers 168 14 Iterators, Algorithms, Ranges 169 14.1 Iterators 169 14.1.1 Iterators 169 14.1.2 Simple iterating, the hard way 170 14.1.3 Forming sub-arrays 170 14.1.4 Vector operations through iterators 171 14.2 Algorithms 172 14.2.1 Any/all/each 172 14.2.2 Reduction 173 14.3 Ranges 174 14.3.1 Infinite sequences 176 15 References 177 15.1 Reference 177 15.2 Pass by reference 177 15.3 Reference to class members 178 15.4 Reference to array members 180 15.5 rvalue references 181 16 Pointers 183 16.1 The ‘arrow’ notation 183 16.2 Making a shared pointer 184 16.2.1 Pointers and arrays 185 16.2.2 Smart pointers versus address pointers 186 16.3 Garbage collection 187 16.4 More about pointers 188 16.4.1 Get the pointed data 188 16.5 Advanced topics 189 Victor Eijkhout 7 Contents 16.5.1 Unique pointers 189 16.5.2 Base and derived pointers 189 16.5.3 Shared pointer to ‘this’ 189 16.5.4 Weak pointers 190 16.5.5 Null pointer 191 16.5.6 Opaque pointer 191 16.5.7 Pointers to non-objects 191 16.6 Smart pointers vs C pointers 192 17 C-style pointers and arrays 193 17.1 What is a pointer 193 17.2 Pointers and addresses, C style 193 17.3 Arrays and pointers 195 17.4 Pointer arithmetic 197 17.5 Multi-dimensional arrays 197 17.6 Parameter passing 197 17.6.1 Allocation 198 17.6.2 Use of new 200 17.7 Memory leaks 201 17.8 Const pointers 201 18 Const 203 18.1 Const arguments 203 18.2 Const references 203 18.2.1 Const references in range-based loops 205 18.3 Const methods 205 18.4 Overloading on const 205 18.5 Const and pointers 206 18.5.1 Old-style const pointers 207 18.6 Mutable 208 18.7 Compile-time constants 209 19 Prototypes and header files 211 19.1 Include files 211 19.2 Prototypes for functions 212 19.2.1 Separate compilation 213 19.2.2 Header files 213 19.2.3 C and C++ headers 214 19.3 Prototypes for class methods 215 19.4 Header files and templates 215 19.5 Namespaces and header files 216 19.6 Global variables and header files 216 19.7 Modules 217 20 Namespaces 219 20.1 Solving name conflicts 219 20.1.1 Namespace header files 220 20.2 Best practices 221 21 Preprocessor 223 21.1 Include files 223 8 Introduction to Scientific Programming Contents 21.1.1 Kinds of includes 223 21.1.2 Search paths 224 21.2 Textual substitution 224 21.2.1 Dynamic definition of macros 225 21.2.2 A new use for ‘using’ 225 21.2.3 Parametrized macros 225 21.3 Conditionals 226 21.3.1 Check on a value 226 21.3.2 Check for macros 226 21.3.3 Including a file only once 226 21.4 Other pragmas 227 22 Templates 229 22.1 Templated functions 229 22.2 Templated classes 230 22.2.1 Specific implementation 230 22.3 Example 230 22.4 Concepts 232 23 Error handling 233 23.1 General discussion 233 23.2 Mechanisms to support error handling and debugging 234 23.2.1 Assertions 234 23.2.2 Exception handling 235 23.2.3 ‘Where does this error come from’ 238 23.2.4 Legacy mechanisms 238 23.2.5 Legacy C mechanisms 238 23.3 Tools 238 24 Standard Template Library 239 24.1 Complex numbers 239 24.2 Containers 240 24.2.1 Maps: associative arrays 240 24.3 Regular expression 241 24.3.1 Regular expression syntax 242 24.4 Tuples and structured bindings 242 24.5 Union-like stuff: tuples, optionals, variants 243 24.5.1 Tuples 243 24.5.2 Optional 244 24.5.3 Variant 244 24.5.4 Any 246 24.6 Limits 246 24.7 Common numbers 248 24.8 Random numbers 248 24.8.1 C random function 249 24.9 Time 250 24.10 File system 251 24.11 Regular expressions 251 24.12 Enum classes 251 Victor Eijkhout 9 Contents 25 Concurrency 253 25.1 Thread creation 253 25.2 Synchronization 253 26 Obscure stuff 255 26.1 Auto 255 26.1.1 Declarations 255 26.1.2 Auto and function definitions 256 26.1.3 decltype: declared type 256 26.2 Iterating over classes 257 26.3 Casts 260 26.3.1 Static cast 260 26.3.2 Dynamic cast 261 26.3.3 Const cast 262 26.3.4 Reinterpret cast 262 26.3.5 A word about void pointers 262 26.4 lvalue vs rvalue 263 26.4.1 Conversion 264 26.4.2 References 264 26.4.3 Rvalue references 264 26.5 Move semantics 265 26.6 Graphics 265 26.7 Standards timeline 266 26.7.1 C++98/C++03 266 26.7.2 C++11 266 26.7.3 C++14 266 26.7.4 C++17 267 26.7.5 C++20 267 27 Graphics 269 28 C++ for C programmers 271 28.1 I/O 271 28.2 Arrays 271 28.2.1 Vectors from C arrays 271 28.3 Dynamic storage 272 28.4 Strings 272
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages515 Page
-
File Size-