C++ for C Programmers,
Second Edition

by Ira Pohl  

Addison-Wesley ISBN 0-8053-3159-X

C++ for C Programmers, Second Edition was published in 1994 and reflects the language standard for that general time frame. Material on STL, namespaces and other features that were added after 1994 are not included in this version of the text. The book is now available in its Third Edition. If you own this Second Edition text, and want to get the information on changes to C++, my C++ Distilled Book which is around $20.00 is meant to be an informal reference - with selected tips and examples to the new ANSI standard.

The C++ code examples in the book are available in several forms. The code is arranged in directories corresponding to the chapters of the book. To get to the unpacked version for any system, or to get individual program files, you can use the FTP directory (www.cse.ucsc.edu/~pohl/CPP2/). Files are also available in zip form for Windows users and compressed tar form for UNIX users.


Additional C++ Code Examples

Ira has provided additional code to demonstrate dynamic cast, complete clock program, multiple inheritance, mutable members, string constructors, and rvalue vs. lvalue.


Errata

Caveats: Some standard libraries may have been modified since this book was written and the function prototypes may differ from what is available on your compiler. Check your compiler vendor for the correct prototypes.

Notations: p425 means page 425  
                            +8 means 8 lines from top 
                             -7 means 7 lines from bottom

If you encounter errata in this book not listed here, please contact Ira Pohl via email at pohl@cse.ucsc.edu with your errata. You will be cited in the correction if you are the first to report it.

Acknowledgements: Special thanks to Sharon Harvey of Normandale Community College and David Hiebeler of Cornell University.


p54 (Sharon Harvey) line -11: double (*)[N]q; is incorrect

Needs to be: double (*q)[N]


p60 (David Hiebeler) line 77: //definition has names

Needs to be: //definition has no names


p68 (Sharon Harvey) line 3: Anonymous unions must be static

Needs to be: static union {


p73 (Sharon Harvey) line 9-11: suit is incorrect. Use s by itself in the table entries.

Needs to be:

cd.s              p -> s         spades
deck[0].pips      deck -> pips   5
(*p).s            p -> s         spades


p75 (David Hiebeler) line 1: enum boolean {false, true} doesn't work on some compilers

Explanation: Since the second edition was written, three new keywords have been added to compilers: bool, true, and false. When the third edition is published in 1998, it will use the built-in bool type rather than the enumerated boolean type used in edition 2. The code in C++ Distilled also reflects the newer style of using bool.


p80 (Jason Christensen) line 10: The first sentence in the Access:private and public section should not refer only to functions.

Needs to be: The concept of struct is augmented in C++ to allow public and private members.


p82 (Sharon Harvey) line 10:the first code fragment is missing << "i" after the imag

Needs to be: { cout << real << " + " << "i "; }


p83 (Sharon Harvey) line -3: boolean str::how_many = 0;

Needs to be: int str::how_many = 0;


p86 (Sharon Harvey) line 2 X::Y::c is incorrect.

Needs to be: X::c


p105 (Sharon Harvey) line -8: Unneeded semicolon at the end of the function header for the stack constructor.

Needs to be: :max_len(str.max_len, top(str.top)


p106 (Sharon Harvey) line -16: missing semicolon on constructor

Needs to be: stack (const stack& str); //copy constructor


p112 and 114 (David Hiebeler) line 10: The temporary character array in the concat() function is referenced as t in the code instead of temp as it should be. It isn't really needed however. The function can be better coded without it.

Needs to be:

void string::concat(const string& a, const string& b)
{
   len = a.len  + b.len;
   delete []s;
   s = new char [len + 1];
   strcpy(s, a.s);
   strcat(s, b.s);
}


p112 and 113 (Sharon Harvey) line 6: Comment for retrieve old string is incorrect. It should indicate string is to be deleted.

Needs to be: //delete old string


p117 (Sharon Harvey) line -4: Assertion assert( n < 1) is incorrect.

Needs to be: assert(n > 0)


p118 (David Hiebeler) line -8: Since the change in the rule on for-loop scoping of variables declared in the for-statement, some compilers will find i not defined.

Explanation: This rule has changed. Depending on your compiler, you may want to declare i either outside both for loops, or in each for loop.


p118 (Robert Morris) line -12 : the calls to a_w_h.element(i) are incomplete.

Needs to be:

for (int i = 0; i <= a_w_h.a.ub(); ++i) {
   a_w_h.a.element(i) = 21 + i;
   a_w_h.b.element(i) = 135 + i;
   a_w_h.c.element(i) = 62 + i;
}
for (i = 0; i <= a_w_h.a.ub(); ++i) {
   cout << a_w_h.a.element(i) << " years ";
   cout << a_w_h.b.element(i) << " pounds ";
   cout << a_w_h.c.element(i) << " inches\n";
}


p120 (Sharon Harvey) line 17:The del() function is a member function of list.

Needs to be: void list::del()


p123 (Terrell Koken) line 17: Use multidimensional instead of higher-dimensional to talk about arrays of more than one dimension.

Needs to be: Standard C does not have authentic multidimensional arrays. .... can implement flexible, safe, dynamic multidimensional arrays.


p124 (David Hiebeler) line -8: The delete p[i] statement should be changed so that it conforms to the delete syntax for arrays allocated by new[].

Needs to be: delete []p[i]


p130 (David Hiebeler) line 4-10: The string type and str_obj types are confused here. We can't use b.len and s[i] and s to get at the variables. You need to use b.st to access them. The code

 for (i = 0; i <= b.len; ++i)
    if (c == s[i])
       break;
substring.assign(s + i);
Needs to be:

for (i = 0; i <= b.st - len; ++i)
    if (c == b.st - s[i])
       break;
substring.assign(b.st - s + i);


p137 (Robert Morris) line 14: The member function retrieve() probably induces the student to invoke vect::element(), but that is not a const function, whereas retrieve() is. The g++ compiler enforces the guarantee and will not compile a version of retrieve based on element() as declared in the vect class in the text.

Needs to be: void retrieve(int ind, int& i, int& j, int& k);


p137 (Robert Morris) line 16: The member function int ub const ( return (size - 1); } has ( where it should have {

Needs to be: int ub const { return (size - 1); }


p139 (Don Gillies) line 13 : The u_add() and u_sub() functions should not be constant.

Needs to be:

void u_add(int i) {t |= masks[i]; }
void u_sub(int i) {t &= ~masks[i]; }


p144 (Terrell Koken) line 4: void* pointers are discussed in Chapter 2, not Chapter 4.

Needs to be: As we mentioned in Chapter 2, ...


p146 (Sharon Harvey) line 4: The strcpy(s, p) has the argument order reversed.

Needs to be: strcpy(p, s)


p152 (David Hiebeler) line -1: Inefficiently is incorrect.

Needs to be: inefficient.


p155 (Terrell Koken) line 8: Section 9.9 of Appendix C should reference only Section 9.

Needs to be: (see Apeendix C, Section 9),


p158 (Terrell Koken) line 13: Use addition instead of adding

Needs to be: Let us create an addition operation ...


p160 (David Hiebeler) line 14: The vect example is found in Section 4.7. where ub() was already a member function. Section 4.4

Needs to be: Section 4.7 and two sentences in the final paragraph on page 160 need to be removed. They are "The public data member ub is changed to a member function. This prevents a user from inadvertently introducing a program error by modifying the member."


p161 (Terrell Koken) line : The vect operator= needs to be in the class

Needs to be:

class vect {
   ...
   int& operator[](int i) const ;   //range checked element
   vect& operator=(const vect& v);  //assignment
   ...


p162 (Sharon Harvey) line 20: The class-name should reference instead the type of the array element.

Needs to be: element-type& operator[]( integral-type);


p181 (Sharon Harvey) line -7: The print function is overridden, not overloaded, since both functions have the same signature.

Needs to be: The print member functions are overridden.


p183 (Terrell Koken) line -6: Need plural verb for temperatures ... are instead of is

Needs to be: temperatures representing water in its liquid range are 32-212.


p. 185 (David Hiebeler) line 9: The colon is missing before the initializer list.

Needs to be: vect_bnd::vect_bnd(int lb, int ub) :


p187 (Terrell Koken) line 2: data needs to be in the code font.

Needs to be: The pointer data matches ...


p. 199 (David Hiebeler) line 17: The init(odd); call should be removed. The first call to update(odd, even) creates (inits) odd, when no dele(odd) has yet been called. The code as is will leave a little pile of lost heap memory lying around.Change the line with init(odd); init(even);

Needs to be: init(even);


p221 (Terrell Koken) line -9: Missing declaration for ch

Needs to be: char str1[100], str2[100], ch;


p224 (Jason Christensen) line 8: allocation is from the stack (or off the stack), not of the stack.

Needs to be: The benefits of this parameterization include allocation off the stack...


p229 (Jason Christensen) line 1: gentree should be gen_tree

Needs to be: The generic tree type gen_tree...


p231 (Jason Christensen) line 19: Bad spacing around cin and {.

Needs to be: while (cin >> dat) {


p237 (Sharon Harvey) line -2: Exercise 11 is incomplete

Needs to be: exercise 11 should be removed.


p251 (Terrell Koken) line -1: Change call to calls

Needs to be: each of these calls the ....