本书在保持前两版特色的基础上,对部分章节内容进行了修改和补充。全书案例易懂、切合实际。本书共8章,围绕面向对象程序设计中类和对象的作用,介绍标准C++中类与对象的定义和封装、继承、重载、多态、模板的概念及实现方法。本书用通俗易懂的英语描述其内容,让初学者了解面向对象程序设计的原文表达;而且在各章节中的重要知识点和易混淆知识点处均有双语注解,有助读者掌握面向对象的程序设计方法。本书面向具有程序设计基础的读者,可作为高等院校计算机及相关专业的面向对象程序设计课程的双语教材。
刘嘉敏,现为国际SCI期刊论文评阅人。承担计算机专业硕士研究生、本科生专业课和专业基础课十多年,主持和参与辽宁省教和校级教改项目,获了省教改项目二等奖,并发表数篇教改论文。出版了全英文撰写的《C++面向对象程序设计双语教程》教材。数年从事布局优化、计算机图形学与虚拟现实的研究,曾主持和参与10多项辽宁省科技厅、省教育厅、市科委及地方企业的科研项目,并在国内外重要及核心期刊和国际会议发表学术论文30余篇,数篇论文被SCI和EI检索。
Contents
Chapter 1 Introduction 1
1.1 Overview of Programming 1
1.1.1 What Is Programming? 1
1.1.2 How to Write a Program? 3
1.2 Programming Methodologies 5
1.2.1 Structured Programming 5
1.2.2 Object-Oriented Programming 8
1.3 Characteristics of Object-Oriented Programming 10
1.4 C++ Programming Language 13
1.4.1 History of C and C++ 13
1.4.2 Learning C++ 15
Word Tips 16
Exercises 17
Chapter 2 Basic Facilities — Shifting from C to C++ Programs 18
2.1 C++ Program Structure 18
2.2 Input/Output Streams 21
2.2.1 Input Stream 21
2.2.2 Output Stream 22
2.3 Constants 23
2.4 Functions 25
2.4.1 Function Declarations 25
2.4.2 Function Definitions 26
2.4.3 Default Arguments 28
2.4.4 Inline Functions 30
2.4.5 Function Overloading 30
2.5 References 35
2.5.1 Reference Definition 35
2.5.2 Reference Variables as Parameters 39
2.5.3 References as Returning Values 40
2.5.4 Reference as Left-Hand Values 42
2.6 Namespaces 43
Word Tips 47
Exercises 48
Chapter 3 Foundation of Classes and Objects—Data Abstraction and Definition
of Classes 52
3.1 Introduction to Structures 52
3.1.1 Defining a Structure in C++ 52
3.1.2 Accessing Members of Structures 53
3.1.3 Structures with Member Functions 55
3.2 Data Abstraction and Classes 56
3.2.1 Data Abstraction 56
3.2.2 Defining Classes 57
3.2.3 Defining Objects 58
3.2.4 Accessing Member Functions 59
3.2.5 In-Class Member Function Definition 61
3.2.6 File Structure of an Abstract Data Type 63
3.3 Information Hiding 65
3.4 Access Control 66
3.5 Constructors 69
3.5.1 Definition of Constructors 69
3.5.2 Overloading Constructors 70
3.5.3 Constructors with Default Parameters 71
3.6 Destructors 74
3.6.1 Definition of Destructors 74
3.6.2 UML Diagram for Classes 75
3.6.3 The Order of Constructor and Destructor Calls 76
3.7 Encapsulation 78
3.8 Case Study: A GradeBook Class 79
Word Tips 82
Exercises 83
Chapter 4 Advance of Classes and Objects —Further Definition of Class Members
and Objects 87
4.1 Constant Member Functions and Constant Objects 87
4.2 this Pointers 89
4.3 Static Members 91
4.3.1 Static Data Members 93
4.3.2 Static Member Functions 96
4.4 Free Store 97
4.5 Object Members 101
4.5.1 Definition of Object Members 101
4.5.2 The Order of Constructors and Destructors for Member Objects 105
4.5.3 Object Members with Default Constructors 105
4.5.4 Class Members by Using Initializers 106
4.6 Copy Members 107
4.6.1 Definition of Copy Constructors 108
4.6.2 Shallow Copy and Deep Copy 110
4.7 Arrays of Objects 118
4.7.1 Initialize an Object Array by Using a Default Constructor 118
4.7.2 Initialize an Object Array by Using Constructors with Parameters 121
4.8 Friends 122
4.8.1 Friend Functions 122
4.8.2 Friend Classes 125
4.9 Case Study: Advance of the GradeBook Class 126
Word Tips 132
Exercises 132
Chapter 5 Operator Overloading 137
5.1 Introduction to Operator Overloading 137
5.2 Operator Functions 138
5.2.1 Overloaded Operators 138
5.2.2 Operator Functions 138
5.3 Binary and Unary Operators 142
5.3.1 Overloading Binary Operators 142
5.3.2 Overloading Unary Operators 143
5.4 Overloading Combinatorial Operators 147
5.5 Mixed Arithmetic of User-Defined Types 151
5.6 Type Conversion of User-Defined Types 152
5.6.1 Converting a Built-In Type to a User-Defined Type 152
5.6.2 Converting User-Defined Types to Built-In Types 153
5.7 Case Study: A MyInteger Class 155
Word Tips 160
Exercises 160
Chapter 6 Inheritance 163
6.1 Class Hierarchies 163
6.2 Derived Classes 164
6.2.1 Declaration of Derived Classes 164
6.2.2 Structure of Derived Classes 165
6.3 Constructors and Destructors of Derived Classes 168
6.3.1 Constructors of Derived Classes 168
6.3.2 Destructors of Derived Classes 171
6.3.3 The Calling Order of Derived Class Objects 172
6.3.4 Inheritance and Composition 175
6.4 Member Functions of Derived Classes 175
6.4.1 Defining a Member Function 175
6.4.2 Overriding Member Functions 177
6.5 Access Control 179
6.5.1 Access Control in Classes 179
6.5.2 Access to Base Classes 180
6.6 Multiple Inheritance 184
6.6.1 Declaration of Multiple Inheritance 185
6.6.2 Constructors of Multiple Inheritance 187
6.7 Virtual Inheritance 188
6.7.1 Multiple Inheritance Ambiguities 188
6.7.2 Trying to Solve Inheritance Ambiguities 189
6.7.3 Virtual Base Classes 191
6.7.4 Constructing Objects of Multiple Inheritance 194
6.8 Case Study: The iWatch Class 195
Word Tips 201
Exercises 202
Chapter 7 Polymorphism and Virtual Functions 212
7.1 Polymorphism 212
7.1.1 Introduction to Polymorphism 212
7.1.2 Binding 213
7.2 Virtual Functions 216
7.2.1 Definition of Virtual Functions 216
7.2.2 Extensibility 219
7.2.3 Principle of Virtual Functions 221
7.2.4 Virtual Destructors 223
7.2.5 Function Overloading and Function Overriding 224
7.3 Abstract Base Classes 227
7.4 Case Study: A Mini System 230
Word Tips 235
Exercises 235
Chapter 8 Templates 241
8.1 Introduction to Templates 241
8.2 Function Templates 242
8.2.1 Definition of Function Templates 242
8.2.2 Function Template Instantiation 244
8.2.3 Function Template with Different Parameter Types 246
8.2.4 Function Template Overloading 247
8.3 Class Templates 248
8.3.1 Definition of Class Templates 248
8.3.2 Class Template Instantiation 251
8.4 Non-Type Parameters for Templates 253
8.5 Derivation and Class Templates 255
8.6 Case Study: A Vector Class Template 256
Word Tips 262
Exercises 262
References 264