Preface v.
1 OBJECT-ORIENTED PROGRAMMING 1
1.1 Object-Oriented and Procedural Programming 2
Relationships 4
1.2 Classes and Abstract Data Types 5
Information Hiding 5
Encapsulation 6
Abstract Data Types 6
1.3 The Client/Server Model and Message Passing 9
The Client/Server Model 9
Message Passing and Method Invocation 10
1.4 Inheritance and Polymorphism 12
Inheritance 12
Polymorphism 13
Polymorphism and Recursion 14
1.5 Interfaces and Components 17
Component Technology 19
2 FROM C TO C++ 21
2.1 Namespaces 22
2.2 Introduction to C++ Input/Output 27
Manipulators 29
Mixing C and C++ Input/Output 34
2.3 Files 35
Testing Whether Files Are Open 37
2.4 C++ Features 38
Casts 38
static_cast 38
const_cast 39
reinterpret_cast 40
dynamic_cast 40
Constants 40
The DataType bool 40
Enumerated Types 41
Defining Variables 42
Structures 43
2.5 The Type string 44
Defining string Variables 45
Conversion to C-Style Strings 45
String Length 45
Writing and Reading strings 45
Assignment 47
Concatenation 48
Modifying Strings 48
Extracting a Substring 51
Searching 51
Comparing Strings 53
2.6 Functions 56
Prototypes 56
The main Function 57
References 57
Call by Reference 58
Return by Reference 59
Inline Functions 62
Default Arguments 63
Overloading Functions 64
Function Signatures 65
2.7 The new and delete Operators 70
2.8 Exception Handling 73
C++ Postscript 78
Keywords 78
Unnamed Namespaces 78
Anonymous Unions 78
The Member Selector Operators 79
Common Programming Errors 83
Programming Exercises 93
3 CLASSES 98
3.1 Classes and Objects 99
Class Declarations 99
Information Hiding in C++ 100
The Member Selector Operator 10!
Class Scope 103
The Difference between the Keywords class and struct 103
Defining Class Methods 104
Using Classes in a Program 106
3.2 Sample Application: A Stack Class 109
3.3 Efficiency and Robustness Issues for Classes and Objects 112
Passing and Returning Objects by Reference 113
Object References as const Parameters 113
const Methods 114
Overloading Methods to Handle Two Types of Strings 116
3.4 Sample Application: A Time Stamp Class 117
3.5 Constructors and the Destructor 124
Constructors 124
Arrays of Class Objects and the Default Constructor 127
Restricting Object Creation Through Constructors 127
The Copy Constructor 129
Defining a Copy Constructor 129
Disabling Passing and Returning by Value for Class Objects 135
Convert Constructors 136
The Convert Constructor and Implicit Type Conversion 137
Constructor Initializers 138
Constructors and the Operators new and new[ ] 139
The Destructor 139
3.6 Sample Application: A Task Class 145
3.7 Class Data Members and Methods 151
static Variables Defined Inside Methods 155
3.8 Pointers to Objects 157
The Pointer Constant this 159
Common Programming Errors 161
Programming Exercises 169
4 INHERITANCE 175
4.1 Introduction 176
4.2 Basic Concepts and Syntax 178
private Members in Inheritance 180
Adjusting Access 181
Name Hiding 182
Indirect Inheritance 183
4.3 Sample Application: Tracking Films 185
4.4 protected Members .190
4.5 Constructors and Destructors Under Inheritance 195
Constructors Under Inheritance 195
Derived Class Constructor Rules 198
Destructors Under Inheritance 202
4.6 Sample Application: A Sequence Hierarchy 205
4.7 Multiple Inheritance 216
Inheritance and Access 217
Virtual Base Classes 218
C++ Postscript 221
Protected Inheritance 221
private Inheritance 222
Common Programming Errors 223
Programming Exercises 226
5 POLYMORPHISM 229
5.1 Run-Time versus Compile-Time Binding in C++ 230
Requirements for C++ Polymorphism 231
Inheriting virtual Methods 235
Run-Time Binding and the Vtable 236
Constructors and the Destructor 237
virtual Destructors 237
Object Methods and Class Methods 240
5.2 Sample Application: Tracking Films Revisited 242
5.3 Name Overloading, Name Overriding, and Name Hiding 252
Name Overloading 252
Name Overriding 253
Name Hiding 255
Name Sharing in C++ Programming 257
5.4 Abstract Base Classes 260
Abstract Base Classes and Pure virtu1 Methods 260
Restrictions on Pure Functions 262
Uses of Abstract Base Classes 262
Microsoft's IUnknown Interface 263
5.5 Run-Time Type Identification 265
The dynamic_cast Operator 265
The Rules for dynamic_cast and static_cast 271
Summary of dynamic_cast and Static_cast 272
The typeid Operator 272
Extending RTTI 274
C++ Postscript 275
Strong and Weak Polymorphism 275
Common Programming Errors 276
Programming Exercises 280
6 OPERATOR OVERLOADING 285
6.1 Basic Operator Overloading 286
Operator Precedence and Syntax 289
6.2 Sample Application: A Complex Number Class 291
6.3 Operator Overloading Using Top-Level Functions 296
6.4 friend Functions 302
6.5 Overloading the Input and Output Operators 305
6.6 Overloading the Assignment Operator 307
6.7 Overloading Some Special Operators 311
Overloading the Subscript OperatOr 312
Overloading the Function Call Operator 315
Overloading the Increment and Decrement Operators 318
Type Conversions 321
6.8 Sample Application: An Associative Array 324
6.9 Memory Management Operators 329
C++ Postscript 334
friend Classes 334
Common Programming Errors 335
Programming Exercises 338
7 TEMPLATES AND THE STANDARD TEMPLATE LIBRARY 340
7.1 Template Basics 341
Template Instantiations 345
Template Classes in a Parameter List 347
Function-Style Parameters 348
7.2 Sample Application: A Template Stack Class 351
Assertions 358
7.3 The Standard Template Library 359
Containers, Algorithms, and Iterators 360
Reasons for Using STL 360
Container Basics 362
Basic Sequential Containers: vector,deque,and list 363
Efficiency of vectors,deques,and lists 367
Basic Associative Containers: set,multiset,map,and multimap 367
Container Adaptors 370
Other Containers 372
Algorithms 377
Other STL Constructs 382
7.4 Sample Application: Stock Performance Reports 385
C++ Postscript 394
Template Classes and Inheritance 394
Common Programming Errors 395
Programming Exercises 397
8 THE C++ INPUT/OUTPUT CLASS HIERARCHY 402
8.1 Overview 403
Templates 407
8.2 The Classes ios_base and basic_ios 408
ios_base 408
basic_ios 414
Exceptions 416
8.3 The High-Level Input/Output Classes 419
basic_istream 419
basic_ostream 424
basic_iostream 426
8.4 Manipulators 428
8.5 The File Input/Output Classes 432
basic_ofstream 433
basic_ifstream 434
basic_fstream 436
8.6 Sample Application: A Random Access File Class 438
8.7 The Character Stream Input/Output Classes 457
basic_ostringstream 457
basic_istringstream 458
basic_stringstream 460
8.8 Sample Application: A High-Level Copy Function 461
8.9 The Buffer Classes 463
basic_streambuf 463
basic_filebuf 465
basic_stringbuf 472
C++ Postscript 474
Common Programming Errors 475
Programming Exercises 477
9 OBIECT-ORIENTED PROGRAMMING IN THE MICROSOFT FOUNDATION
CLASSES 479
9.1 Windows Programming in MFC 48|
Code Generators for MFC Programming 482
9.2 The Document/View Architecture in MFC 483
Document Serialization 486
9.3 Sample Application: Document Serialization 489
9.4 The Common Object Model 503
Changeable Servers and Unchangeable Interfaces 505
The Interface Hierarchy 505
The IDispatch Interface 506
Types of COM Applications 507
VC++ Support for COM 508
COM and OLE 508
9.5 Sample Application: An Automation Server and Controller 510
The Challenge of Reference Counting 522
C++ Postscript 523
Acronyms Used in Chapter 9 523
Programming Exercises 524
APPENDICES
A ASCII TASTE 527
B SEtECTED C++ FUNCTIONS AND METHODS 531
HINTS AND SOtUTIONS TO ODD-NUMBERED EXERCISES 573
INDEX 601