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.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.
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.
Needs to be: bool
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.
Needs to be: in the source file containing main().
Needs to be: *p = 7; //*p is lvalue of a, so a is assigned 7
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.
Needs to be:
cd.suit pc -> s spades deck[0].p deck -> p 5 (*pc).suit pc -> s spades
Needs to be: y = x.d.x;
Needs to be: "reversal"
Needs to be: "exceeds"
Explanation: No this order was intentional to demonstrate the possible machine dependency in using union.
Needs to be: The concept of struct is augmented in C++ to allow public and private members.
Needs to be: A data member that is declared static ...
Needs to be : int str::how_many = 0;
Needs to be: X::c
Needs to be: "occurs"
Needs to be: int k = (rand() % (52 - i));
Needs to be: a space
Explanation: No. Because the constructor does a modulo 60 operation, b.v ends up being 1, not 61.
Needs to be: a default constructor, array allocation causes an error.
Needs to be: stack (const stack& str); //copy constructor
Needs to be: //delete old string
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); }
Needs to be: assert(n > 0)
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);
Needs to be: ... , add a default
Needs to be: int ub() const
Needs to be: strcpy(p, s)
Needs to be: { this -> tick(); return *this; }
Needs to be: unsigned long
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."
Needs to be: return *this
Explanation: It is the style of this book to not use parentheses with a simple return argument.
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);
Needs to be: return *this
Explanation: It is the style of this book to not use parentheses with a simple return argument.
Needs to be: { this -> tick(); return *this; }
Needs to be: vect_bnd::vect_bnd(int lb, int ub) :
Needs to be: virtual void foo(int); virtual void foo(double); and in line 23 void foo(int);
Needs to be: class under_grad
Needs to be: class grad
Needs to be: init(even);
Needs to be: char str1[100], str2[100], ch;
Needs to be: The benefits of this parameterization include allocation off the stack
Needs to be: return *this;
Needs to be: The generic tree type gen_tree...
Needs to be: while (cin >> dat) {
Needs to be: for (int i = 0; i < how_many - 2; ++i)
Needs to be: ! ~
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*.
Needs to be: a = static_cast < peer > (frog);
Needs to be: int good():
Needs to be: operator void*() const;