Contents
1 Introduction 1
1.1 Introduction 1
1.2 History of C and C++ 2
1.3 C++ Standard Library 2
1.4 Key Software Trend: Object Technology 3
1.5 Typical C++ Development Environment 4
1.6 Notes About C++ and Small C++ How to Program, 5/e 7
1.7 Test-Driving a C++ Application 7
1.8 Introduction to Object Technology and the UML 12
1.9 Wrap-Up 17
1.10 Web Resources 17
2 Introduction to C++ Programming 21
2.1 Introduction 21
2.2 First Program in C++: Printing a Line of Text 21
2.3 Modifying Our First C++ Program 24
2.4 Another C++ Program: Adding Integers 25
2.5 Memory Concepts 28
2.6 Arithmetic 29
2.7 Decision Making: Equality and Relational Operators 33
2.8 Wrap-Up 36
3 Introduction to Classes and Objects 41
3.1 Introduction 41
3.2 Classes, Objects, Member Functions and Data Members 41
3.3 Overview of the Chapter Examples 42
3.4 Defining a Class with a Member Function 43
3.5 Defining a Member Function with a Parameter 45
3.6 Data Members, set Functions and get Functions 48
3.7 Initializing Objects with Constructors 54
3.8 Placing a Class in a Separate File for Reusability 57
3.9 Separating Interface from Implementation 60
3.10 Validating Data with set Functions 65
3.11 Wrap-Up 69
Contents
c++Book.book Page 1 Wednesday, November 9, 2005 1:35 PM
2 Contents
4 Control Statements: Part 1 73
4.1 Introduction 73
4.2 Algorithms 73
4.3 Pseudocode 73
4.4 Control Structures 74
4.5 if Selection Statement 78
4.6 if…else Double-Selection Statement 79
4.7 while Repetition Statement 83
4.8 Formulating Algorithms: Counter-Controlled Repetition 84
4.9 Formulating Algorithms: Sentinel-Controlled Repetition 89
4.10 Formulating Algorithms: Nested Control Statements 98
4.11 Assignment Operators 102
4.12 Increment and Decrement Operators 103
4.13 Wrap-Up 105
5 Control Statements: Part 2 111
5.1 Introduction 111
5.2 Essentials of Counter-Controlled Repetition 111
5.3 for Repetition Statement 113
5.4 Examples Using the for Statement 117
5.5 do…while Repetition Statement 120
5.6 switch Multiple-Selection Statement 122
5.7 break and continue Statements 130
5.8 Logical Operators 132
5.9 Confusing Equality (==) and Assignment (=) Operators 135
5.10 Structured Programming Summary 136
5.11 Wrap-Up 141
6 Functions and an Introduction to Recursion 145
6.1 Introduction 145
6.2 Program Components in C++ 145
6.3 Math Library Functions 147
6.4 Function Definitions with Multiple Parameters 148
6.5 Function Prototypes and Argument Coercion 152
6.6 C++ Standard Library Header Files 154
6.7 Case Study: Random Number Generation 155
6.8 Case Study: Game of Chance and Introducing enum 160
6.9 Storage Classes 163
6.10 Scope Rules 166
6.11 Function Call Stack and Activation Records 168
6.12 Functions with Empty Parameter Lists 172
6.13 Inline Functions 173
6.14 References and Reference Parameters 174
6.15 Default Arguments 178
6.16 Unary Scope Resolution Operator 180
c++Book.book Page 2 Wednesday, November 9, 2005 1:35 PM
Contents 3
6.17 Function Overloading 181
6.18 Function Templates 183
6.19 Recursion 185
6.20 Example Using Recursion: Fibonacci Series 188
6.21 Recursion vs. Iteration 191
6.22 Wrap-Up 193
7 Arrays and Vectors 203
7.1 Introduction 203
7.2 Arrays 203
7.3 Declaring Arrays 205
7.4 Examples Using Arrays 205
7.5 Passing Arrays to Functions 219
7.6 Case Study: Class GradeBook Using an Array to Store Grades 223
7.7 Searching Arrays with Linear Search 228
7.8 Sorting Arrays with Insertion Sort 230
7.9 Multidimensional Arrays 232
7.10 Case Study: Class GradeBook Using a Two-Dimensional Array 234
7.11 Introduction to C++ Standard Library Class Template vector 240
7.12 Wrap-Up 244
8 Pointers and Pointer-Based Strings 249
8.1 Introduction 249
8.2 Pointer Variable Declarations and Initialization 249
8.3 Pointer Operators 250
8.4 Passing Arguments to Functions by Reference with Pointers 253
8.5 Using const with Pointers 256
8.6 Selection Sort Using Pass-by-Reference 262
8.7 sizeof Operators 265
8.8 Pointer Expressions and Pointer Arithmetic 267
8.9 Relationship Between Pointers and Arrays 270
8.10 Arrays of Pointers 273
8.11 Case Study: Card Shuffling and Dealing Simulation 274
8.12 Function Pointers 279
8.13 Introduction to Pointer-Based String Processing 283
8.13.1 Fundamentals of Characters and Pointer-Based Strings 283
8.13.2 String Manipulation Functions of the String-Handling Library 285
8.14 Wrap-Up 292
9 Classes: A Deeper Look, Part 1 299
9.1 Introduction 299
9.2 Time Class Case Study 299
9.3 Class Scope and Accessing Class Members 305
9.4 Separating Interface from Implementation 307
c++Book.book Page 3 Wednesday, November 9, 2005 1:35 PM
4 Contents
9.5 Access Functions and Utility Functions 307
9.6 Time Class Case Study: Constructors with Default Arguments 310
9.7 Destructors 314
9.8 When Constructors and Destructors Are Called 315
9.9 Time Class Case Study:A Subtle Trap—Returning a Reference to a private
Data Member 318
9.10 Default Memberwise Assignment 320
9.11 Software Reusability 322
9.12 Wrap-Up 322
10 Classes: A Deeper Look, Part 2 327
10.1 Introduction 327
10.2 const (Constant) Objects and const Member Functions 327
10.3 Composition: Objects as Members of Classes 336
10.4 friend Functions and friend Classes 341
10.5 Using the this Pointer 345
10.6 Dynamic Memory Management with Operators new and delete 349
10.7 static Class Members 351
10.8 Data Abstraction and Information Hiding 356
10.8.1 Example: Array Abstract Data Type 357
10.8.2 Example: String Abstract Data Type 357
10.8.3 Example: Queue Abstract Data Type 357
10.9 Container Classes and Iterators 358
10.10 Proxy Classes 358
10.11 Wrap-Up 361
11 Operator Overloading; String and Array Objects 365
11.1 Introduction 365
11.2 Fundamentals of Operator Overloading 365
11.3 Restrictions on Operator Overloading 366
11.4 Operator Functions as Class Members vs. Global Functions 368
11.5 Overloading Stream Insertion and Stream Extraction Operators 369
11.6 Overloading Unary Operators 372
11.7 Overloading Binary Operators 372
11.8 Case Study: Array Class 373
11.9 Converting between Types 383
11.10 Case Study: String Class 384
11.11 Overloading ++ and -- 394
11.12 Case Study: A Date Class 395
11.13 Standard Library Class string 399
11.14 explicit Constructors 402
11.15 Wrap-Up 405
c++Book.book Page 4 Wednesday, November 9, 2005 1:35 PM
Contents 5
12 Object-Oriented Programming: Inheritance 409
12.1 Introduction 409
12.2 Base Classes and Derived Classes 410
12.3 protected Members 412
12.4 Relationship between Base Classes and Derived Classes 412
12.4.1 Creating and Using a CommissionEmployee Class 413
12.4.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance 417
12.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance
Hierarchy 422
12.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy
Using protected Data 426
12.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy
Using private Data 435
12.5 Constructors and Destructors in Derived Classes 439
12.6 public, protected and private Inheritance 446
12.7 Software Engineering with Inheritance 446
12.8 Wrap-Up 448
13 Object-Oriented Programming: Polymorphism 451
13.1 Introduction 451
13.2 Polymorphism Examples 452
13.3 Relationships Among Objects in an Inheritance Hierarchy 453
13.3.1 Invoking Base-Class Functions from Derived-Class Objects 453
13.3.2 Aiming Derived-Class Pointers at Base-Class Objects 459
13.3.3 Derived-Class Member-Function Calls via Base-Class Pointers 460
13.3.4 Virtual Functions 462
13.3.5 Summary of the Allowed Assignments Between Base-Class and Derived-Class
Objects and Pointers 467
13.4 Type Fields and switch Statements 468
13.5 Abstract Classes and Pure virtual Functions 468
13.6 Case Study: Payroll System Using Polymorphism 470
13.6.1 Creating Abstract Base Class Employee 471
13.6.2 Creating Concrete Derived Class SalariedEmployee 474
13.6.3 Creating Concrete Derived Class HourlyEmployee 476
13.6.4 Creating Concrete Derived Class CommissionEmployee 478
13.6.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee 480
13.6.6 Demonstrating Polymorphic Processing 481
13.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood” 485
13.8 Case Study: Payroll System 488
13.9 Virtual Destructors 491
13.10 Wrap-Up 492
c++Book.book Page 5 Wednesday, November 9, 2005 1:35 PM