A complete textbook and reference for engineers to learn the fundamentals of computer programming with modern C++
Introduction to Programming with C++ for Engineers is an original presentation teaching the fundamentals of computer programming and modern C++ to engineers and engineering students.
Professor Cyganek, a highly regarded expert in his field, walks users through basics of data structures and algorithms with the help of a core subset of C++ and the Standard Library, progressing to the object-oriented domain and advanced C++ features, computer arithmetic, memory management and essentials of parallel programming, showing with real world examples how to complete tasks. He also guides users through the software development process, good programming practices, not shunning from explaining low-level features and the programming tools.
Being a textbook, with the summarizing tables and diagrams the book becomes a highly useful reference for C++ programmers at all levels.
Introduction to Programming with C++ for Engineers teaches how to program by:
- Guiding users from simple techniques with modern C++ and the Standard Library, to more advanced object-oriented design methods and language features
- Providing meaningful examples that facilitate understanding of the programming techniques and the C++ language constructions
- Fostering good programming practices which create better professional programmers
- Minimizing text descriptions, opting instead for comprehensive figures, tables, diagrams, and other explanatory material
- Granting access to a complementary website that contains example code and useful links to resources that further improve the reader’s coding ability
- Including test and exam question for the reader’s review at the end of each chapter
Engineering students, students of other sciences who rely on computer programming, and professionals in various fields will find this book invaluable when learning to program with C++.
Table of Contents
Preface xi
Acknowledgments xiii
Abbreviations xv
About the Companion Website xvii
1 Introduction 1
1.1 Structure of the Book 5
1.2 Format Conventions 8
1.3 About the Code and Projects 9
2 Introduction to Programming 13
2.1 Hardware Model 13
2.2 Software Development Ecosystem 16
2.3 Software Development Steps 18
2.4 Representing and Running Algorithms 20
2.4.1 Representing Algorithms 21
2.4.2 Using Online Compilers 22
2.4.3 Structure of a C++ Program 24
2.4.4 Code Analysis 24
2.4.5 Building a Linux Executable 26
2.5 Example Project - Compound Interest Calculator 29
2.5.1 Compound Interest Analysis 29
2.5.2 Implementation of the Interest Calculator 30
2.5.3 Building and Running the Software 33
2.6 Example Project - Counting Occurrences of Characters in Text 34
2.6.1 Problem Analysis and Implementation 34
2.6.2 Running the C++ Code with the Online Compiler 35
2.6.3 Histogram Code, Explained 36
2.7 Summary 39
Questions and Exercises 39
3 C++ Basics 43
3.1 Constants and Variables - Built-In Data Types, Their Range, and Initialization 43
3.2 Example Project - Collecting Student Grades 53
3.3 Our Friend the Debugger 56
3.4 The Basic Data Structure - std::vector 59
3.5 Example Project - Implementing a Matrix as a Vector of Vectors 64
3.6 Special Vector to Store Text - std::string 67
3.7 Using the auto Keyword and decltype for Automatic Type Deduction 72
3.8 Common Standard Algorithms 75
3.9 Structures: Collecting Objects of Various Types 79
3.10 Fixed-Size Arrays 83
3.10.1 Multidimensional Fixed-Size Arrays 85
3.11 References 87
3.12 Pointers 90
3.12.1 Object Access with Pointers 90
3.13 Statements 95
3.13.1 Blocks of Statements and Access to Variables - The Role of Braces 95
3.13.2 C++ Statements 97
3.13.2.1 Conditional Statements 97
3.13.2.2 Loop Statements 103
3.13.2.3 Auxiliary Statements - continue and break 108
3.13.2.4 The goto Statement 110
3.13.2.5 Structural Exception Handling - The try-catch Statement 110
3.14 Functions 112
3.14.1 Anatomy of a Function in C++ 112
3.14.2 Passing Arguments to and from a Function 117
3.14.2.1 Argument Passing by Copy (Value Semantics) 118
3.14.2.2 Indirect Argument Passing by Reference 119
3.14.2.3 Passing by Pointer 121
3.14.3 Function Call Mechanism and Inline Functions 123
3.14.4 Recursive Functions and the Call Stack 125
3.14.5 Function Overloading - Resolving Visibility with Namespaces 126
3.14.6 Lambda Functions 128
3.14.7 More on Lambda Functions 132
3.14.8 Function Pointers 138
3.14.9 Functions in an Object-Oriented Framework 140
3.15 Example Project - Wrapping Objects in a Structure with a Constructor 142
3.15.1 EMatrix in an Object-Oriented Environment 145
3.15.2 Basic Operations with EMatrix 145
3.15.3 Input and Output Operations on EMatrix 147
3.15.4 Basic Mathematical Operations on EMatrix 148
3.15.5 Organizing the Project Files and Running the Application 150
3.15.6 Extending Matrix Initialization with a Simple Random Number Generator 153
3.16 Example Project - Representing Quadratic Equations 154
3.16.1 Definition of a Class to Represent Quadratic Polynomials 155
3.16.2 TQuadEq Member Implementation 162
3.16.3 TQuadEq in Action 165
3.17 Example Project - Tuples and Structured Bindings for Converting Roman Numerals 167
3.17.1 More on std::tuple and the Structured Binding 170
3.17.2 How to Write a Software Unit Test 173
3.17.3 Automating Unit Tests - Using the Standard Random Number Library 174
3.18 Example Project - Building a Currency Calculator Component 176
3.18.1 Currency Exchange Problem Analysis 177
3.18.2 CurrencyCalc Software Design 179
3.18.3 TCurrency Class Representing Currency Records 181
3.18.3.1 C++ Input/Output Manipulators 183
3.18.4 TCurrencyExchanger Class for Exchanging Currency 186
3.18.5 Putting It All Together - The Complete Currency Exchange Program 190
3.19 Operators 196
3.19.1 Summary of the C++ Operators 199
3.19.2 Further Notes on Operators 222
3.20 Summary 223
Questions and Exercises 224
4 Delving into Object-Oriented Programming 227
4.1 Basic Rules and Philosophy of Object-Oriented Design and Programming 227
4.2 Anatomy of a Class 231
4.2.1 Naming Conventions and Self-Documenting Code 233
4.3 Rules for Accessing Class Members 233
4.4 Example Project - TComplex Class for Operator Overloading 235
4.4.1 Definition of the TComplex Class 236
4.4.2 Definition of the TComplex Class Members 241
4.4.3 Test Functions for the TComplex Class 243
4.5 More on References 246
4.5.1 Right and Forward References 246
4.5.2 References vs. Pointers 251
4.5.3 Pitfalls with References 252
4.6 Example Project - Mastering Class Members with the TheCube Class 253
4.6.1 Automatic vs. Explicit Definition of the Constructors 254
4.6.2 TheCube Object Layout and Semantics 264
4.6.3 Shallow vs. Deep Copy Semantics 265
4.6.4 Move Constructor and Move Assignment Semantics 266
4.6.5 Implementation of the TheCube Streaming Operators 267
4.6.6 Validation of TheCube 269
4.7 Example Project - Moving EMatrix to the Class 272
4.7.1 Definition of the EMatrix Class 272
4.7.2 Implementation of the Class Streaming Operators 274
4.7.3 Implementation of the Arithmetic Operators 278
4.7.4 Testing Matrix Operations 279
4.8 Introduction to Templates and Generic Programming 281
4.8.1 Generalizing a Class with Templates 282
4.8.2 Template Specializations 286
4.8.3 Template Functions and Type Checking 287
4.8.4 Example Project - Designing Template Classes with TStack 289
4.8.4.1 Design and Implementation of the TStackFor Class 290
4.8.4.2 Testing TStack 293
4.8.5 Template Member Functions 294
4.9 Class Relations - “Know,” “Has-A,” and “Is-A” 297
4.10 Example Project - Extending Functionality Through Class Inheritance with TComplexQuadEq 304
4.11 Virtual Functions and Polymorphism 310
4.12 More on the Virtual Mechanism 316
4.13 The Curiously Recurring Template Pattern and Static Polymorphism 318
4.14 Mixin Classes 322
4.15 Example Project - The TLongNumberFor Class for Efficient Storage of Numbers of Any Length 323
4.15.1 Binary-Coded Decimal Representation 325
4.15.2 Endianness 326
4.15.3 Definition of the TLongNumberFor Class 326
4.15.3.1 Type-Converting Operations 329
4.15.3.2 TLongNumberFor Test Function 333
4.15.4 Designing Classes for PESEL IDs 335
4.15.4.1 Aggregating PESEL 336
4.15.4.2 Inherited PESEL 337
4.15.4.3 LongNumber Project Organization 338
4.15.5 Extending the Functionality of TLongNumberFor with the Proxy Pattern 340
4.15.5.1 Definition of the Proxy Class 341
4.15.5.2 Testing the Functionality of the TLongNumberFor Class with the Proxy Pattern 343
4.16 Strong Types 345
4.17 Summary 346
Questions and Exercises 346
5 Memory Management 349
5.1 Types of Data Storage 349
5.2 Dynamic Memory Allocation - How to Avoid Memory Leaks 349
5.2.1 Introduction to Smart Pointers and Resource Management 358
5.2.1.1 RAII and Stack Unwinding 359
5.3 Smart Pointers - An Overview with Examples 360
5.3.1 More on std::unique_ptr 360
5.3.1.1 Context for Using std::unique_ptr 360
5.3.1.2 Factory Method Design Pattern 374
5.3.1.3 Custom deletes for unique_ptr 376
5.3.1.4 Constructions to Avoid When Using unique_ptr 378
5.3.2 More on shared_ptr and weak_ptr 378
5.4 Summary 381
Questions and Exercises 381
6 Advanced Object-Oriented Programming 383
6.1 Functional Objects 383
6.2 Example Project - Extending the Currency Search in XML Files, and Using State Machine and Regular Expressions with the regex Library 389
6.2.1 Pattern Matching with the Regular Expression Library 390
6.2.2 State Machine Pattern 392
6.2.3 Implementing the Extended Class 393
6.2.4 Project Extension - Loading Currency Information from the Internet 399
6.2.5 Launching the Extended Version of CurrencyCalc 405
6.2.6 Building a Static Library and a Terminal Window Application 409
6.2.7 C++ Filesystem 410
6.2.8 User Interface 419
6.2.8.1 Definition of the CC_GUI Class 420
6.2.8.2 Definitions of Members of the CC_GUI Class and the Callback Mechanism 423
6.2.8.3 Launching the GUI-Based Application 430
6.3 System Clocks and Time Measurements 431
6.4 Time Measurement for Function Execution 435
6.5 Range Class 437
6.5.1 Functional Programming and the Ranges Library 442
6.6 Example Project - Parsing Expressions 443
6.6.1 Defining Language Expressions with Formal Grammar Rules 444
6.6.2 Design of the Expression-Processing Framework 446
6.6.3 The First Expression Interpreter 447
6.6.4 Building the Syntax Tree with the Composite Design Pattern 451
6.6.4.1 The Composite Design Pattern to Define the Nodes of a Tree 452
6.6.4.2 Implementation of the TNode Hierarchy and Cooperation with Visitors 453
6.6.4.3 Implementation of the ValueLeafNode Class 455
6.6.4.4 Implementation of the BinOperator Class 457
6.6.4.5 Implementation of the PlusOperator Class 458
6.6.4.6 Deep Copying Node Objects - The Prototyping Mechanism 459
6.6.5 Interpreter to Build a Syntax Tree 460
6.6.6 Stack for Smart Pointers 466
6.6.7 Traversing Trees with the Visitor Design Pattern 469
6.6.7.1 The Expression-Evaluating Visitor 472
6.6.7.2 The Expression-Printing Visitor 474
6.6.8 Testing the Interpreters 476
6.6.9 Representing Expressions on a Stack in Reverse Polish Notation 479
6.6.9.1 Reverse Polish Notation 479
6.6.9.2 Algorithm for Evaluating an RPN Expression 480
6.7 Summary 485
Questions and Exercises 485
7 Computer Arithmetic 489
7.1 Integer Value Representation 489
7.1.1 Base Conversion Algorithm 491
7.1.2 Hexadecimal and Octal Representations 492
7.1.3 Binary Addition 493
7.1.4 Negative Values and Subtraction 494
7.1.5 Arithmetic Control Flags 496
7.1.6 Representing Fractions 498
7.2 Binary Shift Operations 501
7.3 Example Project - Software Model for Fixed-Point Representations 503
7.3.1 Fixed-Point Numbers and Their Arithmetic 503
7.3.2 Definition of the FxFor Class 504
7.3.3 Selected Methods of the FxFor Class 510
7.3.4 Applications of FxFor 516
7.4 Floating-Point Representations 519
7.4.1 Number Representation in Floating-Point Format 520
7.4.2 Distribution of Floating-Point Numbers and the Computational Consequences 524
7.4.3 Real-Value Approximation Error with Floating-Point Representations 527
7.4.4 The IEEE 754 Standard for Floating-Point Arithmetic 530
7.4.5 The Standard FP Operation Model 537
7.4.6 Computations That Are Conscious of Numerical Errors 537
7.4.7 Example Project - Evaluating the Summation Algorithms 539
7.4.8 Example Project - The Newton Method of Finding the Roots of a Function 544
7.4.8.1 Function to Compute Square Roots Based on Newton’s Iteration 548
7.5 Summary 550
Questions and Exercises 551
8 Basics of Parallel Programming 553
8.1 Basic Concepts of Parallel Computations 553
8.2 Adding Parallelism to the Standard Algorithms 556
8.3 Launching Asynchronous Tasks 559
8.4 Parallelization with the OpenMP Library 561
8.4.1 Launching a Team of Threads and Providing Exclusive Access Protection 562
8.4.2 Loop Parallelization and Reduction Operations 564
8.4.3 Massive Data Parallelization 567
8.5 Summary 575
Questions and Exercises 575
Appendix 577
A.1 Preprocessor Directives 577
A.2 Short Introduction to C 582
A.2.1 Built‐in Arrays 583
A.2.1.1 Multidimensional Fixed-Size Arrays 585
A.2.2 Passing Arrays to Functions - The Main Function 586
A.2.3 C Structures 590
A.2.4 C Functions and Input/Output 591
A.2.5 Unions 592
A.2.6 Memory and String Operations 593
A.2.7 Binding C and C++ Code 599
A.3 Linking and Binary Organization of C/C++ Objects 599
A.4 Graphical User and Web Interfaces for C++ Projects 601
A.5 Converting Bin, Oct, Dec, and Hex Values with FixBinCalc 603
A.6 Programming Toolchain 604
A.6.1 Project-Generating Tool (CMake) 604
A.6.2 Source Version Control and Repositories 609
A.6.3 Profiler 610
A.7 Software Testing 612
A.8 Summary 616
Questions and Exercises 616
Bibliography 619
Index 623