Introduction to Programming with C++ for Engineers by Boguslaw Cyganek, ISBN-13: 978-1119431107
[PDF eBook eTextbook]
- Publisher: Wiley-IEEE Press; 1st edition (February 8, 2021)
- Language: English
- 656 pages
- ISBN-10: 1119431107
- ISBN-13: 978-1119431107
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 first learning to program in C and C++.
Table of Contents:
Preface 8
Acknowledgements 9
1 Intro 11
1.1 Structure of the Book 15
1.2 Format Conventions 17
1.3 About the Code and Projects 19
2 Introduction to Programming 21
2.1 Hardware Model 21
2.2 Software Development Ecosystem 24
2.3 Steps of Software Development 26
2.4 Representing and Running Algorithms 28
2.4.1 Representing Algorithms 28
2.4.2 Using On-Line Compilers 30
2.4.3 Structure of a C++ Program 31
2.4.4 Code Analysis 32
2.4.5 (*) Building a Linux Executable 33
2.5 EXAMPLE PROJECT – Compound Interest Calculator 36
2.5.1 Compound Interest Analysis 36
2.5.2 Implementation of the Interest Calculator 37
2.5.3 Software Building and Testing 40
2.6 EXAMPLE PROJECT – How to Count Occurrences of Characters in a Word? 41
2.6.1 Problem Analysis and the First Code 41
2.6.2 Running the C++ Code with the On-Line Compiler 42
2.6.3 Histogram Code Explained 43
2.7 Summary 46
2.8 Questions & Exercises 46
3 C++ Basics 48
3.1 Constants and Variables – Built-In Data Types, their Range and Initialization 48
3.2 The Basic Data Structure – std::vector 57
3.3 EXAMPLE PROJECT – Implementing a matrix as a vector of vectors 63
3.4 Special Vector to Store Texts – std::string 65
3.5 Using auto keyword and decltype for automatic type deduction 67
3.6 Common Standard Algorithms 70
3.7 Structures: Collecting Objects of Various Types 72
3.8 (*) Fixed-size Arrays 76
3.8.1 Multi-dimensional Fixed-Size Arrays 78
3.9 References 79
3.10 (*) Pointers 81
3.10.1 Objects Access with Pointers 81
3.11 (*) More on References 86
3.11.1 Right and Forward References 86
3.11.2 References vs. Pointers 90
3.11.3 Pitfalls with References 90
3.12 Statements 92
3.12.1 Blocks of Statements and Access to Variables – The Role of the Braces 92
3.12.2 C++ Statements 95
3.12.2.1 Conditional Statements 95
3.12.2.2 Loop Statements 99
3.12.2.3 Auxiliary Statements – continue and break 103
3.12.2.4 The goto Statement 106
3.12.2.5 Structural Exception Handling – the try-catch Statement 107
3.13 Functions 108
3.13.1 Anatomy of a Function in C++ 108
3.13.2 Passing Arguments to and from a Function 113
3.13.2.1 Argument Passing by Copy (Value Semantics) 113
3.13.2.2 Indirect Argument Passing by Reference 114
3.13.2.3 (*) Passing by Pointer 116
3.13.3 Function Call Mechanism and Inline Functions 118
3.13.4 Recursive Functions and the Call Stack 119
3.13.5 Function Overloading – Resolving Visibility with Namespaces 120
3.13.6 Lambda Functions 122
3.13.7 (*) More on Lambda Functions 126
3.13.8 (*) Function Pointers 132
3.13.9 (*) Functions in Object Oriented Framework 133
3.14 EXAMPLE PROJECT – Wrapping Objects in a Structure with a Constructor 136
3.14.1 EMatrix in Object-Oriented Environment 138
3.14.2 Basic Operations with EMatrix 139
3.14.3 Input and output operations on EMatrix 140
3.14.4 Basic mathematical operations on EMatrix 142
3.14.5 Organizing the project files and running the application 144
3.14.6 Extending Matrix Initialization with a Simple Random Generator 146
3.15 EXAMPLE PROJECT – Representing Quadratic Equations 147
3.15.1 Definition of a Class to Represent the Quadratic Polynomials 148
3.15.2 TQuadEq Member Implementation 153
3.15.3 TQuadEq in Action 156
3.16 EXAMPLE PROJECT – Tuples and Structured Bindings for Converting Roman Numbers 158
3.16.1 More on std::tuple and the Structured Binding 160
3.16.2 How to Write a Software Unit Test? 162
3.16.3 (*) Automating Unit Tests – Using the Standard Random Library 163
3.17 (*) EXAMPLE PROJECT – Building the Currency Calculator Component 165
3.17.1 Currency Exchange Problem Analysis 165
3.17.2 CurrencyCalc Software Design 167
3.17.3 TCurrency Class to Represent a Currency Record 169
3.17.3.1 C++ Input/Output Manipulators 171
3.17.4 TCurrencyExchanger Class for Currency Exchanging 173
3.17.5 Putting it All Together – The Complete Program for Currency Exchange 177
3.18 Operators 182
3.18.1 Summary of the C/C++ Operators 185
3.18.2 Further Notes on C/C++ Operators 204
3.19 Summary 205
3.20 Questions & Exercises 206
4 Delving into the Object-Oriented Programming 208
4.1 Basic Rules and Philosophy of the Object-Oriented Design and Programming 208
4.2 Anatomy of a Class 211
4.2.1 Naming Conventions and Self-Explaining Code 213
4.3 Class Members Access Rules 214
4.4 OPERATOR OVERLOADING – TComplex Class Example 215
4.4.1 Definition of the TComplex Class 216
4.4.2 Definition of the TComplex Class Members 221
4.4.3 Test Functions for the TComplex Class 223
4.5 Mastering Class Members – TheCube Class Example 225
4.5.1 Automatic vs. explicit definition of the constructors 235
4.5.2 TheCube Object Layout and Semantics 235
4.5.3 Shallow vs. Deep Copy Semantics 237
4.5.4 Move Constructor and Move Assignment Semantics 237
4.5.5 Implementation of the TheCube Streaming Operators 239
4.5.6 Validation of TheCube 240
4.6 EXAMPLE PROJECT – Moving EMatrix to the Class 243
4.6.1 Definition of the EMatrix Class 243
4.6.2 Implementation of the Class Streaming Operators 245
4.6.3 Implementation of the Arithmetic Operators 248
4.6.4 Testing Matrix Operations 250
4.7 Introduction to Templates and Generic Programming 252
4.7.1 (*) Template Specializations 256
4.8 Designing Template Functions and Classes – TStack Example 257
4.8.1 Design and Implementation of the TStackFor Class 258
4.8.2 Testing TStack 261
4.9 Template Functions and Template Members Function 262
4.10 Class Relations – “Know”, “Has-A”, “Is-A” Concepts 265
4.11 Extending Functionality through Class Inheritance – TComplexQuadEq Example 272
4.12 Virtual Functions and Polymorphism 277
4.13 (*) More on Virtual Mechanism 283
4.14 (*) The Curiously Recurring Template Pattern and the Static Polymorphism 285
4.15 EXAMPLE PROJECT – TLongNumberFor Class for Efficient Storage of Numbers of Any Length 289
4.15.1 Binary Coded Decimal Representation 290
4.15.2 Endianness 291
4.15.3 Definition of the TLongNumberFor Class 292
4.15.3.1 Type Converting Operations 294
4.15.3.2 TLongNumberFor Test Function 298
4.15.4 Designing Classes for PESEL ID 299
4.15.4.1 Aggregating PESEL 300
4.15.4.2 Inherited PESEL 302
4.15.4.3 LongNumber Project Organization 303
4.15.5 (*) Extending Functionality of the TLongNumberFor with the Proxy Pattern 304
4.15.5.1 Definition of the Proxy Class 305
4.15.5.2 Testing Functionality of the TLongNumberFor Class with the Proxy Pattern 307
4.16 Strong Types 309
4.17 Summary 310
4.18 Questions & Exercises 310
5 Memory Management 312
5.1 Data Storage Types 313
5.2 Dynamic Memory Allocations – How to Avoid Memory Leaks 318
5.2.1 Introduction to Smart Pointers and Resource Management 320
5.2.1.1 RAII and stack unwinding 321
5.3 Smart Pointers – An Overview with Examples 322
5.3.1 (*) More on the std::unique_ptr 328
5.3.1.1 Context of Using the std::unique_ptr 328
5.3.1.2 Factory Method Design Pattern 331
5.3.1.3 Custom Deleters for the unique_ptr 333
5.3.1.4 Constructions to Avoid when Using unique_ptr 334
5.3.2 (*) More on shared_ptr and weak_ptr 335
5.4 Summary 337
5.5 Questions & Exercises 337
6 Advancing the Object-Oriented Programming 339
6.1 Functional Objects 339
6.2 EXAMPLE PROJECT – Extending Currency Search in XML Files – Learning State Machine and Regular Expressions with regex Library 344
6.2.1 Pattern Matching with Regular Expression Library 345
6.2.2 State Machine Pattern 347
6.2.3 Implementation of the Extended Class 348
6.2.4 Project Extension – Loading Currency Information from the Internet 353
6.2.5 Launching the Extended Version of CurrencyCalc 359
6.2.6 Building a Static Library and a Terminal Window Application 362
6.2.7 C++ Filesystem 364
6.2.8 User Interface 371
6.2.8.1 Definition of the CC_GUI class 372
6.2.8.2 Definitions of Members of the CC_GUI Class and the Callback Mechanism 375
6.2.8.3 Launching the GUI Based Application 381
6.3 System Clocks and Time Measurements 383
6.4 (*) Time Measurements of a Function Execution 386
6.5 Range Class 388
6.5.1 Functional Programming & the Ranges Library 393
6.6 EXAMPLE PROJECT – Parsing Expressions 393
6.6.1 Defining Language Expressions with Formal Grammar Rules 394
6.6.2 Design of the Expression Processing Framework 396
6.6.3 The First Expression Interpreter 397
6.6.4 Building the Syntax Tree with the Composite Design Pattern 401
6.6.4.1 The Composite Design Pattern to Define Nodes of a Tree 402
6.6.4.2 Implementation of TNode Hierarchy and Cooperation with the Visitors 403
6.6.4.3 Implementation of the ValueLeafNode Class 405
6.6.4.4 Implementation of the BinOperator Class 406
6.6.4.5 Implementation of the PlusOperator Class 407
6.6.4.6 Deep Copy of the Node Objects – the Prototyping Mechanism 408
6.6.5 Syntax Tree Building Interpreter 409
6.6.6 Stack for Smart Pointers 414
6.6.7 Traversing the Trees with the Visitor Design Pattern 417
6.6.7.1 The Expression Evaluating Visitor 419
6.6.7.2 The Expression Printing Visitor 421
6.6.8 Testing the Interpreters 423
6.6.9 Representing Expressions on a Stack in Reversed Polish Notation 426
6.6.9.1 Reversed Polish Notation 426
6.6.9.2 Algorithm for Evaluating the RPN Expression 427
6.7 Summary 432
6.8 Questions & Exercises 433
7 Computer Arithmetic 435
7.1 Integer Value Representation 435
7.1.1 Base conversion algorithm 436
7.1.2 Hexadecimal and Octal Representations 437
7.1.3 Binary Addition 438
7.1.4 Negative Values and Subtraction Operation 439
7.1.5 Arithmetic Control Flags 441
7.1.6 Fractional Representation 443
7.2 Binary Shift Operations 446
7.3 (*) EXAMPLE PROJECT – Software Model for the Fixed-Point Representation 448
7.3.1 Fixed-Point Numbers and Their Arithmetic 448
7.3.2 Definition of the FxFor Class 448
7.3.3 Selected Methods of the FxFor Class 455
7.3.4 Applications of the FxFor 460
7.4 Floating Point Representation 462
7.4.1 Floating-Point Format for Number Representation and Basic Properties 463
7.4.2 Distribution of the Floating-Point Numbers and Its Computational Consequences 467
7.4.3 (*) Real-Value Approximation Error with Floating-Point Representation 470
7.4.4 The IEEE 754 Standard for Floating-Point Arithmetic 472
7.4.5 The Standard FP Operation Model 478
7.4.6 Computations Conscious of Numerical Errors 478
7.4.7 EXAMPLE PROJECT – Evaluation of the Summation Algorithms 480
7.4.8 EXAMPLE PROJECT – The Newton Method of Root Finding of a Function 484
7.4.8.1 Computing Square Root Function Based on Newton Iterations 488
7.5 Summary 490
7.6 Questions & Excercises 490
8 Basics of Parallel Programming 493
8.1 Adding Parallelism to the Standard Algorithms 496
8.2 Launching the Asynchronous Tasks 498
8.3 Parallelization with the Open MP Library 500
8.3.1 Launching a Pool of Threads and Exclusive Access Protection 501
8.3.2 Loop Parallelization and Reduction Operation 503
8.3.3 Massive Data Parallelization 505
8.4 Summary 512
8.5 Questions & Exercises 512
9 Appendix 514
9.1 Preprocessor Directives 514
9.2 Short Introduction to C 517
9.2.1 Built-in Arrays 518
9.2.1.1 Multi-dimensional Fixed-Size Arrays 520
9.2.2 Passing Arrays to Functions – main Explained 521
9.2.3 C structures 525
9.2.4 C functions and input/output 525
9.2.5 Unions 527
9.2.6 Memory and String Operations 528
9.2.7 Binding C and C++ code 532
9.3 Linking and Binary Organization of the C/C++ Objects 532
9.4 Graphical User and Web Interfaces for the C++ Projects 534
9.5 Converting Bin-Oct-Dec-Hex Values with FixBinCalc 536
9.6 Programming Toolchain 536
9.6.1 Project Generation Tool (CMake) 536
9.6.2 Source Version Control and Repositories 540
9.6.3 Profiler 541
9.7 Software Unit Testing 543
9.8 Summary 548
9.9 Questions & Exercises 548
Bibliografia 549
Index 552
Prof. Boguslaw Cyganek, Department of Electronics, AGH, University of Science and Technology, Krakow, Poland. Boguslaw Cyganek obtained his Ph.D. degree cum laude in 2001 with a thesis on correlation of stereo images, and D.Sc. degree in 2011 with a thesis on methods and algorithms of object recognition in digital images. During recent years, Dr. Cyganek has been cooperating with many scientific centers in development of computer vision systems. He has also gained several years of practical experience working as a Software Development Manager and a Senior Software Engineer both in the USA and Poland. He is an author or a co-author of over ninety conference and journal papers and four books including “An Introduction to 3D Computer Vision Techniques and Algorithms”, as well as “Object Detection and Recognition in Digital Images: Theory and Practice”, published by Wiley. Dr. Cyganek is a member of the IEEE, SPIE, IAPR and SIAM.
What makes us different?
• Instant Download
• Always Competitive Pricing
• 100% Privacy
• FREE Sample Available
• 24-7 LIVE Customer Support