For courses that offer a Microsoft-specific C++ programming focus using Visual C++ 2008. Based on the authors' highly popular C++-standard-compliant textbook, C++ How to Program, 6/e, Visual C++ How to Program 2/ E builds off this successful approach to offer readers a comprehensive introduction to Visual C++. Through their ongoing consultation with the Microsoft Visual C++ team, the Deitels have developed an approach that focuses on native C++ and presents examples of .NET managed code programming with C++/CLI, where appropriate. The book takes an easy-to-follow, carefully developed early classes and objects approach. This includes comprehensive coverage of the Visual C++ environment. Like other books in the How to Program series, the new edition retains the Deitels' classic approach to developing good programming skills. These tools have been refined over a combined six decades of academic and industry teaching experience. This approach has been the foundation of several programmers in industry today.
Table of Contents
1 Introduction to Computers, the Internet and Visual C++ 1.1 Introduction 1.2 What Is a Computer? 1.3 Computer Organization 1.4 Early Operating Systems 1.5 Personal Computing, Distributed Computing and Client/Server Computing 1.6 The Internet and the World Wide Web 1.7 Hardware Trends 1.8 Microsoft's WindowsA (R) Operating System 1.9 Machine Languages, Assembly Languages and High-Level Languages 1.10 Visual C++ 1.11 C++ Standard Library 1.12 Java, C# and Visual Basic 1.13 Other High-Level Languages 1.14 Microsoft's .NET 1.15 The .NET Framework and the Common Language Runtime 1.16 Key Software Trend: Object Technology 1.17 Typical Visual C++ Development Environment 1.18 Test-Driving a Visual C++ Application 1.19 Software Technologies 1.20 Future of Visual C++: Open Source Boost Libraries, TR1 and C++0x 1.21 (Only Required Section of the Case Study) Software Engineering Case Study: Introduction to Object Technology and the UML 1.22 Wrap-Up 2 Dive IntoA (R) Visual C++A (R) 2008 Express 2.1 Introduction 2.2 Overview of the Visual Studio 2008 IDE 2.3 Menu Bar and Toolbar 2.4 Navigating the Visual Studio 2008 IDE 2.4.1 Solution Explorer 2.4.2 Properties Window 2.5 Using Help 2.6 Wrap-Up 2.7 Web Resources 2.7.1 Toolbox 3 Introduction to Visual C++ Programming 3.1 Introduction 3.2 First Program in Visual C++: Printing a Line of Text 3.3 Modifying Our First Visual C++ Program 3.4 Another Visual C++ Program: Adding Integers 3.5 Memory Concepts 3.6 Arithmetic 3.7 Decision Making: Equality and Relational Operators 3.8 (Optional) Software Engineering Case Study: Examining the ATM Requirements Specification 3.9 Wrap-Up 4 Introduction to Classes and Objects 4.1 Introduction 4.2 Classes, Objects, Member Functions and Data Members 4.3 Overview of the Chapter Examples 4.4 Defining a Class with a Member Function 4.5 Defining a Member Function with a Parameter 4.6 Data Members, set Functions and get Functions 4.7 Initializing Objects with Constructors 4.8 Placing a Class in a Separate File for Reusability 4.9 Separating Interface from Implementation 4.10 Validating Data with set Functions 4.11 Defining a Managed Class with Member Functions in C++/CLI 4.12 Instance Variables and Properties in C++/CLI 4.13 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Specification 4.14 Wrap-Up 5 Control Statements: Part 1 5.1 Introduction 5.2 Algorithms 5.3 Pseudocode 5.4 Control Structures 5.5 if Selection Statement 5.6 if...else Double-Selection Statement 5.7 while Repetition Statement 5.8 Formulating Algorithms: Counter-Controlled Repetition 5.9 Formulating Algorithms: Sentinel-Controlled Repetition 5.10 Formulating Algorithms: Nested Control Statements 5.11 Assignment Operators 5.12 Increment and Decrement Operators 5.13 (Optional) Software Engineering Case Study: Identifying Class Attributes in the ATM System 5.14 Wrap-Up 6 Control Statements: Part 2 6.1 Introduction 6.2 Essentials of Counter-Controlled Repetition 6.3 for Repetition Statement 6.4 Examples Using the for Statement 6.5 do...while Repetition Statement 6.6 switch Multiple-Selection Statement 6.7 break and continue Statements 6.8 Logical Operators 6.9 Confusing the Equality (==) and Assignment (=) Operators 6.10 Structured Programming Summary 6.11 (Optional) Software Engineering Case Study: Identifying Objects' States and Activities in the ATM System 6.12 Wrap-Up 7 Functions and an Introduction to Recursion 7.1 Introduction 7.2 Program Components in Visual C++ 7.3 Math Library Functions 7.4 Function Definitions with Multiple Parameters 7.5 Function Prototypes and Argument Coercion 7.6 C++ Standard Library Header Files 7.7 Case Study: Random Number Generation 7.8 Case Study: Game of Chance; Introducing enum 7.9 Storage Classes 7.10 Scope Rules 7.11 Function-Call Stack and Activation Records 7.12 Functions with Empty Parameter Lists 7.13 Inline Functions 7.14 References and Reference Parameters 7.15 Default Arguments 7.16 Unary Scope Resolution Operator 7.17 Function Overloading 7.18 Function Templates 7.19 Recursion 7.20 Example Using Recursion: Fibonacci Series 7.21 Recursion vs. Iteration 7.22 Enumerations in C++/CLI 7.23 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System 7.24 Wrap-Up 8 Arrays and Vectors 8.1 Introduction 8.2 Arrays 8.3 Declaring Arrays 8.4 Examples Using Arrays 8.4.1 Declaring an Array and Using a Loop to Initialize the Array's Elements 8.4.2 Initializing an Array in a Declaration with an Initializer List 8.4.3 Specifying an Array's Size with a Constant Variable and Setting Array Elements with Calculations 8.4.4 Summing the Elements of an Array 8.4.5 Using Bar Charts to Display Array Data Graphically 8.4.6 Using the Elements of an Array as Counters 8.4.7 Using Arrays to Summarize Survey Results 8.4.8 Static Local Arrays and Automatic Local Arrays 8.5 Passing Arrays to Functions 8.6 Case Study: Class GradeBook Using an Array to Store Grades 8.7 Searching Arrays with Linear Search 8.8 Sorting Arrays with Insertion Sort 8.9 Multidimensional Arrays 8.10 Case Study: Class GradeBook Using a Two-Dimensional Array 8.11 Introduction to C++ Standard Library Class Template vector 8.12 Introduction to Managed Arrays with C++/CLI 8.13 for each Statement 8.14 Multidimensional Arrays in C++/CLI 8.15 (Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System 8.16 Wrap-Up 9 Pointers and Pointer-Based Strings 9.1 Introduction 9.2 Pointer Variable Declarations and Initialization 9.3 Pointer Operators 9.4 Passing Arguments to Functions by Reference with Pointers 9.5 Using const with Pointers 9.6 Selection Sort Using Pass-by-Reference 9.7 sizeof Operator 9.8 Pointer Expressions and Pointer Arithmetic 9.9 Relationship Between Pointers and Arrays 9.10 Arrays of Pointers 9.11 Case Study: Card Shuffling and Dealing Simulation 9.12 Function Pointers 9.13 Introduction to Pointer-Based String Processing 9.13.1 Fundamentals of Characters and Pointer-Based Strings 9.13.2 String-Manipulation Functions of the String-Handling Library 9.14 Introduction to C++/CLI Handles 9.15 Passing Arguments to Functions by Reference with Handles 9.16 Tracking References and References to Handles 9.17 Interior Pointers 9.18 Wrap-Up 10 Classes: A Deeper Look, Part 1 10.1 Introduction 10.2 Time Class Case Study 10.3 Class Scope and Accessing Class Members 10.4 Separating Interface from Implementation 10.5 Access Functions and Utility Functions 10.6 Time Class Case Study: Constructors with Default Arguments 10.7 Destructors 10.8 When Constructors and Destructors Are Called 10.9 Time Class Case Study: A Subtle Trap-Returning a Reference to a private Data Member 10.10 Default Memberwise Assignment 10.11 Class View and Object Browser 10.12 (Optional) Software Engineering Case Study: Starting to Program the Classes of the ATM System 10.13 Wrap-Up 11 Classes: A Deeper Look, Part 2 11.1 Introduction 11.2 const (Constant) Objects and const Member Functions 11.3 Composition: Objects as Members of Classes 11.4 friend Functions and friend Classes 11.5 Using the this Pointer 11.6 Dynamic Memory Management with Operators new and delete 11.7 static Class Members 11.8 Data Abstraction and Information Hiding 11.8.1 Example: Array Abstract Data Type 11.8.2 Example: String Abstract Data Type 11.8.3 Example: Queue Abstract Data Type 11.9 Container Classes and Iterators 11.10 Proxy Classes 11.11 const and friend in C++/CLI 11.12 Dynamic Memory Management in C++/CLI 11.13 Stack Semantics in C++/CLI 11.14 Finalizers 11.15 Value Types vs. Reference Types 11.16 Boxing and Unboxing in C++/CLI 11.17 Indexers 11.18 Wrap-Up 12 Operator Overloading; String and Array Objects 12.1 Introduction 12.2 Fundamentals of Operator Overloading 12.3 Restrictions on Operator Overloading 12.4 Operator Functions as Class Members vs. Global Functions 12.5 Overloading Stream Insertion and Stream Extraction Operators 12.6 Overloading Unary Operators 12.7 Overloading Binary Operators 12.8 Case Study: Array Class 12.9 Converting between Types 12.10 Case Study: String Class 12.11 Overloading ++ and -- 12.12 Case Study: A Date Class 12.13 Standard Library Class string 12.14 explicit Constructors 12.15 C++/CLI Operators and Constructors 12.16 Wrap-Up 13 Object-Oriented Programming: Inheritance 13.1 Introduction 13.2 Base Classes and Derived Classes 13.3 protected Members 13.4 Relationship between Base Classes and Derived Classes 13.4.1 Creating and Using a CommissionEmployee Class 13.4.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance 13.4.3 Creating a CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy 13.4.4 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data 13.4.5 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using private Data 13.5 Constructors and Destructors in Derived Classes 13.6 public, protected and private Inheritance 13.7 Software Engineering with Inheritance 13.8 Inheritance in C++/CLI 13.9 Wrap-Up 14 Object-Oriented Programming: Polymorphism 14.1 Introduction 14.2 Polymorphism Examples 14.3 Relationships Among Objects in an Inheritance Hierarchy 14.3.1 Invoking Base-Class Functions from Derived-Class Objects 14.3.2 Aiming Derived-Class Pointers at Base-Class Objects 14.3.3 Derived-Class Member-Function Calls via Base-Class Pointers 14.3.4 Virtual Functions 14.3.5 Summary of the Allowed Assignments Between Base-Class and Derived-Class Objects and Pointers 14.4 Type Fields and switch Statements 14.5 Abstract Classes and Pure virtual Functions 14.6 Case Study: Payroll System Using Polymorphism 14.6.1 Creating Abstract Base Class Employee 14.6.2 Creating Concrete Derived Class SalariedEmployee 14.6.3 Creating Concrete Derived Class HourlyEmployee 14.6.4 Creating Concrete Derived Class CommissionEmployee 14.6.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee 14.6.6 Demonstrating Polymorphic Processing 14.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding "Under the Hood" 14.8 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info 14.9 Virtual Destructors 14.10 Polymorphism in C++/CLI 14.11 (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System 14.12 Wrap-Up 15 Templates and Generics 15.1 Introduction 15.2 Function Templates 15.3 Overloading Function Templates 15.4 Class Templates 15.5 Nontype Parameters and Default Types for Class Templates 15.6 Notes on Templates and Inheritance 15.7 Notes on Templates and Friends 15.8 Notes on Templates and static Members 15.9 Templates in C++/CLI 15.10 .NET Generics in C++/CLI 15.11 Generic Type Constraints 15.12 Contrasting Templates and Generics 15.13 Wrap-Up 16 Exception Handling 16.1 Introduction 16.2 Exception-Handling Overview 16.3 Example: Divide by Zero Without Exception Handling 16.4 Example: Handling an Attempt to Divide by Zero 16.5 When to Use Exception Handling 16.6 Rethrowing an Exception 16.7 Processing Unexpected Exceptions 16.8 Stack Unwinding 16.9 Constructors, Destructors and Exception Handling 16.10 Exceptions and Inheritance 16.11 Processing new Failures 16.12 Class auto_ptr and Dynamic Memory Allocation 16.13 Standard Library Exception Hierarchy 16.14 Other Error-Handling Techniques 16.15 .NET Exception Hierarchy with C++/CLI 16.15.1 Classes ApplicationException and SystemException 16.15.2 Determining Which Exceptions a Function Throws 16.16 finally Block in C++/CLI 16.17 Exception Properties in C++/CLI 16.18 User-Defined Exception Classes 16.19 Wrap-Up 16.20 Exception Specifications 17 Stream Input/Output and Files 17.1 Introduction 17.2 Streams 17.2.1 Classic Streams vs. Standard Streams 17.2.2 iostream Library Header Files 17.2.3 Stream-Input/Output Classes and Objects 17.3 Stream Output 17.3.1 Output of char * Variables 17.3.2 Character Output Using Member Function put 17.4 Stream Input 17.4.1 get and getline Member Functions 17.4.2 istream Member Functions peek, putback and ignore 17.4.3 Type-Safe I/O 17.5 Unformatted I/O Using read, write and gcount 17.6 Introduction to Stream Manipulators 17.6.1 Integral Stream Base: dec, oct, hex and setbase 17.6.2 Floating-Point Precision (precision, setprecision) 17.6.3 Field Width (width, setw) 17.6.4 User-Defined Output Stream Manipulastors 17.7 Stream Format States and Stream Manipulators 17.7.1 Trailing Zeros and Decimal Points (showpoint) 17.7.2 Justification (left, right and internal) 17.7.3 Padding (fill, setfill) 17.7.4 Integral Stream Base (dec, oct, hex, showbase) 17.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed) 17.7.6 Uppercase/Lowercase Control (uppercase) 17.7.7 Specifying Boolean Format (boolalpha) 17.7.8 Setting and Resetting the Format State via Member Function flags 17.8 Stream Error States 17.9 Tying an Output Stream to an Input Stream 17.10 Data Hierarchy 17.11 Files and Streams 17.12 Creating a Sequential File 17.13 Reading Data from a Sequential File 17.14 Updating Sequential Files 17.15 Wrap-Up 18 Files and Streams in .NET 18.1 Introduction 18.2 Files and Streams 18.3 Classes File and Directory 18.4 Creating a Sequential-Access Text File 18.5 Reading Data from a Sequential-Access Text File 18.6 Serialization 18.7 Creating a Sequential-Access File Using Object Serialization 18.8 Reading and Deserializing Data from a Sequential-Access Text File 18.9 Wrap-Up 19 Class string and String Stream Processing 19.1 Introduction 19.2 string Assignment and Concatenation 19.3 Comparing strings 19.4 Substrings 19.5 Swapping strings 19.6 string Characteristics 19.7 Finding Substrings and Characters in a string 19.8 Replacing Characters in a string 19.9 Inserting Characters into a string 19.10 Conversion to C-Style Pointer-Based char * Strings 19.11 Iterators 19.12 String Stream Processing 19.13 Fundamentals of Characters and Strings in C++/CLI 19.14 String Constructors 19.15 String Indexer, Length Property and CopyTo Function 19.16 Comparing Strings 19.17 Locating Characters and Substrings in Strings 19.18 Extracting Substrings from Strings 19.19 Concatenating Strings 19.20 Miscellaneous String Functions 19.21 Class StringBuilder 19.22 Length and Capacity Properties, EnsureCapacity Function and Indexer of Class StringBuilder 19.23 Append and AppendFormat Functions of Class StringBuilder 19.24 Insert, Remove and Replace Functions of Class StringBuilder 19.25 Char Functions 19.26 Wrap-Up 20 Searching and Sorting 20.1 Introduction 20.2 Searching Algorithms 20.2.1 Efficiency of Linear Search 20.2.2 Binary Search 20.3 Sorting Algorithms 20.3.1 Efficiency of Selection Sort 20.3.2 Efficiency of Insertion Sort 20.3.3 Merge Sort (A Recursive Implementation) 20.4 Wrap-Up 21 Data Structures 21.1 Introduction 21.2 Self-Referential Classes 21.3 Dynamic Memory Allocation and Data Structures 21.4 Linked Lists 21.5 Stacks 21.6 Queues 21.7 Trees 21.8 Wrap-Up 22 Bits, Characters, C Strings and structs 22.1 Introduction 22.2 Structure Definitions 22.3 Initializing Structures 22.4 Using Structures with Functions 22.5 typedef 22.6 Example: High-Performance Card Shuffling and Dealing Simulation 22.7 Bitwise Operators 22.8 Bit Fields 22.9 Character-Handling Library 22.10 Pointer-Based String-Conversion Functions 22.11 Search Functions of the Pointer-Based String-Handling Library 22.12 Memory Functions of the Pointer-Based String-Handling Library 22.13 Wrap-Up 23 Standard Template Library (STL) 23.1 Introduction to the Standard Template Library (STL) 23.1.1 Introduction to Containers 23.1.2 Introduction to Iterators 23.1.3 Introduction to Algorithms 23.2 Sequence Containers 23.2.1 vector Sequence Container 23.2.2 list Sequence Container 23.2.3 deque Sequence Container 23.3 Associative Containers 23.3.1 multiset Associative Container 23.3.2 set Associative Container 23.3.3 multimap Associative Container 23.3.4 map Associative Container 23.4 Container Adapters 23.4.1 stack Adapter 23.4.2 queue Adapter 23.4.3 priority_queue Adapter 23.5 Algorithms 23.5.1 fill, fill_n, generate and generate_n 23.5.2 equal, mismatch and lexicographical_compare 23.5.3 remove, remove_if, remove_copy and remove_copy_if 23.5.4 replace, replace_if, replace_copy and replace_copy_if 23.5.5 Mathematical Algorithms 23.5.6 Basic Searching and Sorting Algorithms 23.5.7 swap, iter_swap and swap_ranges 23.5.8 copy_backward, merge, unique and reverse 23.5.9 inplace_merge, unique_copy and reverse_copy 23.5.10 Set Operations 23.5.11 lower_bound, upper_bound and equal_range 23.5.12 Heapsort 23.5.13 min and max 23.5.14 STL Algorithms Not Covered in This Chapter 23.6 Class bitset 23.7 Function Objects 23.8 Introduction to STL/CLR 23.9 Wrap-Up 23.10 STL Web Resources 25 Regular Expressions 25.1 Introduction 25.2 Simple Regular Expressions and Class Regex 25.3 Complex Regular Expressions 25.4 Validating User Input with Regular Expressions 25.5 Regex Member Functions Replace and Split 25.6 Wrap-Up 26 Collections 26.1 Introduction 26.2 Collections Overview 26.3 Class Array and Enumerators 26.4 Nongeneric Collections 26.4.1 Class ArrayList 26.4.2 Class Stack 26.4.3 Class Hashtable 26.5 Generic Collections 26.5.1 Generic Class SortedDictionary 26.5.2 Generic Class LinkedList 26.6 Wrap-Up 27 Other Topics 27.1 Introduction 27.2 Other Cast Operators 27.3 namespaces 27.4 Operator Keywords 27.5 mutable Class Members 27.6 Pointers to Class Members (.* and ->*) 27.7 Multiple Inheritance 27.8 Multiple Inheritance and virtual Base Classes 27.9 Variable-Length Argument Lists 27.10 Using Command-Line Arguments 27.11 Delegates and Events in .NET 27.12 Wrap-Up 27.13 Creating a Random-Access File
Paul J. Deitel, CEO and Chief Technical Officer of Deitel & Associates, Inc., is a graduate of MIT's Sloan School of Management, where he studied Information Technology. He holds the Java Certified Programmer and Java Certified Developer certifications, and has been designated by Sun Microsystems as a Java Champion. Through Deitel & Associates, Inc., he has delivered Java, C, C++, C# and Visual Basic courses to industry clients, including IBM, Sun Microsystems, Dell, Lucent Technologies, Fidelity, NASA at the Kennedy Space Center, the National Severe Storm Laboratory, White Sands Missile Range, Rogue Wave Software, Boeing, Stratus, Cambridge Technology Partners, Open Environment Corporation, One Wave, Hyperion Software, Adra Systems, Entergy, CableData Systems, Nortel Networks, Puma, iRobot, Invensys and many more. He has also lectured on Java and C++ for the Boston Chapter of the Association for Computing Machinery. He and his father, Dr. Harvey M. Deitel, are the world's best-selling programming language textbook authors. Dr. Harvey M. Deitel, Chairman and Chief Strategy Officer of Deitel & Associates, Inc., has 45 years of academic and industry experience in the computer field. Dr. Deitel earned B.S. and M.S. degrees from the MIT and a Ph.D. from Boston University. He has 20 years of college teaching experience, including earning tenure and serving as the Chairman of the Computer Science Department at Boston College before founding Deitel & Associates, Inc., with his son, Paul J. Deitel. He and Paul are the co-authors of several dozen books and multimedia packages and they are writing many more. With translations published in Japanese, German, Russian, Spanish, Traditional Chinese, Simplified Chinese, Korean, French, Polish, Italian, Portuguese, Greek, Urdu and Turkish, the Deitels' texts have earned international recognition. Dr. Deitel has delivered hundreds of professional seminars to major corporations, academic institutions, government organizations and the military.