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]
//definition
has names
Needs to be: //definition has no names
static
Needs to be: static union {
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
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.
<< "i"
after the imag
Needs to be: { cout << real << " + " <<
"i "; }
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
Needs to be: :max_len(str.max_len, top(str.top)
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)
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.
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"; }
del()
function is a member function of list.
Needs to be: void list::del()
Needs to be: Standard C does not have authentic multidimensional arrays. .... can implement flexible, safe, dynamic multidimensional arrays.
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]
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]; }
void*
pointers
are discussed in Chapter 2, not Chapter 4.
Needs to be: As we mentioned in Chapter 2, ...
strcpy(s,
p)
has the argument order reversed.
Needs to be: strcpy(p, s)
Needs to be: inefficient.
Needs to be: (see Apeendix C, Section 9),
Needs to be: Let us create an addition operation ...
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."
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 ...
Needs to be: element-type& operator[](
integral-type);
print
function is overridden, not overloaded, since both functions have the same signature.
Needs to be: The print
member functions
are overridden.
Needs to be: temperatures representing water in its liquid range are 32-212.
Needs to be: vect_bnd::vect_bnd(int lb, int ub) :
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);
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...
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.
Needs to be: each of these calls the ....