C++ For Pascal Programmers, Second Edition

by Ira Pohl


Addison-Wesley isbn 0-8053-3158-1

In a newly revised second edition, Ira Pohl has created a seamless transition to C++ by using Pascal constructs to build a knowledge of programming in C++. He begins by introducing readers to C and then focuses on C++ as a better C. Using numerous exercises and frequent language comparisons, he teaches concepts quickly and C++ Standard, the second edition covers the latest language features including detailed discussions of templates and exception handling.

Features


You can buy this book online through Barnes and Noble.

If you are not conversant in C or Pascal, then the book C++ by Dissection is an excellent start for the professional programmer who wants to master C++ for use in an object-oriented environment. No knowledge of C is required. The author presents instruction in C++ using object-oriented methodology throughout.

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/PASCAL/). 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

Some of these errata have been corrected in later printings. A note with the approximate month for the correction is at the end of any corrections which may already have been made.

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.

Acknowledgments: Thanks to Professor Robin Hill for giving me a first detailed list of errata.
p37 (Author) Simple Types bool and wchar_t

Explanation: bool wchar_t are now firmly established in most C++ compilers. These are ANSI C++ standard native types. See C++ Distilled for details of their use.


p43 (Hill) M_TO_K and m_to_k confusion.

Explanation: m_to_k lower case identifiers follow C and C++ conventions. M_TO_K used in the Pascal program followed Pascal conventions for constant identifiers.


p48 (Hill) line -13 book

Needs to be: bool


p65 (Hill) Exercise 8 the last subexpression ((j/m) || q) causes a divide by zero.

Explanation: This was deliberate though the reader should have been warned or asked about this expression separately. In the previous statement the subexpression (q || (j / m)) is okay because of short-circuit evaluation. This part of the exercise is meant to demonstrate this distinction.


p79 (Hill) line -8 ...in main().

Needs to be: in the source file containing main().


p93 (Hill) line 18 comment is confusing

Needs to be: *p = 7; //*p is lvalue of a, so a is assigned 7


p. 99 (Paul Sevinc) change a single #define ...

Needs to be: change a single const declaration to process ...

Explanation: #define was a carry over from C style use of the preprocessor. Unlike C, C++ allows such const declarations to be used to declare array sizes.


p122 (Jason Christensen) line in table: suit should be just s

Needs to be:

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


p. 138 (Paul Sevinc) y = x.d.i;

Needs to be: y = x.d.x;


p116 (Hill) line -2 "reveral"

Needs to be: "reversal"


p117 (Hill) line 8 "exceed"

Needs to be: "exceeds"


p135 (Hill) line -1, -2 should be interchanged?

Explanation: No this order was intentional to demonstrate the possible machine dependency in using union.


p143 (Jason Christensen) line 8: 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.


p146 (Jason Christensen) line 6: declare should be declared

Needs to be: A data member that is declared static ...


p146 (Sharon Harvey) line -3 boolean str::how_many ...

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


p149 (Hill) line 15 X::Y::c is incorrect.

Needs to be: X::c


p151 (Hill) line -2 "occur"

Needs to be: "occurs"


p151 (Jason Christensen) line 18: extra parentheses are not needed.

Needs to be: int k = (rand() % (52 - i));


p153 (Hill) line -3 Negated existential quantifier

Needs to be: a space


p167 (Hill) line -10 "b.v = 1;" shouldn't this be "b.v = 61;" ?

Explanation: No. Because the constructor does a modulo 60 operation, b.v ends up being 1, not 61.


p168 (Hill) line -5 need a comma after constructor

Needs to be: a default constructor, array allocation causes an error.


p172 (Sharon Harvey) line 8: missing semicolon on constructor

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


p179 and 181 (Sharon Harvey) line 11: Comment for retrieve old string is incorrect. It should indicate string is to be deleted.

Needs to be: //delete old string


p179 and 181 (David Hiebeler) line 10: The temporary character array in the concat() function is not really needed. 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);
}


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

Needs to be: assert(n > 0)


p192 (David Hiebeler) line 15: 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]


p197 (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);


p. 206 (Brian Suchomel) line 8 ... , and include a default

Needs to be: ... , add a default


p207 (author) exercise 11 int ub const

Needs to be: int ub() const


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

Needs to be: strcpy(p, s)


p225 (Jason Christensen) line 6: It is the style of the book not to parenthesize simple arguments such as *this

Needs to be: { this -> tick(); return *this; }


p. 228 (Paul Sevinc) line 13: unsigned int

Needs to be: unsigned long


p229 (David Hiebeler) line 21-23: The vect example had ub() as a member function.

Needs to be: Two sentences in the second paragraph on page 226 need to be removed. They are "The public data member ub is changed to a member function. This prevents a user from inadvertently intoducing a program error by modifying the member."


p. 231 (Paul Sevinc) line 5: return (*this)

Needs to be: return *this

Explanation: It is the style of this book to not use parentheses with a simple return argument.


p230 (Terrell Koken) line 10: 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
   ...


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

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


p. 232 (Paul Sevinc) line -2: return (*this)

Needs to be: return *this

Explanation: It is the style of this book to not use parentheses with a simple return argument.


p235 (Jason Christensen) line -4: It is the style of the book not to parenthesize simple arguments such as *this

Needs to be: { this -> tick(); return *this; }


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

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


p262 (Hill) line 18, 19, 23 foo() should have explicit return type.

Needs to be: virtual void foo(int); virtual void foo(double); and in line 23 void foo(int);


p. 272 (Paul Sevinc) line -1: class::under_grad

Needs to be: class under_grad


p. 273 (Paul Sevinc) line 1: class::grad

Needs to be: class grad


p. 269 (David Hiebeler) line -4: 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);


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

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


p296 (Jason Christensen) line -10: 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


p300 (Jason Christensen) line 9: It is the style of the book not to parenthesize simple arguments such as *this

Needs to be: return *this;


p301 (Jason Christensen) line -2: gentree should be gen_tree

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


p304 (Jason Christensen) line 8: Bad spacing around cin and {.

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


p311 (Jason Christensen) line 7: Bad spacing on int i= 0

Needs to be: for (int i = 0; i < how_many - 2; ++i)


p349 (Stuart Reges) line 9: Two entries in the table are too close together. !~ should be separated by space

Needs to be: ! ~


p. 434 (Jeffrey Peden) lines 10-13: Base* and Derived should be switched.

Needs to be:

class Base { .....};
class Derived : Base { ..... };

void fcn(Base* ptr)
{
   Derived* bptr = dynamic_cast <Derived*>(ptr);

In this example, the case converts the pointer value ptr to a Derived*.


p436 (author) line -1 static_cast < peer > (b) = frog;

Needs to be: a = static_cast < peer > (frog);


p454 (C. L. Tondo) line -13: Semicolon missing after function

Needs to be: int good():


p454 (C. L. Tondo) line -4: Missing semicolon, parenthesis and const from void

Needs to be: operator void*() const;