Errata for "Practical C++ Programing" Steve Oualline 3/14/96 Preface ======= Page xvii, paragraph 2: "Simple Classes". The last line should be "that can be performed" instead of "that can be preformed." Chapter 1 ========= Page 3. First paragraph of "A Brief History of C++" "(The name came about because C was superseded by the old programming language they were using called B)" should read "(The name came about because C was preceded by the old programming language they were using called B) Chapter 2 ========= Page 14. In the paragraph before the warning "MS-DOS" is typeset in one font and a different one is used in the last line of the warning. Page 16. Add after the first paragraph on "Creating a Program Using an Integrated Development Environment". Compiler makers tend to make minor changes in their Integrated Development Environment from one version of the compiler to the other. The exact keystrokes presented here may not work if your version of the compiler is different from the one used in the book. However the major concepts are still the same. Chapter 3 ========= Page 40. In the paragraph under "Revision History", the word UNIX is typeset in one font while in the box another typeface is used for the same word. Page 45, 70% down, paragraph beginning "C++ statements should not." "avoided.If" should be "avoided. If" Chapter 4. ========== Page 50. In the basic program structure: "data declarations" should be in italics. Page 50, second to last paragraph. "Example 3-3" should be "Example 3-2". Page 52, middle of the page: "Example 4-2" should be "Example 4-1". Page 60, add just before "Programing Exercises" "bool" Type The C++ Draft Standard defines a boolean type: bool that can have the value true or false. Most compilers do not yet support this new type, so we will not discuss it here. Instead it can be found in Chapter 28, C++'s Dustier Corners under the section "Vampire Features". Chapter 5 ========= Page 65, Second paragraph "double quotes (")" should be "double quotes(")". Page 65, third paragraph, last line. "variable name to "Sam" you..." should be "variable name to "Sam" you..." Page 70, 40% down: "a place for the `\0' character" should be "a place for the '\0' character" Page 73, Example 5-8. Change main() { to int main() { ch = 37; Page 75, second example: "// Max. number of elements in the total list" should be on one line. Page 79, Before Side Effects #1. "value of 6" should be "value of 5". Page 82, Exercise 5-4. "miles per hour to kilometers per hour" should be "kilometers per hour to miles per hour". Page 83, Answer 5-1 should start: #include int height; /* the height of the triangle ----------------------------- int width; /* the width of the triangle */ -------------------------------------------------- int area; /* area of the triangle (computed) */ main() (See Practical C Answer 4-3 for a good version of this answer.) Page 83, Answer 5-2 should be Answer 5-3. The actual answer for Question 5-2 is: The problem is with the way we specified the element of the array: "array[2,4]". This should have been written: "array[2][4]". The reason that the specification "array[2,4]" does not generate a syntax error is that it is legal (but strange) C++. There is a comma operator in C++ (See C++'s Darker Corners) so the expression "2,4" evaluates to "4". So "array[2,4]" is the same as "array[4]". C++ treats this as a pointer (See Simple pointers) and written shows up as a memory address. Chapter 6 ========= Page 90, first line of the figure should be: cout << current_number << '\n'; Page 94, Exercise 6-1 should read Write a program to find the square of the distance between two points. Find the distance only if you want to do the independent research needed to perform a square root in C++. Chapter 7 ========= Page 98, the last paragraph should be justified. Page 100. The title to the preliminary specification should be centered and the entire preliminary specification should be set in courier. Chapter 9 ========= Page 139, 40% down: But since this is a reference, you can use it on the left side as well. should be But since this is a reference, you can use it on the right side as well. Page 143, code examples void draw(const rectangle &rectangle; double scale) should be: void draw(const rectangle &rectangle, double scale) also void draw(const rectangle &rectangle; double scale = 1.0) should be: void draw(const rectangle &rectangle, double scale = 1.0) Chapter 10 ========== Page 157, last paragraph "The directive #ifdef" should be "The directive #ifndef" Page 160, just before the middle #ifdef _CONST_H_INCLUDED_ should be: #ifndef _CONST_H_INCLUDED_ Page 163, Exercise 10-1 should read: Exercise 10-1. The C++ standard contains a boolean type (bool) that defines the values "true" and "false". The problem is most compilers haven't implemented this type yet. Create a boolean type by using #define to define values for BOOLEAN, TRUE, and FALSE. Chapter 11 ========== Page 181, The program for Question 11-2 should be: Example 11-3 high/high.cc #include const int HIGH_SPEED = (1<<7); /* modem is running fast */ // we are using a hardwired connection const int DIRECT_CONNECT = (1<<8); char flags = 0; // start with nothing main() { flags |= HIGH_SPEED; // we are running fast flags |= DIRECT_CONNECT;// because we are wired together if ((flags & HIGH_SPEED) != 0) cout <<"High speed set\n"; if ((flags & DIRECT_CONNECT) != 0) cout <<"Direct connect set\n"; return (0); } Page 181, Exercise 11-1. "Example 11-1" should be "Example 11-2". Chapter 12 ========== Page 192, 75% down: "apply to 3 to 3 + 5" should be "apply to 3 or 3 + 5". Page 196, Exercise 12-1 should read: Exercise 12-1: Design a data structure to handle the data for a mailing list. Chapter 13 ========== Page 209, First box in the figure: "stack::~stack" is called at the end of the function. The arrow should point to the closing brace. Page 209, Figure in the middle of the page. The line for "use_stack(a_stack) should be shorter. It should not go into the middle of the box. Page 215, Exercise 13-4. The comments for the member functions should line up. Page 216: Exercise 13-5 should be: Exercise 13-5 I have a simple method of learning foreign vocabulary words. I write the words down on a list of flash cards. I then go through the stack of flash cards one at a time. If I get a word right, that card is discarded. If I get it wrong, the card goes to the back of the stack. Write a class to implement this system. Member functions: struct single_card { char question[40]; // English version of the word char answer[40]; // Other language version of the word }; // Constructor -- takes a list of cards to // initialize the flash card stack void flash_card::flash_card(single_card list[]); // Get the next card const single_card &flash_card::get_card(void); // The student got the current card right void flash_card::right(void); // The student got the current card wrong void flash_card::wrong(void); // Returns 1 -- done / 0 -- more to do int done(void); Note: In the book the code lines wrapped in three places inappropriately. Chapter 15 ========== Page 230, The B caption for Figure 15-2 should read: B) other = *thing_ptr; Page 232, the section on "Contant Pointer" should read: const Pointers ============== Declaring constant pointers is a little tricky. For example, although the declaration: const int result = 5; tells C++ that result is a constant so that result = 10; // Illegal is illegal. The declaration: const char *answer_ptr = "Forty-Two"; does not tell C++ that the variable answer_ptr is a constant. Instead it tells C++ that the data pointed to by answer_ptr is a constant. The data can not be changed, but the ponter can. Again we need to make sure we know the difference between "things" and "pointers to things" What's answer_ptr? A pointer. Can it be changed? Yes it's just a pointer. What does it point to? A const char array. Can the data pointed to by answer_ptr be changed? No, it's constant. In C++ this is: answer_ptr = "Fifty-One"; // Legal (answer_ptr is a ariable) *answer_ptr = `X'; // Illegal (*answer_ptr is a constant) If we put the const after the * we tell C++ that the pointer is constant. For example: char *const name_ptr = "Test"; What's name_ptr? A constant pointer. Can it be changed? No. What does it point to? A character. Can the data we pointed to by name_ptr be change? Yes. name_ptr = "New"; // Illegal (name_ptr is constant) *name_ptr = `B'; // Legal (*name_ptr is a char) Finally we can put const in both places creating a pointer that can not be changed to a data item that can not be changed. const char *const title_ptr = "Title"; Page 243, the first line of the print program (a line of /*****) is missing. Chapter 16 ========== Page 257, Table. "ios::showpos" should be in courier. Page 257, The code fragment should read: number = 0x3FF; cout << "Dec: " << number << '\n'; cout.setf(ios::hex); cout << "Hex: " << number << '\n'; cout.setf(ios::dec); Page 257, The example output should read: Dec: 1023 Hex: 3ff Page 277, first code fragment should read: if (fread((char *)&rectangle, 1, sizeof(rectangle), in_file) != sizeof(rectangle)) { Chapter 18 ========== Page 341, Exercise 18-1 should be: Exercise 18-1: Write a class to handle fractions such as "1/3". Define addition, subtraction, multiplication, and division operators for these fractions. For example 1/3 + 1/2 = 5/6. Chapter 20 ========== Page 355, code example. "value" and "next_pointer" should both be indented 4 spaces. Page 363, Code example: list *before_ptr; list *after_ptr; Should be item *before_ptr; item *after_ptr; Page 363, Code example, after the while: insert_ptr = before_ptr; should be: before_ptr = insert_ptr; Chapter 21 ========== Page 382, Figure 21-1 should show a box names "stack" taped to a box named "b_stack." Page 397, Question 21-1. "line 17" should be "line 20". Page 399, Exercise 21-1: "checkbook class of Example 13-2" should be "checkbook class of Exercise 13-2." "queue class of Example 13-2" should be "queue class of Exercise 13-2." Chapter 22 ========== Page 410, Exercise 22-1. "Example 13-2" should be "Exercise 13-3" Page 410, Exercise 22-2. "Example 18-3" should be "Exercise 18-3" Page 410, Exercise 22-3. "Example 13-2" should be "Exercise 13-2" Chapter 24 ========== Page 438, "Generated code" The generated functions "int max(int d1, int d2)" and "float max(float d1, float d2)" are reversed. Page 446, Exercise 24-3 "Example 13-2" should be "Exercise 13-4." Chapter 27 ========== Page 491, Figure 27-1. "longjump" should be "longjmp" Index ===== Page 555, The entry "stanard libraries" should "standard libraries." Back Cover ========== Last sentence: remove Macintosh compilers.