Chapter 1 Introduction 1
1.1 Overview of Programming 1
1.1.1 What Is Programming? 1
1.1.2 How Do We Write a Program? 3
1.2 The Evolution of Programming Language 5
1.2.1 Assembly and Machine Languages 5
1.2.2 Early Languages 6
1.2.3 Later-Generation Languages 7
1.2.4 Modern Languages 7
1.3 Programming Methodologies 8
1.3.1 Structured Programming 8
1.3.2 Object-Oriented Programming 10
1.4 Object-Oriented Programming 12
1.5 C++Programming Language 15
1.5.1 History of C and C++ 15
1.5.2 Learning C++ 16
Word Tips 17
Exercises 18
Chapter 2 Basic Facilities 19
2.1 C++ Program Structure 19
2.2 Input/Output Streams 21
2.3 Constant 22
2.4 Functions 24
2.4.1 Function Declarations 24
2.4.2 Function Definitions 25
2.4.3 Default Parameters 26
2.4.4 Inline Functions 28
2.4.5 Overloaded Functions 29
2.5 References 33
2.5.1 Reference Definition 33
2.5.2 Reference Variables as Parameters 37
2.5.3 References as Value-Returning 38
2.5.4 References as Left-Hand Values 40
2.6 Namespaces 41
Word Tips 45
Exercises 45
Chapter 3 Classes and Objects(Ⅰ) 49
3.1 Structures 49
3.1.1 Defining a Structure 49
3.1.2 Accessing Members of Structures 50
3.1.3 Structures with Member Functions 52
3.2 Data Abstraction and Classes 53
3.2.1 Data Abstraction 53
3.2.2 Defining Classes 54
3.2.3 Defining Objects 55
3.2.4 Using Member Functions 55
3.2.5 In-Class Member Function Definition 58
3.2.6 File Structure of an Abstract Data Type 59
3.3 Information Hiding 62
3.4 Access Control 63
3.5 Constructors 65
3.5.1 Overloading Constructors 66
3.5.2 Constructors with Default Parameters 67
3.6 Destructors 69
3.6.1 Definition of Destructors 69
3.6.2 Order of Constructor and Destructor Calls 70
Word Tips 73
Exercises 74
Chapter 4 Classes and Objects(Ⅱ) 77
4.1 Constant Members 77
4.2 this Pointers 78
4.3 Static Members 79
4.4 Free Store 84
4.5 Objects as Members of A Class 89
4.6 Copy Members 94
4.6.1 Definition of Copy Constructors 94
4.6.2 Shallow Copy and Deep Copy 97
4.7 Arrays of Objects 106
4.8 Friends 110
4.8.1 Friend Functions 110
4.8.2 Friend Classes 113
4.9 Examples of User-Defined Types 114
Word Tips 120
Exercises 121
Chapter 5 Operator Overloading 125
5.1 Why Operator Overloading Is Need 125
5.2 Operator Functions 126
5.2.1 Overloaded Operators 126
5.2.2 Operator Functions 126
5.3 Binary and Unary Operators 130
5.3.1 Overloading Binary Operators 130
5.3.2 Overloading Unary Operators 131
5.4 Overloading Combinatorial Operators 135
5.5 Mixed Arithmetic of User-Defined Types 139
5.6 Type Conversion of User-Defined Types 139
5.7 Examples of Operator Overloading 141
5.7.1 A Complex Number Class 141
5.7.2 A String Class 149
Word Tips 155
Exercises 156
Chapter 6 Inheritance 158
6.1 Class Hierarchies 158
6.2 Derived Classes 159
6.2.1 Declaration of Derived Classes 159
6.2.2 Structure of Derived Classes 160
6.3 Constructors and Destructors of Derived Classes 163
6.3.1 Constructors of Derived Classes 163
6.3.2 Destructors of Derived Classes 166
6.3.3 Order of Calling Class Objects 167
6.3.4 Inheritance and Composition 170
6.4 Member Functions of Derived Classes 170
6.5 Access Control 173
6.5.1 Access Control in A Class 173
6.5.2 Access to Base Classes 175
6.6 Multiple Inheritance 179
6.6.1 Declaration of Multiple Inheritance 180
6.6.2 Constructors of Multiple Inheritance 182
6.7 Virtual Inheritance 183
6.7.1 Multiple Inheritance Ambiguities 183
6.7.2 Trying to Solve Inheritance Ambiguities 184
6.7.3 Virtual Base C1asses 187
6.7.4 Constructing Objects of Multiple Inheritance 189
Word Tips 191
Exercises 192
Chapter 7 Polymorphism and Virtual Functions 202
7.1 Polymorphism 202
7.1.1 Concept of Polymorphism 202
7.1.2 Binding 203
7.2 Virtual Functions 205
7.2.1 Definition of Vitual Functions 206
7.2.2 Extensibilitv 209
7.2.3 Principle of Virtual Functions 212
7.2.4 Virtual Destructors 213
7.2.5 Function Overloading and Function Overriding 214
7.3 Abstract Base C1asses 217
Word Tips 220
Exercises 220
Chapter 8 Templates 226
8.1 Templates Mechanism 226
8.2 Function Templates and Template Functions 227
8.2.1 Why We Use Function Templates? 227
8.2.2 Definition of Function Templates 228
8.2.3 Function Template Instantiation 229
8.2.4 Function Template with Different Parameter Types 231
8.2.5 Function Template Overloading 232
8.3 Class Templates and Template Classes 233
8.3.1 Definition of Class Templates 233
8.3.2 Class Template Instantiation 236
8.4 Non-Type Parameters for Templates 239
8.5 Derivation and Class Templates 240
Word Tips 242
Exercises 242
References 244