Chapter 1 Introduction to Computers and C++ Programming
This chaper discusses what computers are, how they work and how they are programmed. It introduces the notion of structured programming and explains why these techniques have fostered a revolution in the way programs are written. The chapter gives a brief history of the development of programming languages from machine languages, to assembly languages, to high-level languages. The origin of the C++ programming language is discussed. The chapter introduces a typical C++ programming environment
and gives a concise introduction to writing C++ programs. A detailed treatment of decision making and arithmetic operations in C++ is presented. We have introduced a new, more open, easier to read "look and feel" for our C++ source programs, most notably using syntax coloring to highlight keywords, comments and regular program text and to make programs more readable. We have also introduced a new background highlight to focus readers' attention on the new features presented in each program. After studying this chapter, the student will understand how to write simple, but complete, C++ programs. We discuss the explosion of interest in the Internet
that has occurred with the advent of the World Wide Web
. We discuss namespaces and the using
statements. Readers plunge right into object-orientation in the "Thinking About Objects" section that introduces the basic terminology of object technology
Chapter 2 Control Structures
Introduces the notion of algorithms (procedures) for solving problems. It explains the importance of using control structures effectively in producing programs that are understandable, debuggable, maintainable and more likely to work properly on the first try. It introduces the sequence structure, selection structures (if
) and repetition structures (while
). It examines repetition in detail and compares counter-controlled loops and sentinel-controlled loops. It explains the technique of top-down, stepwise refinement that is critical to the production of properly structured programs and presents the popular program design aid, pseudocode. The methods and approaches used in Chapter are applicable to effective use of control structures in any programming language
(not just C++). This chapter helps the student develop good programming habits in preparation for dealing with the more substantial programming tasks in the remainder of the text. The chapter concludes with a discussion of logical operators—&&
(or) and !
(not). We introduce the static_cast
operator, which is safer than using the old-style casting C++ inherited from C. We added the "Peter Minuit" exercise, so students can see the wonders of compound interest—with the computer
doing most of the work! We discuss the scoping rules for loop counters in for
-loops. In the optional "Thinking About Objects" section, we begin the first phase of an object-oriented
design (OOD) for the elevator simulator—identifying the classes needed to implement the simulator. We also introduce the UML use case, class and object diagrams and discuss the concepts of associations, multiplicity, composition, roles and links.
Chapter 3 Functions
Discusses the design and construction of program modules. C++'s function-related capabilities include standard-library functions, programmer-defined functions, recursion, call-by-value and call-by-reference capabilities. The techniques presented in Chapter are essential to the production of properly structured programs, especially the kinds of larger programs and software
that system programmers and application programmers are likely to develop in real-world applications. The "divide and conquer" strategy is presented as an effective means for solving complex problems by dividing them into simpler interacting components. Students enjoy the treatment of random numbers and simulation, and they appreciate the discussion of the dice game of craps, which makes elegant use of control structures. The chapter offers a solid introduction to recursion and includes a table summarizing the dozens of recursion examples and exercises distributed throughout the remainder of the book. Some texts leave recursion for a chapter late in the book; we feel this topic is best covered gradually throughout the text. The extensive collection of 60 exercises at the end of the chapter includes several classic recursion problems such as the Towers of Hanoi. The chapter discusses the so-called "C++ enhancements to C," including inline
functions, reference parameters, default arguments, the unary scope resolution operator, function overloading and function templates. The header files table introduces many of the header files that the reader will use throughout the book. In the optional "Thinking About Objects" section, we determine many of the class attributes needed to implement the elevator simulator. We also introduce the UML statechart and activity diagrams and the concepts of events and actions as they relate to these diagrams.
Chapter 4 Arrays
Discusses the structuring of data into arrays, or groups, of related data items of the same type. The chapter presents numerous examples of both single-subscripted arrays and double-subscripted arrays. It is widely recognized that structuring data properly is just as important as using control structures effectively in the development of properly structured programs. Examples in the chapter investigate various common array manipulations, printing histograms, sorting data, passing arrays to functions and an introduction to the field of survey data analysis (with simple statistics). A feature of this chapter is the discussion of elementary sorting and searching techniques and the presentation of binary searching as a dramatic improvement over linear searching. The 121 end-of-chapter exercises include a variety of interesting and challenging problems, such as improved sorting techniques, the design of a simple airline-reservations system, an introduction to the concept of turtle graphics (made famous in the LOGO language) and the Knight's Tour and Eight Queens problems that introduce the notion of heuristic programming so widely employed in the field of artificial intelligence. The exercises conclude with many recursion problems including the selection sort, palindromes, linear search, binary search, the Eight Queens, printing an array, printing a string backwards and finding the minimum value in an array. This chapter still uses C-style arrays, which, as you will see in Chapter , are really pointers to the array contents in memory. In the "Thinking About Objects" section of Chapter , we determine many of the operations (behaviors) of the classes in the elevator simulation. We also introduce the UML sequence diagram and the concept of messages that objects can send to each other.
Page 1 | 2