CONTENTS xxix Exception Specification 911 Pitfall:Exception Specification in Derived Classes 913 16.2 PROGRAMMING TECHNIQUES FOR EXCEPTION HANDLING 914 When to Throw an Exception 914 Pitfall:Uncaught Exceptions 916 Pitfall:Nested try-catch Blocks 916 Pitfall:Overuse of Exceptions 916 Exception Class Hierarchies 917 Testing for Available Memory 917 Rethrowing an Exception 918 Chapter Summary 918 Answers to Self-Test Exercises 918 Practice Programs 920 Programming Projects 921 Chapter 17 Templates 925 17.1 TEMPLATES FOR ALGORITHM ABSTRACTION 926 Templates for Functions 927 Pitfall:Compiler Complications 931 Programming Example:A Generic Sorting Function 933 Programming Tip:How to Define Templates 937 Pitfall:Using a Template with an Inappropriate Type 938 17.2 TEMPLATES FOR DATA ABSTRACTION 939 Syntax for Class Templates 939 Programming Example:An Array Class 942 Chapter Summary 949 Answers to Self-Test Exercises 949 Practice Programs 953 Programming Projects 953 Chapter 18 Standard Template Library 957 18.1 ITERATORS 959 using Declarations 959 Iterator Basics 960
Contents xxix Exception Specification 911 Pitfall: Exception Specification in Derived Classes 913 16.2 Programming Techniques for Exception Handling 914 When to Throw an Exception 914 Pitfall: Uncaught Exceptions 916 Pitfall: Nested try-catch Blocks 916 Pitfall: Overuse of Exceptions 916 Exception Class Hierarchies 917 Testing for Available Memory 917 Rethrowing an Exception 918 Chapter Summary 918 Answers to Self-Test Exercises 918 Practice Programs 920 Programming Projects 921 Chapter 17 Templates 925 17.1 Templates for Algorithm Abstraction 926 Templates for Functions 927 Pitfall: Compiler Complications 931 Programming Example: A Generic Sorting Function 933 Programming Tip: How to Define Templates 937 Pitfall: Using a Template with an Inappropriate Type 938 17.2 Templates for Data Abstraction 939 Syntax for Class Templates 939 Programming Example: An Array Class 942 Chapter Summary 949 Answers to Self-Test Exercises 949 Practice Programs 953 Programming Projects 953 Chapter 18 Standard Template Library 957 18.1 Iterators 959 using Declarations 959 Iterator Basics 960
XXX CONTENTS Programming Tip:Use auto to Simplify Variable Declarations 964 Pitfall:Compiler Problems 964 Kinds of Iterators 966 Constant and Mutable Iterators 970 Reverse Iterators 971 Other Kinds of Iterators 972 18.2 CONTAINERS 973 Sequential Containers 974 Pitfall:Iterators and Removing Elements 978 Programming Tip:Type Definitions in Containers 979 Container Adapters stack and queue 979 Associative Containers set and map 983 Programming Tip:Use Initialization,Ranged For, and auto with Containers 990 Efficiency 990 18.3 GENERIC ALGORITHMS 991 Running Times and Big-O Notation 992 Container Access Running Times 995 Nonmodifying Sequence Algorithms 997 Container Modifying Algorithms 1001 Set Algorithms 1003 Sorting Algorithms 1004 Chapter Summary 1005 Answers to Self-Test Exercises 1005 Practice Programs 1007 Programming Projects 1008 APPENDICES 1 C++Keywords 1015 2 Precedence of Operators 1016 3 The ASCII Character Set 1018 4 Some Library Functions 1019 5 Inline Functions 1026 6 Overloading the Array Index Square Brackets 1027 7 The this Pointer 1029 8 Overloading Operators as Member Operators 1032 INDEX 1034
xxx Contents Programming Tip: Use auto to Simplify Variable Declarations 964 Pitfall: Compiler Problems 964 Kinds of Iterators 966 Constant and Mutable Iterators 970 Reverse Iterators 971 Other Kinds of Iterators 972 18.2 Containers 973 Sequential Containers 974 Pitfall: Iterators and Removing Elements 978 Programming Tip: Type Definitions in Containers 979 Container Adapters stack and queue 979 Associative Containers set and map 983 Programming Tip: Use Initialization, Ranged For, and auto with Containers 990 Efficiency 990 18.3 Generic Algorithms 991 Running Times and Big-O Notation 992 Container Access Running Times 995 Nonmodifying Sequence Algorithms 997 Container Modifying Algorithms 1001 Set Algorithms 1003 Sorting Algorithms 1004 Chapter Summary 1005 Answers to Self-Test Exercises 1005 Practice Programs 1007 Programming Projects 1008 Appendices 1 C++ Keywords 1015 2 Precedence of Operators 1016 3 The ASCII Character Set 1018 4 Some Library Functions 1019 5 Inline Functions 1026 6 Overloading the Array Index Square Brackets 1027 7 The this Pointer 1029 8 Overloading Operators as Member Operators 1032 Index 1034
Introduction to Computers and C++Programming 1.1 COMPUTER SYSTEMS 2 Pitfall:Using the Wrong Slash in \n 23 Hardware 2 Programming Tip:Input and Output Software 7 Syntax 23 High-Level Languages 8 Layout of a Simple C++Program 24 Compilers 9 Pitfall:Putting a Space Before the include History Note 12 File Name 26 Compiling and Running a C++Program 26 1.2 PROGRAMMING AND Pitfall:Compiling a C++11 Program 27 PROBLEM-SOLVING 12 Programming Tip:Getting Your Program Algorithms 12 to Run 27 Program Design 15 Object-Oriented Programming 16 1.4 TESTING AND DEBUGGING 29 The Software Life Cycle 17 Kinds of Program Errors 30 Pitfall:Assuming Your Program Is Correct 31 1.3 INTRODUCTION TO C++18 Origins of the C++Language 18 A Sample C++Program 19 Chapter Summary 32 Practice Programs 35 Answers to Self-Test Exercises 33 Programming Projects 36
Introduction to Computers and C++ Programming 1.1 Computer Systems 2 Hardware 2 Software 7 High-Level Languages 8 Compilers 9 History Note 12 1.2 PROGRAMMING AND PROBLEM-SOLVING 12 Algorithms 12 Program Design 15 Object-Oriented Programming 16 The Software Life Cycle 17 1.3 Introduction to C++ 18 Origins of the C++ Language 18 A Sample C++ Program 19 Pitfall: Using the Wrong Slash in \n 23 Programming Tip: Input and Output Syntax 23 Layout of a Simple C++ Program 24 Pitfall: Putting a Space Before the include File Name 26 Compiling and Running a C++ Program 26 Pitfall: Compiling a C++11 Program 27 Programming Tip: Getting Your Program to Run 27 1.4 Testing and Debugging 29 Kinds of Program Errors 30 Pitfall: Assuming Your Program Is Correct 31 1 Chapter Summary 32 Answers to Self-Test Exercises 33 Practice Programs 35 Programming Projects 36
The whole of the development and operation of analysis are now capable of being executed by machinery....As soon as an Analytical Engine exists,it will necessarily guide the future course of science. CHARLES BABBAGE(1792-1871) INTRODUCTION In this chapter we describe the basic components of a computer,as well as the basic technique for designing and writing a program.We then show you a sample C++program and describe how it works. 1.1 COMPUTER SYSTEMS A set of instructions for a computer to follow is called a program.The collection of programs used by a computer is referred to as the software for that computer.The actual physical machines that make up a computer installation are referred to as hardware.As we will see,the hardware for a computer is conceptually very simple.However,computers now come with a large array of software to aid in the task of programming.This software includes editors,translators,and managers of various sorts.The resulting environment is a complicated and powerful system.In this book we are concerned almost exclusively with software,but a brief overview of how the hardware is organized will be useful. Hardware There are three main classes of computers:PCs,workstations,and mainframes. A PC(personal computer)is a relatively small computer designed to be used by one person at a time.Most home computers are PCs,but PCs are also widely used in business,industry,and science.A workstation is essentially a larger and more powerful PC.You can think of it as an "industrial-strength" PC.A mainframe is an even larger computer that typically requires some support staff and generally is shared by more than one user.The distinctions between PCs,workstations,and mainframes are not precise,but the terms are commonly used and do convey some very general information about a computer. A network consists of a number of computers connected so that they may share resources such as printers and may share information.A network might contain a number of workstations and one or more mainframes,as well as shared devices such as printers. For our purposes in learning programming,it will not matter whether you are working on a PC,a mainframe,or a workstation.The basic configuration of the computer,as we will view it,is the same for all three types of computers. 2
Introduction In this chapter we describe the basic components of a computer, as well as the basic technique for designing and writing a program. We then show you a sample C++ program and describe how it works. 1.1 Computer Systems A set of instructions for a computer to follow is called a program. The collection of programs used by a computer is referred to as the software for that computer. The actual physical machines that make up a computer installation are referred to as hardware. As we will see, the hardware for a computer is conceptually very simple. However, computers now come with a large array of software to aid in the task of programming. This software includes editors, translators, and managers of various sorts. The resulting environment is a complicated and powerful system. In this book we are concerned almost exclusively with software, but a brief overview of how the hardware is organized will be useful. Hardware There are three main classes of computers: PCs, workstations, and mainframes. A PC (personal computer) is a relatively small computer designed to be used by one person at a time. Most home computers are PCs, but PCs are also widely used in business, industry, and science. A workstation is essentially a larger and more powerful PC. You can think of it as an “industrial-strength” PC. A mainframe is an even larger computer that typically requires some support staff and generally is shared by more than one user. The distinctions between PCs, workstations, and mainframes are not precise, but the terms are commonly used and do convey some very general information about a computer. A network consists of a number of computers connected so that they may share resources such as printers and may share information. A network might contain a number of workstations and one or more mainframes, as well as shared devices such as printers. For our purposes in learning programming, it will not matter whether you are working on a PC, a mainframe, or a workstation. The basic configuration of the computer, as we will view it, is the same for all three types of computers. 2 The whole of the development and operation of analysis are now capable of being executed by machinery. . . . As soon as an Analytical Engine exists, it will necessarily guide the future course of science. Charles Babbage (1792–1871)
1.1 Computer Systems 3 The hardware for most computer systems is organized as shown in Display 1.1.The computer can be thought of as having five main components: the input device(s),the output device(s),the processor (also called the CPU, for central processing unit),the main memory,and the secondary memory.The processor,main memory,and secondary memory are normally housed in a single cabinet.The processor and main memory form the heart of a computer and can be thought of as an integrated unit.Other components connect to the main memory and operate under the direction of the processor.The arrows in Display 1.1 indicate the direction of information flow. An input device is any device that allows a person to communicate information to the computer.Your primary input devices are likely to be a keyboard and a mouse. An output device is anything that allows the computer to communicate information to you.The most common output device is a display screen, referred to as a monitor.Quite often,there is more than one output device.For example,in addition to the monitor,your computer probably is connected to a printer for producing output on paper.The keyboard and monitor are sometimes thought of as a single unit called a terminal. DISPLAY 1.1 Main Components of a Computer Processor(CPU) Input Output device(s) Main memory device(s) Secondary memory
1.1 Computer Systems 3 The hardware for most computer systems is organized as shown in Display 1.1. The computer can be thought of as having five main components: the input device(s), the output device(s), the processor (also called the CPU, for central processing unit), the main memory, and the secondary memory. The processor, main memory, and secondary memory are normally housed in a single cabinet. The processor and main memory form the heart of a computer and can be thought of as an integrated unit. Other components connect to the main memory and operate under the direction of the processor. The arrows in Display 1.1 indicate the direction of information flow. An input device is any device that allows a person to communicate information to the computer. Your primary input devices are likely to be a keyboard and a mouse. An output device is anything that allows the computer to communicate information to you. The most common output device is a display screen, referred to as a monitor. Quite often, there is more than one output device. For example, in addition to the monitor, your computer probably is connected to a printer for producing output on paper. The keyboard and monitor are sometimes thought of as a single unit called a terminal. Display 1.1 Main Components of a Computer Main memory Processor (CPU) Secondary memory Input device(s) Output device(s)