Applications Programming in C++
PrefaceApplications Programming in C++ is based on C++ courses given by the authors at DePaul University over the last several years.  We assume no prior knowledge of C++, but we do assume programming experience in some high-level language.  We provide Chapter 0 as background for those with modest programming experience.  More advanced users can begin with Chapter 1.  The book can be used for a one-term course in applications programming in C++ or for self-study.The book and its supplements---an Instructor's Guide and a World Wide Web site---provide a comprehensive support system to help the reader master C++.  The book includes numerous examples, exercises, sample applications, programming exercises, lists of common programming errors, and illustrations.OverviewDuring the 1980s and early 1990s, C became the language of choice for many applications and systems programmers.  Most major software available for personal computers was written in C: spreadsheets, word processors, databases, communications packages, statistical software, graphics packages, and so on.  Virtually all software written for the UNIX environment was likewise written in C, and many mainframe systems meant to be ported from one platform to another were also coded in C.  In the early 1980s, Bjarne Stroustrup of AT\&T; Bell Labs developed C++ as an extension of C that supports object-oriented programming, a type of programming that is well suited to the large, complex software systems now written for all platforms, from the cheapest personal computers to the most expensive mainframes.  C++ also corrects some shortcomings in C, which C++ includes as a subset, and it supports abstract data types and generic functions through templates.C++ is a highly complex language.  Fortunately, most C++ programmers can benefit from its power without mastering each and every one of its features.  Because this is an introductory book, we focus on the most useful aspects of the language; but we place some of the more esoteric and specialized parts of the language in end-of-chapter sections labeled C++ Postscript.  We focus on using C++ to write practical programs based on sound design techniques, rather than on tricks and surprises in C++.This book includes the following features:Examples and exercises that cover a wide range of applications.Motivating real-world applications.A broad variety of programming exercises.  The book contains over 160 programming exercises.Sections on common programming errors.Discussion of standard C++ functions and methods.Exercises at the ends of sections so that readers can check their mastery of the sections.The book contains nearly 600 such exercises.  Answers to the odd-numbered section exercises are given in the back of the book, and answers to the even-numbered section exercises are given in the Instructor's Guide. Illustrations to facilitate the learning process.The latest changes to the C++ language including new-style headers, new-style casts, type, namespaces, and the namespace.A discussion of algorithms (Section 0.1).Coverage of computer systems (Section 0.2).An overview of programming languages (Section 0.4).A discussion of program development including program specification, algorithm design, coding, and testing (Section 0.5).Problem-solving strategies (Section 0.5).Early coverage of files (Section 2.5).A thorough discussion of recursion (Section 3.8).Coverage of STL (the Standard Template Library) (Chapter 10).A number of appendices.Topics grouped according to their use and their relationships to one another.  This organization enables readers to write simple but useful programs immediately and to skip or postpone some of the less often used and more esoteric parts of the language.Understandable code.  We have opted for clarity rather than subterfuges based on obscure C++ features.C++: A Hybrid LanguageC++ is essentially C with extensions to support object-oriented programming.  Because C supports traditional, procedural programming, C++ is a hybrid language; it supports both procedural and object-oriented programming.  It is a challenge to teach both procedural and object-oriented programming in the same course and to develop pedagogical strategies to address both programming paradigms.  This book reflects our belief, based on our experience teaching C++, that some procedural programming is necessary before moving on to the object-oriented parts of C++.  Thus we first cover basic control structures (e.g., loops, if statements), data types, operators, arrays, and functions.  Once the basics of C++ as a procedural language have been covered, we introduce classes, inheritance, and polymorphism---the key concepts in object-oriented programming.  We then turn to operator overloading, storage, templates, and other C++ features.  This approach is justified because:It is pedagogically sound to begin with less abstract topics (e.g., basic control structures) before moving up to more abstract topics (e.g., classes and inheritance).All but the most trivial classes need basic control structures, data types, operators, arrays, and functions.  Even if the programming paradigm is "object-oriented,"' implementation of classes requires basic control structures, data types, operators, arrays, and functions.Organization of the BookChapter 0 serves as an introduction to computer systems and program development.  (This chapter can be skipped if this material is already known.) We begin in Section 0.1 by discussing algorithms. Section 0.2 presents an overview of computer systems including hardware (main memory, CPU, etc.) and software (text editors, compilers, etc.).  Internal representations of integers, floating-point numbers, characters, and instructions are discussed in Section 0.3.  Section 0.4 continues with a discussion of programming languages (assembly languages and high-level languages).  Section 0.5 summarizes the phases of program development: program specification, algorithm design, coding, and testing.  In addition, several problem-solving strategies are given in Section 0.5.  A discussion of the C++ language (Section 0.6) concludes Chapter 0.Chapters 1 through 4 are devoted to the basics of C++ as a procedural language.  In Chapter 1 we discuss integer and floating-point data types; identifiers; and arithmetic, relational, and logical operators. After Chapter~1, readers can immediately begin writing simple, but useful, programs.In Chapter 2, we discuss the  if-else statement; while, do while, and for loops; files; the assignment, increment, and decrement operators; the break, continue, and switch statements; promotions and casts; and basic formatting. Because C++ programs compiled under Windows are best used with disk input and output files, we treat files early.Functions and program structure are discussed in Chapter 3. Parameters and call by value are covered in Sections 3.1 through 3.3. Call by reference is the topic of Section 3.5.  Function overloading is introduced in Section 3.7.  Recursion is the topic of Section 3.8.  Sections 3.4 and 3.6 are devoted to sample applications.Chapter 4 is devoted to arrays.  Pointers and character strings are introduced to underscore their connection with arrays.After covering the basics of C++ as a procedural language, we introduce the object-oriented features: classes, inheritance, and polymorphism.  We cover class basics in Chapter 5: classes and objects, data members and methods, constructors and the destructor, defining classes and using them in programs, and using existing class libraries.Chapter 6 introduces inheritance, including protected members, and constructors and the destructor under inheritance.Polymorphism (run-time binding) is the topic of Chapter 7.  Also, in this chapter, we discuss the difference between run-time binding and features that resemble it. This chapter also explains how an abstract base class can be used to specify an interface and emphasizes the importance of interfaces in the object-oriented model.Chapter 8 considers operator overloading for classes, including overloading operators as either methods or top-level functions. This chapter abounds with examples to show how operators may be overloaded to advantage.Storage is the topic of Chapter 9.  Compile-time and run-time storage are discussed in detail, as are the main C++ storage classes: auto, static, and extern.Chapter 10 shows how C++ supports generic functions through templates.  This chapter also introduces STL (Standard Template Library) and offers short programs to illustrate STL containers, iterators, algorithms, and function objects.In each of Chapters 1--10, several sections are devoted to sample applications.  Each of these sections contains a statement of a problem, sample input and output, a solution to the problem, and a well-documented implementation of the problem in C++.  Most of these sections conclude with an extended discussion.  In some of the examples, these sections include a line-by-line discussion of the C++ program.The sample applications include the following:Statistics (Section 2.9).Simulation (the Monty Hall problem) (Section 3.4).Counting votes (Section 4.4).A task class (Section 5.6).Tracking films (Sections 6.3 and 7.2).An associative array class (Section 8.7).Sorting and searching (Section 9.4).A template stack class (Section 10.2).Stock performance reports using STL (Section 10.5).The C++ Postscript sections discuss less-used parts of the language and give additional technical details about certain parts of the language.The Common Programming Errors sections highlight those aspects of the language that are easily misunderstood.The bookcontains over 160 programming exercises drawn from a wide variety of applications.ExamplesThe book contains over 350 numbered examples, which clarify particular facets of C++ for the reader and show the purpose of various C++ features.  A box \outlinediamond\ marks the end of each example.ExercisesThe book contains nearly 600 section review exercises, the answers to which are true or false, short answers, code segments, and, in a few cases, entire programs.  These exercises are suitable as homework problems or as self-tests.  The answers to the odd-numbered exercises are given in the back of the book, and the answers to the even-numbered exercises are given in the Instructor's Guide.Class testing this book has convinced us of the importance of these exercises.The applications covered in the programming exercises at the ends of the chapters include the following:Vote fraud (Programming Exercise 2.12)Weight lifting (Programming Exercise 2.13)Chaos (Programming Exercise 2.14)Simulation (Programming Exercises 3.15 and 5.14)Game of {\it Life} (Programming Exercise 4.18)Digital images (Programming Exercise 4.20)Data structures (Programming Exercises 5.8, 5.9, and 9.2 through 9.7)Databases (Programming Exercise 5.15)Scheduling (Programming Exercises 5.18 and 10.14)Windows environments (Programming Exercise 7.8)Dating services (Programming Exercise 7.10)Software engineering (Programming Exercise 9.1)World Wide Web (Programming Exercise 10.12)Not every reader will be interested in all of these applications; however, we think that it is important to show the variety of problems that C++ can address.AppendicesFive appendices are provided for reference.  Appendix A contains the ASCII table.  Appendix B explains the C++ preprocessor.  Appendix C contains a list of some of the most useful C++ functions and class methods.  We describe the parameters and return values for each, the header file to include, and what the function or method does.  Appendix D covers run-time type identification (RTTI), and Appendix E covers exception handling.Recent Language ChangesWe have incorporated the latest changes to the C++ language including:The logical type.New-style headers.Namespaces and the namespace.New-style casts.STL (Standard Template Library).Exception handling.Run-time type identification.Namespaces and the namespace.The operator.We use these new features throughout the book, but for those readers using older compilers, we also explain how to convert to the old-style C++.Before the logical type was introduced, integer types were used for false (zero) and true (nonzero).New-style headers drop the extension and prefix to the header files acquired from C.  For example, the old C++ header file becomes, and the C header file becomes.  Standard features furnished by these header files are now grouped under the name ( standard).  The technical term for a region in which such features are grouped is namespace.Acknowledgments We thank the following reviewers: Ed Angel, University of New Mexico; Karen Bernstein, DePaul University; Ralph Ewton, University of Texas at El Paso; Mike Gearen, Punahou High School; Jeanine Ingber, University of New Mexico; and Bina Ramamurthy, SUNY at Buffalo. We are grateful to our friendly copy editor, Patricia Johnsonbaugh, for smoothing our prose and for deleting words and statements we drafted but should not have.Our student, Wen Tao Liu, helped us check most of the major code segments.We are indebted to the School of Computer Science, Telecommunications and Information Systems at DePaul University and its dean, Helmut Epp, for providing time and encouragement for the development of this book.We received consistent support from the people at Prentice Hall.  Special thanks go to Alan Apt, publisher, and Laura Steele, acquisitions editor.R.J. M.K.