Cybernetics in C++

C++ is a powerful, much sought after programming language, but can be daunting to work with, even for engineering professionals. Why is this book so useful? Have you ever wondered: • How do keywords like static and virtual change their meanings according to context? • What are the similarities and d...

Ausführliche Beschreibung

Gespeichert in:
Bibliographische Detailangaben
1. Verfasser: Bhattacharyya, Avi
Format: E-Book
Sprache:Englisch
Veröffentlicht: River Publishers 2020
Schriftenreihe:River publishers series in software engineering
Schlagworte:
ISBN:9788793609457, 8793609450
Online-Zugang:Volltext
Tags: Tag hinzufügen
Keine Tags, Fügen Sie den ersten Tag hinzu!
Inhaltsangabe:
  • PREFACE xxix DEDICATION xxxi LIST OF FIGURES xxxiii LIST OF PROGRAMS xxxv INTRODUCTION 1 PART I – IMPERATIVE PROGRAMMING (1) 7 1. Getting Started 8 1.1 Setting Up the Programming Environment 8 1.1.1 Software IDE 9 1.2 Hello World 9 1.3 Writing C++ Source Code 12 1.3.1 Comments 12 1.3.1.1 Style with Primitive Tools 13 1.3.2 Source Code Files 13 1.4 Subsequent Stages in Building a Program 14 1.4.1 Compilation 14 1.4.1.1 Object Files 14 1.4.2 Linking 15 1.4.3 Running 15 1.5 Debugging (1) 15 1.5.1 What are Bugs/Errors? 15 1.5.2 Compile-Time Errors 16 1.5.2.1 Compiler Errors 16 1.5.2.2 Compiler Warning 16 1.5.3 Runtime Errors 17 2. Data Types & Operators 17 2.1 Learning by Example 17 2.1.1 Performing Calculations 17 2.1.2 Interacting with the Console Window 18 2.2 Variables 20 2.2.1 Naming Variables 20 2.2.1.1 Rules 20 2.2.1.2 Guidelines 21 2.2.2 Initialising a Variable 21 2.2.2.1 Using Assignment Equals = 22 2.2.2.2 Using Parentheses () 22 2.2.2.3 Which is Preferable? 22 2.2.3 Ensuring Read Only Status Using Const 22 2.3 Primitive Data Types 23 2.3.1 Type Casting 24 2.3.1.1 Prospective Problems with certain types 25 2.3.1.2 Static Casting of Types 25 2.3.1.3 Solution with Type Casting 25 2.3.1.4 C Style Casts 27 2.4 Operators 27 2.4.1 Mathematical Operators 27 2.4.1.1 Arithmetic Operators 27 2.4.1.2 Assignment & Equality Operators 27 2.4.1.2.1 Assignment Operations using = 28 2.4.1.2.2 = Operator in Mathematics and Computer Science 28 2.4.1.2.3 Equality Operations using == 29 2.4.1.2.4 Compound Assignment Operators 29 2.4.1.2.5 Assignment vs Compound Assignment Operators 30 2.4.1.3 Increment & Decrement Operators 30 2.4.1.3.1 Post Increment 31 2.4.1.3.2 Pre Increment 31 2.4.1.3.3 Similarities & Differences: Pre Increment & Post Increment 31 2.4.1.4 Bitwise Operators 34 2.4.2 Logical Operators 35 2.4.3 The Sizeof Operator 35 3. Control Statements 37 3.1 Conditional Statements 37 3.1.1 If Statements 37 3.1.1.1 What is a condition? 38 3.1.2 The if, Then, Else Clause 39 3.1.2.1 Alternative Form using the Ternary? : Operator 41 3.1.3 Cascaded/Nested if Statements 43 3.1.4 The if, Else if, Else Clause 45 3.1.5 Switch Statement 47 3.1.6 Switch Statement vs if, Else if, Else Clause 50 3.2 Problem Solving with Loops 50 3.2.1 What are Loops? 50 3.2.2 For Loop 51 3.2.3 While Loop 54 3.2.3.1 Empty While Loop 56 3.2.4 Do Loop/While Loop 56 3.3 C++ Statements & Constructs 61 3.3.1 Blocks 61 3.3.2 Break Statement 61 3.3.3 Continue Statement 62 3.3.4 The Goto Statement 64 3.3.4.1 Prospective Evils of the goto statement 66 3.3.5 Dropping the Braces fg For 1 Action 66 3.3.5.1 Potential Hazards in dropping the Braces 67 3.3.5.1.1 Breaking at the Wrong Place 67 3.3.5.1.2 Dangling Else Clause Issue 68 3.3.5.2 Guideline with Braces 69 3.4 Creating Aliases IN C & C++ 69 3.4.1 Defining Constants Using #Define - Avoid in C++ 69 3.4.1.1 Issues with Macro Aliases 70 3.4.2 Typedef 70 3.4.2.1 Cannot modify Typedef 70 4. Programming Practice 71 4.1 Good vs Bad Practices 71 4.2 Guidelines 74 4.2.1 Organisation 74 4.2.2 Naming of Variables 74 4.2.3 Formatting of Source Code 75 4.3 Debugging 75 4.3.1 Breakpoints 75 4.3.2 Viewing Variables 76 4.3.3 Single Stepping 77 PART II – PROCEDURAL PROGRAMMING 79 5. Functions 80 5.1 Procedural Thinking 80 5.1.1 Procedural Thinking Outside the Programming World 80 5.1.2 Procedural Thinking Inside the Programming World 81 5.2 What are Functions? 81 5.2.1 Function Naming 82 5.2.2 Function Definitions 83 5.2.3 Function Declarations 84 5.2.3.1 Signature of a Function (1) 84 5.2.4 Function Invocation 85 5.2.4.1 Caution: Circular Reference 85 5.2.5 Declarations vs Definitions 86 5.3 Functions: No Inputs, No Output 86 5.4 Functions: Some Inputs, No Output 89 5.4.1 Signature of a Function (2) 92 5.4.2 Formal Parameters vs. Actual Parameters 93 5.4.3 Passing a Copy of the Value 93 5.4.4 No Predetermined Order of Evaluation of Actual Parameters 93 5.4.5 Modifying Formal Parameters 94 5.4.5.1 Default Values 94 5.4.5.2 Const Formal Parameters 97 5.5 Functions: Some Inputs, an output 98 5.5.1 Returning a Value 101 5.6 The Main Function 101 5.7 Scope of a Variable 102 5.7.1 Global Variables 104 5.7.1.1 Prospective Evils of Global Variables 104 5.7.2 Local Variables 105 5.7.2.1 Automatic Local Variables 105 5.7.2.2 Static Local Variables 106 5.7.2.2.1 Initialising Static Local Variables 108 5.7.2.2.2 Caution with Static Local Variables 108 5.8 Debugging: Single Step In 108 5.8.1 The Debug/Call Stack 110 5.9 Overloading Functions 110 5.9.1 Prospective Evils of Function Overloading 113 6. Inline Functions and Macros 113 6.1 Function Invocation Overhead 113 6.2 C Macros - For the Best Part Avoid in C++ 114 6.2.1 Creating a Macro 114 6.2.2 Conditional Preprocessor Directives 116 6.2.2.1 The #ifdef, #ifndef #endif Clause 116 6.2.3 Prospective Evils of Macros 118 6.2.3.1 Losing Track of Types 118 6.2.3.2 Losing Track of the Name 119 6.2.3.3 Operator Precedence Problems 119 6.2.4 Rules of Thumb with Macros 120 6.3 Inline Functions 120 6.3.1 Defining & Declaring Inline Functions 120 6.3.2 Using Inline Functions 121 6.3.3 The Inline Keyword: A Request, Not an Instruction 122 6.3.4 Macro Problems not Manifest in Inline Functions 122 7. Program Management Over Multiple Files 123 7.1 Software Projects 123 7.1.1 Source Code 124 7.2 Building C++ Projects 127 7.2.1 Compilation 127 7.2.2 Linking 128 7.3 Controlling Visibility 128 7.3.1 Creating External Visibility Using Extern 128 7.3.1.1 Linker Error caused by a Lack of Definition 129 7.3.2 Hiding External Visibility Using Static 130 7.3.2.1 Duplication of the keyword static 132 7.4 Namespaces 132 7.5 Potential Problem: Multiple Inclusions 134 7.6 Preventing Multiple Inclusions 135 7.6.1 Ensuring Single Inclusions Using Macros 135 7.6.1.1 Using #ifndef to prevent Multiple Inclusions 135 7.6.2 Ensuring Single Inclusions Using Preprocessor Directives 139 8. Recursion 142 8.1 What is Recursion? 142 8.1.1 Recursion Outside the Programming World 142 8.1.2 Recursion Inside the Programming World 146 8.2 Caution with Recursion 146 8.3 Computing Factorials 147 8.3.1 Iterative Algorthm: Using Loops 147 8.3.2 Using Recursion 149 8.4 Fibonacci Sequence 151 8.4.1 Applications of the Fibonacci Sequence 154 8.5 Generating Permutations 154 8.5.1 Recursive 155 8.5.2 The Next Permutation Algorithm 155 8.6 Hanoi Towers 157 8.6.1 Strategy for 3 Rings 158 8.6.2 Strategy for 4 Rings 158 8.6.3 Iterative Strategies 159 8.6.4 Recursive Strategies 159 8.6.5 Implementing Recursion in Hanoi Towers 160 8.6.6 Traditional Legends of Hanoi Towers 163 8.7 Further Recursive Problems 164 8.7.1 Triangular Numbers 164 8.7.2 Knapsack/Rucksack Problem 165 8.7.3 N Queens Problem 165 PART III – DATA STRUCTURES & ALGORITHMS 167 9. Arrays 167 9.1 What are Arrays? 167 9.1.1 Homogeneous Nature of Data Structures 168 9.1.2 Indexing in Arrays 168 9.1.3 The Array Operator 169 9.1.4 Storage in Runtime Memory 169 9.2 Defining Arrays 169 9.3 Writing Data to an Array 170 9.4 Defining and Initialising Arrays 170 9.4.1 Defining and Initialising Separately 170 9.4.1.1 Stand Alone Definition 170 9.4.1.2 Initialisation 170 9.4.2 Defining and Initialising on the same Line 171 9.5 Reading Data from an Array 171 9.6 Using Arrays 171 9.7 Arrays as Function Inputs 173 9.8 Issues with Arrays 175 9.9 Multidimensional Arrays 175 9.10 Dynamic Arrays 177 PART IV – MEMORY & RESOURCE MANAGEMENT PROGRAMMING 179 10. Pointers 181 10.1 What is the Practice of Addressing? 181 10.1.1 Addressing Outside the Programming World 181 10.1.2 Addressing Inside the Programming World 182 10.2 What are Pointers? 182 10.2.1 L-Value vs R-Value 183 10.2.2 The Importance of Pointers 183 10.3 Implementing Pointers in C & C++ 184 10.3.1 Defining Pointers: The * Operator 184 10.3.1.1 Placing the Asterisk BEFORE the Space 185 10.3.1.2 Placing the Asterisk AFTER the Space 185 10.3.2 Populating Pointers: The & Operator 186 10.3.2.1 Definition and Population in the same line 187 10.3.3 Decoding Pointers: The * Operator 187 10.3.4 Demonstration: Creating & Using Pointers 187 10.3.5 Potential Problem: Type Mismatch 189 10.3.6 Equating Pointers 189 10.3.7 Review: 2 Uses of the Asterisk Operator 191 10.4 The NULL Pointer 192 10.4.1 What is the NULL Pointer? 192 10.4.1.1 Do not Confuse NULL Pointers and NUL Characters 192 10.4.2 Defining the NULL Pointer 192 10.4.3 Applications of the NULL Pointer 193 10.5 The Void Pointer 193 10.5.1 Issues with the Void Pointer 194 10.6 Pointers as Function Inputs 195 10.6.1 Problems: Lack of Addressing 195 10.6.2 Passing Function Inputs as Pointers 196 10.6.2.1 Formal Parameters as Pointers 196 10.6.2.2 Actual Parameters as Pointers 197 10.6.3 Fixing the Problems Using Pointer Inputs 197 10.6.4 The Importance of Pointer Inputs 199 10.7 The Pointer as the Function Output 199 10.7.1 Prospective Dangling Pointer Problem 200 11. Advanced Pointers 200 11.1 Pointer to Pointer Hierarchies 200 11.1.1 One Level: Pointer to a Pointer 200 11.1.2 Multiple Level Pointers 202 11.2 Read-Only Pointers Using Const 204 11.3 Pointers to Functions 206 11.4 Arrays of Pointers 206 11.4.1 Array of Pointers to Primitive Variables 207 11.4.2 Array of Pointers to Functions 208 12. References 209 12.1 What are References? 209 12.1.1 L-Value vs R-Value 209 12.1.2 Restrictions with References 209 12.2 Implementing References in C++ 210 12.2.1 Defining References 210 12.2.2 Populating References 210 12.2.3 Decoding References 211 12.3 Using References 212 12.4 References as Function Inputs 213 12.4.1 Problems: Lack of Addressing 213 12.4.2 Passing Function Inputs as References 214 12.4.2.1 Formal Parameters as References 214 12.4.2.2 Actual Parameters as References 215 12.4.3 Fixing the Problems Using References 215 13. Advanced References 217 13.1 Read-Only References Using Const 217 13.2 Potential Ambiguities in Reference Notation 219 13.3 Reference to a Function 219 14. Runtime Memory Management 219 14.1 Garbage Collection 219 14.2 The Runtime Stack 220 14.2.1 LIFO: Last in Firs