C++ How to Program, 4/e Tour of the Book (Continued)
Chapter 5 Pointers and Strings
Presents one of the most powerful and difficult-to-master features of the C++ languagepointers. The chapter provides detailed explanations of pointer operators, call by reference, pointer expressions, pointer arithmetic, the relationship between pointers and arrays, arrays of pointers and pointers to functions. There is an intimate relationship between pointers, arrays and strings in C++, so we introduce basic string-manipulation concepts and discuss of some of the most popular string-handling functions, such as getline (input a line of text), strcpy and strncpy (copy a string), strcat and strncat (concatenate two strings), strcmp and strncmp (compare two strings), strtok ("tokenize" a string into its pieces) and strlen (compute the length of a string). The 134 chapter exercises include a simulation of the classic race between the tortoise and the hare, card-shuffling and dealing algorithms, recursive quicksort and recursive maze traversals. A special section entitled "Building Your Own Computer" also is included. This section explains machine-language programming and proceeds with a project involving the design and implementation of a computer simulator that allows the reader to write and run machine-language programs. This unique feature of the text will be especially useful to the reader who wants to understand how computers really work. Our students enjoy this project and often implement substantial enhancements, many of which are suggested in the exercises. In Chapter , another special section guides the reader through building a compiler; the machine language produced by the compiler then is executed on the machine language simulator produced in the Chapter exercises. Information is communicated from the compiler to the simulator in sequential files, which we discuss in Chapter . A second special section includes challenging string-manipulation exercises related to text analysis, word processing, printing dates in various formats, check protection, writing the word equivalent of a check amount, Morse Code and metric-to-English conversions. The reader will want to revisit these string-manipulation exercises after studying class string in Chapter . Many people find that the topic of pointers is, by far, the most difficult part of an introductory programming course. In C and "raw C++" arrays and strings are pointers to array and string contents in memory (even function names are pointers). Studying this chapter carefully should reward you with a deep understanding of the complex topic of pointers. Again, we cover arrays and strings as full-fledged objects later in the book. In Chapter , we use operator overloading to craft customized Array and String classes. Chapter also introduces Standard Library classes string and vector for manipulating strings and arrays as objects. These classes are explained in detail in Chapter and Chapter , respectively. Chapter is loaded with challenging exercises. Please be sure to try the Special Section: Building Your Own Computer. In the "Thinking About Objects" section, we determine many of the collaborations (interactions among objects in the system) needed to implement the elevator system and represent these collaborations using the UML collaboration diagram. We also include a bibliography and a list of Internet and World Wide Web resources that contain the UML specifications and other reference materials, general resources, tutorials, FAQs, articles, whitepapers and software.
Chapter 6 Classes and Data Abstraction
Begins our discussion of object-based programming. The chapter represents a wonderful opportunity for teaching data abstraction the "right way"through a language (C++) expressly devoted to implementing abstract data types (ADTs). In recent years, data abstraction has become a major topic in introductory computing courses. Include a solid treatment of data abstraction. Chapter discusses implementing ADTs as C++-style classes and why this approach is superior to using structs, accessing class members, separating interface from implementation, using access functions and utility functions, initializing objects with constructors, destroying objects with destructors, assignment by default memberwise copy and software reusability. The chapter exercises challenge the student to develop classes for complex numbers, rational numbers, times, dates, rectangles, huge integers and playing tic-tac-toe. Students generally enjoy game-playing programs. Mathematically inclined readers will enjoy the exercises on creating class Complex (for complex numbers), class Rational (for rational numbers) and class HugeInteger (for arbitrarily large integers). The "Thinking About Objects" section asks you to write a class header file for each of the classes in your elevator simulator. In the "Thinking About Objects" section, we use the UML class diagram developed in previous sections to outline the C++ header files that define our classes. We also introduce the concept of handles to objects, and we begin to study how to implement handles in C++.
Chapter 7 Classes Part II
Continues the study of classes and data abstraction. The chapter discusses declaring and using constant objects, constant member functions, compositionthe process of building classes that have objects of other classes as members, friend functions and friend classes that have special access rights to the private and protected members of classes, the this pointer, which enables an object to know its own address, dynamic memory allocation, static class members for containing and manipulating class-wide data, examples of popular abstract data types (arrays, strings and queues), container classes and iterators. The chapter exercises ask the student to develop a savings-account class and a class for holding sets of integers. In our discussion of const objects, we briefly mention keyword mutable which, as we will see in Chapter , is used in a subtle manner to enable modification of "non-visible" implementation in const objects. We discuss dynamic memory allocation using new and delete. When new fails, the program terminates by default because new "throws an exception" in standard C++. Chapter discusses catching and handling exceptions. We motivate the discussion of static class members with a video-game-based example. We emphasize how important it is to hide implementation details from clients of a class; then, we show private data in our class headers, which certainly reveals implementation. We discuss proxy classes, which provide a means of hiding implementation from clients of a class. The "Thinking About Objects" section asks you to incorporate dynamic memory management and composition into your elevator simulator. Students will enjoy the exercise creating class IntegerSet. This motivates the treatment of operator overloading in Chapter . In the "Thinking About Objects" section, we present a complete elevator simulator C++ program (approximately 1,250 lines of code) and a detailed code walkthrough. The code follows directly from the UML-based design created in previous sections and employs our good programming practices, including the proper use of static and const data members and functions. We also discuss dynamic-memory allocation, composition and object interaction via handles, and how to use forward declarations to avoid the "circular-include" problem.
Chapter 8 Operator Overloading; String and Array Objects
Presents one of the most popular topics in our C++ courses. Students really enjoy this material. They find it a perfect match with the discussion of abstract data types in Chapter and Chapter . Operator overloading enables the programmer to tell the compiler how to use existing operators with objects of new types. C++ already knows how to use these operators with objects of built-in types, such as integers, floats and characters. But suppose that we create a new string classwhat would the plus sign mean when used between string objects? Many programmers use plus with strings to mean concatenation. In Chapter , the programmer will learn how to "overload" the plus sign, so when it is written between two string objects in an expression, the compiler will generate a function call to an "operator function" that will concatenate the two strings. The chapter discusses the fundamentals of operator overloading, restrictions in operator overloading, overloading with class member functions vs. with nonmember functions, overloading unary and binary operators and converting between types. A feature of the chapter is the collection of substantial case studies including an array class, a string class, a date class, a huge integer class and a complex numbers class (the last two appear with full source code in the exercises). Mathematically inclined students will enjoy creating the polynomial class in the exercises. This material is different from what you do in most programming languages and courses. Operator overloading is a complex topic, but an enriching one. Using operator overloading wisely helps you add that extra "polish" to your classes. The discussions of class Array and class String are particularly valuable to students who will go on to use the standard library classes string and vector, which are introduced with test programs that use string and vector to mimic the capabilities shown in the String and Array examples. Introducing string and vector here gives students valuable experience with software reuse by using existing classes, rather than "reinventing the wheel." It is possible to craft a Date class that, if we had been using it for the last two decades, could easily have eliminated a major portion of the so-called "Year 2000 (or Y2K) Problem." The exercises encourage the student to add operator overloading to classes Complex, Rational and HugeInteger to enable convenient manipulation of objects of these classes with operator symbolsas in mathematicsrather than with function calls as the student did in the Chapter exercises.