目 录 Part 1 C/C++ Fundamentals Chapter 1 Getting Started with C/C++ 1 1.1 The C and C++ Programming Language 1 1.2 Why to Learn C and C++ 2 1.3 Three Levels of Programming Language 4 1.4 Interpreted Languages and Compiled Languages 6 1.5 The Development Tools of C/C++: Compilers and Linkers 7 1.6 The Development Cycle 7 Chapter Review 9 Chapter 2 Your First C/C++ Program 10 2.1 Your First C++ Programme on Microsoft VC++ 6.0 10 2.1.1 Create a Win32 Console Application Project 10 2.1.2 How does it work? 18 2.2 Your First C++ Program on Codeblocks 20 2.3 Writing a C Program 28 2.4 Similarities and Difference between C and C++ Program 29 Chapter Review 30 Programming Exercises 30 Chapter 3 Variables and Operators 31 3.1 Variables 31 3.2 Variables and identifiers 32 3.3 Data Types 32 3.4 Variables Declaration and Initialization 33 3.5 Variable Names and Comments 35 3.6 Operators 38 3.6.1 Arithmetic Operators 38 3.6.2 Logical Operators 39 3.6.3 Bitwise Operators 40 3.6.4 Relational Operators 41 3.6.5 Operators Precedence in C/C++ 41 3.7 Some Much Used Operators in C / C++ 43 3.7.1 Increment and Decrement Operator 43 3.7.2 sizeof() Operator 44 3.7.3 Modulus (%) Operator 45 3.7.4 Conditional Operator ( ? : ) 46 3.7.5 comma “,” operator 46 Chapter Review 48 Programming Exercises 48 Chapter 4 Decision Making 52 4.1 Conditional Operations 54 4.2 The if Structure 55 4.2.1 The if Statement 56 4.2.2 The if-else Statement 57 4.2.3 Nested if Statements 58 4.2.4 if-else-if Statement 59 4.2.5 switch-case statement 61 Programming Exercises 64 Chapter 5 Loops 66 5.1 for Loop 66 5.2 while Loop 69 5.3 do...while Loop 71 5.4 for Loops vs while Loops 72 5.5 Loop Control Statement “break” and “continue” 73 5.5.1 “Break” Statement 73 5.5.2 “continue” Statement 75 Chapter Review 76 Programming Exercises 77 Part 2 Core Language Features Chapter 6 Arrays 81 6.1 Declaring an Array 82 6.2 Initializing an Array 83 6.3 Accessing Array Elements 83 6.4 Array out of Bounds 85 6.5 Address-of Operator (&) 86 6.6 Pointers in C and C++ 87 6.7 Dynamic Array 89 Programming Exercises 91 Chapter 7 Functions 93 7.1 What is a Function 93 7.2 Library Functions 93 7.3 User-defined Functions 94 7.3.1 Defining a Function 94 7.3.2 Parameters and Arguments 96 7.3.3 Function Declarations 97 7.4 Calling a Function 98 7.4.1 Passing by Value 98 7.4.2 Passing by Address (or Pointers) 99 7.4.3 Passing by Reference 101 7.5 Recursion 102 7.6 Function Overloading 104 7.6.1 Function Overloading by Having Different types of Parameters 104 7.6.2 Function Overloading by Having Different Number of Parameters 105 Programming Exercises 106 Part 3 Object Oriented Programming Chapter 8 Strings 109 8.1 String Assignment 111 8.2 String Comparison 111 8.3 String Concatenation 112 8.4 String Functions 113 8.5 String Operations 114 Chapter Review 116 Programming Exercises 117 Chapter 9 Classes and Objects 118 9.1 Data Encapsulation 118 9.2 Declaring a Class 120 9.3 Defining a Member Function 121 9.3.1 Getter and Setter 121 9.3.2 Implementing Member Functions 122 9.4 Creating an Object 124 9.5 Constructors for Initialization 124 9.6 Destructor 126 9.7 Air ticket example for classes and objects 127 9.8 Friend Function of a Class 131 9.9 Operator Overloading 134 Chapter Review 137 Programming Exercises 138 Chapter 10 Inheritance 140 10.1 Implementing Inheritance 140 10.2 Types of Inheritance 142 10.3 Access Modes in C++ Inheritance 144 10.4 Example for Inheritance 145 10.5 Air Ticket Example for Inheritance 146 10.6 Polymorphism in C++ 151 Chapter Review 153 Programming Exercises 155 Part 4 File Operating Chapter 11 Files and Stream 157 11.1 Types of Files 157 11.2 File Operations in C++ 158 11.2.1 Creating/Opening a File Using Fstream 159 11.2.2 Writing Text Files 160 11.2.3 Reading Text Files 161 11.2.4 String Streams 163 11.2.5 Converting to text 164 11.3 File Operations in C 167 11.3.1 Opening a File 167 11.3.2 Writing a File 169 11.3.3 Closing a File 169 11.3.4 Read from a text file 170 11.3.5 Writing Characters to a File 171 11.3.6 Appending Data to a File 172 Chapter Review 173 Programming Exercises 173 Chapter 12 Splitting Program into Multiple Files 174 12.1 Separate a Program into Multiple Files in C 174 12.2 Practice in Microsoft Visual C++ 6 177 12.3 Separate a Program into Multiple Files in C++ 179 Programming Exercises 185 Part 5 Projects for C/C++ Part A Elementary C/C++ Projects 187 Project 1 Logical Gates 187 Project 2 Lorry Fleet 193 Project 3 Money Class 198 Part B Computer Game and Machine Learning Projects 201 Project 4 Hangman Game Project 201 Project 5 The tic-tac-toe game 205 Project 6 Designing a chatbot 209 Part C Hardware Based Projects 213 Project 7 Blinking a LED and LED chaser 213 Project 8 Distance Measurement using Ultrasonic Sensor and Arduino 218 Project 9 Servo Motor Projects 222 Appendix: Vocabulary for C/C++ 229 References 233 C/C++程序设计(C/C++ Programming) 目录 VI VII