內容簡介
本書由在電腦程式設計方面有著豐富教學和實踐經驗的中外作者合作編寫。本書內容共分14章,由淺入深、全面介紹C++程式設計方法。本書通俗易懂,例子貼近生活,尤其強調讀者的親自參與意識。所有實例經過精心挑選。每章都為初學者提供了常見錯誤分析,每章結尾有很多有趣的習題,可以提高讀者上機編程的興趣。本書是國內首次出版的中英文對照混排式雙語版C++程式設計教材的更新版,既方便初學者熟悉相關概念和內容,也便於英文非母語的讀者熟悉英文專業辭彙。
圖書目錄
Chapter One Typographic Conventions(緒論) 1
1.1 What is a computer program (什麼是電腦程式 ) 1
1.2 Developing a computer program(開發電腦程式)2
1.2.1 Program development cycle2
1.3 Learning C++(學習 C++) 4
1.4 Web site for this book(本書的網站) 4
1.5 Brief history of C++(C++簡史)4
1.6 ANSI/ISO C++ standard(ANSI/ISO C++標準)5
Chapter Two Beginning to Program in C++(C++編程入門) 6
2.1 Constants(常量) 6
2.2 Variables(變數) 6
2.3 Simple output to the screen(簡單的螢幕輸出) 7
2.4 Comments(注釋) 9
2.5 Data types(數據類型) 10
2.5.1 Short integer data types 10
2.5.2 Long integer data types 10
2.5.3 Boolean data types 11
2.5.4 Double floating-point data types 11
2.5.5 Unsigned integer data types 11
2.6 Data type sizes(數據類型的大小) 11
2.7 Operators (運算符) 12
2.7.1 The assignment operator 12
2.7.2 Arithmetic operators 12
2.7.3 Increment and decrement operators 13
2.7.4 Combined assignment operators 15
2.8 Operator precedence(運算符的優先權) 16
2.9 Data type conversions and casts(類型轉換和強轉) 18
Programming pitfalls 20
Quick syntax reference 21
Exercises 22
Chapter Three Keyboard Input and Screen Output(鍵盤輸入和螢幕輸出) 26
3.1 Simple keyboard input(簡單的鍵盤輸入) 26
3.2 Manipulators(流操縱符) 28
3.3 Single-character input and output(單個字元的輸入和輸出) 30
Programming pitfalls 32
Quick syntax reference 32
Exercises 32
Chapter Four Selection and Iteration(選擇與循環) 34
4.1 Selection(選擇) 34
4.1.1 The if statement 34
4.1.2 The if-else statement 35
4.1.3 Compound statements 35
4.1.4 Logical operators 37
4.1.5 Nested if statements 37
4.1.6 The switch statement 37
4.1.7 The conditional operator : 39
4.2 Iteration(循環) 40
4.2.1 The while statement 40
4.2.2 The do-while loop 42
4.2.3 The for statement 43
4.2.4 Nested loops 45
Programming pitfalls 47
Quick syntax reference 49
Exercises 50
Chapter Five Arrays and Structures(數組和結構體) 53
5.1 Arrays(數組) 53
5.1.1 Introduction 53
5.1.2 Initialising an array 56
5.1.3 Two-dimensional arrays 57
5.1.4 Initialising a two-dimensional array 59
5.1.5 Multi-dimensional arrays 60
5.2 Structures(結構體) 60
5.2.1 Introduction 60
5.2.2 Declaring a structure 61
5.2.3 Initialising a structure variable 63
5.2.4 Nested structures 64
5.3 The typedef statement(typedef 語句) 65
5.4 Arrays of structures(結構體數組) 66
5.5 Enumerated data types(枚舉數據類型) 66
Programming pitfalls 68
Quick syntax reference 68
Exercises 69
Chapter Six Strings(字元串) 72
6.1 C-strings(C 風格字元串) 72
6.2 C-string input and output(C 風格字元串的輸入和輸出) 73
6.3 Accessing individual characters of a C-string(訪問C 風格字元串中的單個字元) 77
6.4 C-string functions(C 風格字元串函式) 77
6.4.1 Finding the length of a C-string 78
6.4.2 Copying a C-string 78
6.4.3 C-string concatenation 79
6.4.4 Comparing C-strings 79
6.4.5 Other C-string functions 79
6.4.6 Converting numeric C-strings to numbers 80
6.5 C++ strings(C++ 字元串) 80
6.5.1 string initialisation and assignment 82
6.5.2 string concatenation 84
6.5.3 string length, string indexing and sub-strings 85
6.5.4 string replace, erase, insert and empty strings 86
6.5.5 string searching 88
6.5.6 string comparisons 89
6.5.7 string input 91
6.5.8 string conversions 92
6.6 Arrays of strings(string 類型的數組) 93
6.7 Character classification(字元分類) 94
Programming pitfalls 96
Quick syntax reference 96
Exercises 97
Chapter Seven Functions(函式) 100
7.1 Introduction(引言) 100
7.2 Function arguments(函式實參) 102
7.3 Default parameter values(默認的形參值) 105
7.4 Returning a value from a function(從函式返回一個值) 106
7.5 Inline functions(內聯函式) 107
7.6 Passing arguments by value(按值傳遞實參) 108
7.7 Passing arguments by reference(按引用傳遞實參) 109
7.8 Passing a one-dimensional array to a function(向函式傳遞一維數組) 112
7.9 Passing a multi-dimensional array to a function(向函式傳遞多維數組) 115
7.10 Passing a structure variable to a function(向函式傳遞結構體變數) 116
7.11 Passing a string to function(向函式傳遞字元串) 118
7.11.1 Passing a C++ string to a function 118
7.11.2 Passing a C-string to a function 119
7.12 Recursion(遞歸) 120
7.13 Function overloading(函式重載) 122
7.14 Storage classes auto and static (auto 和static 存儲類型) 123
7.14.1 auto 123
7.14.2 static 124
7.15 The scope of a variable(變數的作用域) 125
7.15.1 Block scope 125
7.15.2 Global scope 126
7.15.3 Reusing a variable name 127
7.16 Mathematical functions(數學函式) 129
7.16.1 Some trigonometric functions 129
7.16.2 Pseudo-random number functions 130
Programming pitfalls 132
Quick syntax reference 132
Exercises 133
Chapter Eight Objects and Classes(對象和類) 137
8.1 What is an object (什麼是對象 ) 137
8.2 What is a class (什麼是類 ) 137
8.3 Further examples of classes and objects(類和對象的更進一步的示例) 140
8.3.1 A student class 140
8.3.2 A bank account class 140
8.4 Abstraction(抽象) 141
8.5 Constructing a class in C++(用C++構造一個類) 142
8.6 Using a class: defining and using objects(使用類:定義和使用對象) 144
8.7 Abstract data types(抽象數據類型) 145
8.8 Constructors(構造函式) 146
8.9 Default class constructor(默認的類構造函式) 148
8.10 Overloading class constructors(重載類構造函式) 149
8.11 Constructor initialisation lists(構造函式初始化列表) 151
8.12 Default argument values in a constructor(構造函式中的默認實參值) 152
8.13 static class data members(靜態類數據成員) 154
8.14 Using return in a member function(在成員函式中使用return) 157
8.15 Inline class member functions(內聯成員函式) 159
8.16 Class interface and class implementation(類的接口和類的實現) 160
8.16.1 Separation of class interface and class implementation 162
8.16.2 Use of namespaces in header files 164
Programming pitfalls 167
Quick syntax reference 167
Exercises 167
Chapter Nine Pointers and Dynamic Memory(指針和動態記憶體分配) 171
9.1 Variable addresses(變數的地址) 171
9.2 Pointer variables(指針變數) 172
9.3 The dereference operator *(解引用運算符*) 173
9.4 Using const with pointers(使用const 修飾指針變數) 174
9.5 Pointers and one-dimensional arrays(指針和一維數組) 175
9.6 Pointers and multi-dimensional arrays(指針和多維數組) 177
9.7 Pointers to structures(指向結構體的指針) 178
9.8 Pointers to class objects(指向類對象的指針) 179
9.9 Pointers as function arguments(指針變數作為函式實參) 180
9.10 Dynamic memory allocation(動態記憶體分配) 182
9.10.1 Allocating memory dynamically for an array 183
9.10.2 Initialisation with new 184
9.10.3 Allocating memory for multi-dimensional arrays 186
9.10.4 Out of memory error 187
Programming pitfalls 189
Quick syntax reference 190
Exercises 190
Chapter Ten Operator Overloading(運算符重載) 193
10.1 The need for operator overloading(運算符重載的必要性) 193
10.2 Overloading the addition operator +(重載加法運算符+) 193
10.3 Rules of operator overloading(運算符重載的規則) 200
10.4 Overloading ++(重載運算符++) 200
10.4.1 Overloading prefix and postfix forms of ++ 203
10.4.2 Improving the prefix ++ operator member function 206
10.5 Overloading relational operators(重載關係運算符) 206
10.6 Overloading < and >> (重載運算符<>) 209
10.7 Conversion operators(轉換運算符) 214
10.8 Use of friend functions(使用友元函式) 217
10.9 Overloading the assignment operator =(重載賦值運算符=) 218
10.9.1 A class with a pointer data member 218
10.9.2 Assigning one object to another 220
10.10 The copy constructor(拷貝構造函式) 229
10.11 Overloading the index operator [ ](重載下標運算符[ ]) 233
Programming pitfalls 236
Quick syntax reference 237
Exercises 237
Chapter Eleven Inheritance(繼承) 240
11.1 What is inheritance (什麼是繼承 ) 240
11.2 Inheritance syntax(繼承語法) 241
11.3 Passing arguments to a base class constructor(向基類的構造函式傳遞實參) 249
11.4 Protected class members(受保護的類成員) 253
11.5 Types of inheritance: public, protected and private(繼承的類型:public、protected和private) 256
11.6 Composition(組合) 258
11.7 Multiple inheritance(多重繼承) 259
11.8 Virtual base classes(虛基類) 262
Programming pitfalls 265
Quick syntax reference 265
Exercises 266
Chapter Twelve Polymorphism(多態) 271
12.1 What is polymorphism (什麼是多態 ) 271
12.2 Virtual functions(虛函式) 274
12.2.1 When to use virtual functions 279
12.2.2 Overriding and overloading 279
12.3 Abstract base classes(抽象基類) 280
Programming pitfalls 284
Quick syntax reference 284
Exercises 285
Chapter Thirteen Templates(模板) 288
13.1 Introduction(引言) 288
13.2 Function templates(函式模板) 288
13.3 Class templates(類模板) 292
Programming pitfalls 297
Quick syntax reference 297
Exercises 297
Chapter Fourteen Files and Streams(檔案和流) 300
14.1 The C++ input/output class hierarchy(C++ 輸入/ 輸出類的層次結構) 300
14.2 Opening a file(打開檔案) 301
14.3 File error checking(檔案出錯檢查) 303
14.4 Single character I/O and detecting the end of a file(單字元的I/O 和檔案末尾的檢測) 304
14.5 Appending data to the end of a file(向檔案末尾添加數據) 308
14.6 Reading lines from a file(從檔案中讀取行) 309
14.7 Random access(隨機存取) 310
14.8 Object I/O(對象I/O) 313
14.9 Binary I/O(二進制I/O) 314
14.9.1 Serial writing of objects to a binary file 315
14.9.2 Serial reading of objects from a binary file 319
14.9.3 Binary I/O as class member functions 320
14.9.4 Binary file random access 321
Programming pitfalls 325
Quick syntax reference 325
Exercises 326
Appendix A List of C++ Keywords 329
Appendix B Precedence and Associativity of C++ Operators 330
Appendix C ASCII Character Codes 332
Appendix D Fundamental C++ Built-in Data Types 334
Appendix E Common iomanip Manipulators 335
Appendix F Escape Sequences 336
Appendix G The C++ Preprocessor 337