图书简介:
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
展开
前 言
本书自第1版和第2版出版以来,在作者所在学校计算机科学与技术专业和其他高校相关专业教学中已使用5年,得到了广大师生的赞许,不失为高等院校相关专业课程的一本较好且实用性强的双语教材。
本书从面向对象程序设计的特点和工程应用角度出发,渐进式地组织各章节的知识点,采用既易懂又切合实际的实例,引导初学者进入面向对象程序设计之门。本书结合当今学生对新知识的认知程度,以保持原有特色为前提,在前两版的基础上,对各章内容进行了删减、增补和修改,适当地增加了案例和练习题,突出主线,使知识点的关联性更好,有助于初学者理解和掌握相关概念和实现方法。
本书在保持英文原汁原味的基础上,采用符合中国学生学习习惯且通俗易懂的英语描述,并针对重要的知识点,通过“教学目标”“重点注释”“句法”“案例”“概念加注框”“思考题”“词汇小贴士”“案例学习”“练习题”形式来组织内容,重点突出、易于学习。特别是本版对“重点注释”“概念加注框”给出了相应的中文解释,而且采用黑体字突显其中的概念。此外,在正文中采用黑斜体字和斜体字突出了关键术语和代码中出现的变量和函数名,这样更便于学生阅读和掌握。
本书旨在培养学生掌握面向对象程序设计的基本概念、思想和方法,虽然本书内容是以标准C++程序设计语言来描述的,但不是描述C++的面向对象程序设计,其内涵与用其他语言描述面向对象程序设计是一致的,因而在许多概念上不过分强调C++语言的细节,而是着重强调对面向对象程序设计的概念、思想和方法的认识。
面向对象程序设计是一门实践性较强的课程,因而上机实践是学习和巩固知识点必不可少的环节。为此,本书针对每个知识点配有完整的代码和运行结果,使学生可以确定程序预期结果,通过输出结果与程序语句联系在一起,为学生提供实践和自学的方式。书中所有源代码在Visual Studio 2013环境中调试和运行。
本书适合40~54学时教学,配有PPT教学课件,并提供实验指导书、实验题目详解代码、面向对象程序设计的课程设计题目,有需要的任课教师请垂询jmliu@sut.edu.com。
刘嘉敏老师修订了第1、3、4、5、8章,马广焜老师修订了第6、7章并参与了第8章的修订,姚凯老师修订了第2章,常燕老师参与了第3、4章和练习题的修订,朱世铁老师参与了练习题和实验指导书的修订,李树江老师参与了第6、7章的修订。本书得到了沈阳工业大学信息科学与工程学院老师和学生、英国大学同行专家、美国南加州大学王博涵同学的帮助,以及作者家人的支持,而且书中引用了其他同行的工作成果,在此表示衷心感谢。
由于作者的英语水平有限,书中难免存在错漏和不妥之处,恳请读者提出宝贵意见。
编 著 者
展开