C++ for Fortran Programmers

by Ira Pohl

Addison-Wesley ISBN 0-201-92483-8

Using your existing knowledge of Fortran, C++ for Fortran Programmers gets you up and running with C++ quickly. By showing how individual elements of a Fortran program compare and translate into C++, this book helps you make a smooth transition to C++ and object-oriented concepts. Best-selling author and C++ authority Ira Pohl uses his trademark "dissection" technique to illustrate the underlying structure of programs and to help you understand design trade-offs. Scientific and engineering coding examples are featured throughout the text.

Features

You can buy this book online through Barnes and Noble.

If you are not conversant in Fortran or C, 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/FORTRAN/). 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.


p5 (Roy North) line 8: Fortran Program needs to use CALL and RETURN and should have subroutine first in standard Fortran.

Needs to be:

      SUBROUTINE PRMSG (STR)

      CHARACTER * (*) STR
      IF (LEN(STR) .LT. 1) THEN
         PRINT *, 'Hello World!'
      ELSE
         PRINT *, STR
      END IF
      RETURN
      END
      
      PROGRAM WORLD
    
      CALL PRMSG('')
      END


p129 (Jason Christensen) line 5: extra parentheses are not needed.

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


p131 (Jason Christensen) line -1: stack needs to be ch_stack

Needs to be: const ch_stack::int max_len; //declaration required


p157 (Jason Christensen) line 18: The stack.h file contains the class ch_stack

Needs to be: In file ch_stack4.h


p160 (Sharon Harvey) line -13: missing semicolon on constructor and use of stack should be ch_stack

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


p176 (Terrell Koken) line 2-4: 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.


p177 (David Hiebeler) line 4: 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]

The above correction has been made in later printings


p189 (Debra Dolsberry) line 14: stack needs to be ch_stack

Needs to be: explicit ch_stack(int n); //not used for conversion


p. 205 (Paul Sevinc) line -8: page 176,) (has unneeded comma)

Needs to be: page 176)

The above correction has been made in later printings


p. 212 (Paul Sevinc) line 5: unsigned int

Needs to be: unsigned long

The above correction has been made in later printings


p213 (David Hiebeler) line 18-20: 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."


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

Needs to be:

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


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

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


p. 215 (Paul Sevinc) line -8: return (*this)

Needs to be: return *this

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

The above correction has been made in later printings


p218 (C. L. Tondo) line 9: Function missing semicolon

Needs to be: friend istream& operator >>(istream& in, ratiional& x);


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

Needs to be: return *this;


p223 (Jason Christensen) line 23: It is the style of the book not to parenthesize simple arguments such as ptr

Needs to be: return ptr;


p. 223 (Brian Suchomel) The program triple.cpp is not in the downloadable program set from the website.

Explanation: This program has been added to the website.


p. 228 (Brian Suchomel) The program over_new.cpp is not complete in the downloadable program set from the website.

Explanation: This program was erroneously a copy of new_hdler.cpp. The over_new.cpp file has been corrected to flesh out the example code as found on page 228.


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

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


p. 246 (Paul Sevinc) line 3: j != 0

Needs to be: j >= 0

The above correction has been made in later printings


p248 (Jason Christensen) line 16: p + size is incorrect; need bp + size instead

Needs to be: This is why the member function vector::end() returns bp + size.


p277 (Jason Christensen) line -9: 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


p279 (Jason Christensen) line -5: It is the style of the book not to parenthesize simple arguments such as p[i]

Needs to be: return p[i];


p282 (Jason Christensen) line -14: It is the style of the book not to parenthesize simple arguments such as front

Needs to be: return front;


p284 (Jason Christensen) line 13: gentree should be gen_tree

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


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

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


p. 301 (Paul Sevinc) line -4, -7: Class (uppercase incorrect)

Needs to be: class

The above correction has been made in later printings


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

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


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


p. 337 (Paul Sevinc) line 1: class plans

Needs to be: class student_worker

The above correction has been made in later printings


p. 337 (Paul Sevinc) line 20: public virtual person

Needs to be: virtual public person

Explanation: While public virtual and virtual public are both allowed interchangeably, the preferred style is virtual public.

The above correction has been made in later printings


p. 338 (Paul Sevinc) line 1: public virtual student

Needs to be: virtual public person

Explanation: While public virtual and virtual public are both allowed interchangeably, the preferred style is virtual public. The class which is inherited from was incorrect and needs to be person, not student.

The above correction has been made in later printings


p. 338 (Paul Sevinc) line 8: class::student and class::worker

Needs to be: class student and class worker

The above correction has been made in later printings


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

Needs to be: ! ~


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

Needs to be: ! ~


p409 (C. L. Tondo) line -6: BOOLEAN needs semicolon for consistency with the other typedefs

Needs to be: typedef int BOOLEAN;


p424 (C. L. Tondo) line (in table): int not needed before b.

Needs to be: int a = -5, b = 3, c = 0;

p452 (C. L. Tondo) line 4-5: In keeping with general style of using const for arguments which won't be changed, the constructors should use const

Needs to be:

 dbl_vect_bnd(const dbl_vect_bnd& v);       //copy constructor
 dbl_vect_bnd(const dbl_vect& v);           //conversion constructor
.....
//conversion constructor
dbl_vect_bnd::dbl_vect_bnd(const dbl_vect& v) :
          dbl_vect(v), l_bnd(0), u_bnd(size - 1) { }

//copy constructor
dbl_vect_bnd::dbl_vect_bnd(const dbl_vect_bnd& v) :
          dbl_vect(v), l_bnd(v.l_bnd), u_bnd(v.u_bnd) { }


p. 455 (Jeffrey Peden) line 2-6: 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*.

The above correction has been made in later printings


p. 495 (Paul Sevinc) line 15: Class (uppercase incorrect)

Needs to be: class

The above correction has been made in later printings


p482 (C. L. Tondo) line -11: Semicolon missing after function

Needs to be: int good():


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

Needs to be: operator void*() const;


p. 499 (Paul Sevinc) line 7, 10, 14: Class (uppercase incorrect)

Needs to be: class

The above correction has been made in later printings


p. 509-513(author) multiple lines and table entries. The string STL library has been modified since this text was originally written. A new example showing the STL string constructors has also been developed. The changes are listed below.

p509 line 2: seven constructors

Needs to be: six constructors

p509 line 3-4: Two conversions were removed from the standard. Delete sentence "Two of these ..."

String Constructor Members table entries 3 and 5: Two conversions were removed from the standard. Delete

string(const vector...)
string(size_t size, ...)

String Constructor Members table: One conversion was added to the standard. Insert in place of string(const vector...)

string(InputIterator b, InputIterator e) constructor from the InputInterator range b to e

Last Item in String Constructor Members table has order of arguments reversed. string(char c, size_t n = 1) is incorrect and cannot have the default argument.

Needs to be: string(size_t n, char c)

Third table entry from bottom of String overloaded Operator Members, operator vector< ... should be removed

order of arguments reversed. append(char c, size_t n = 1) is incorrect and cannot have the default argument.

Needs to be: append(size_t n, char c)

order of arguments reversed. assign(char c, size_t n = 1) is incorrect and cannot have the default argument.

Needs to be: assign(size_t n, char c)

order of arguments reversed. insert(char c, size_t n = 1) is incorrect and cannot have the default argument.

Needs to be: insert(size_t n, char c)

needs another assign at end of assigns list

Needs to be: string& assign(InputIterator b, InputIterator e);

needs 3 more inserts at end of inserts list:

iterator insert(iterator p, char c);
iterator insert(iterator p, size_t n, char c);
void insert(iterator p, InputIterator b, InputIterator e);

Second table entry in String Members table has string& replace(pos, n, c, rep = 1); The rep argument has been removed.

Needs to be: string& rplace(pos, n, c); and the comment "repeated rep times" needs to be removed.

Third table entry from bottom in String Members table has comment returns the private member res; the first function resets this which needs to be modified.

Needs to be: allocates memory for string; returns the size of the allocation

Third and fourth table entries in String Members table and their explanations should be removed. The two items for removal are: char get_at(pos) const; and void put_at(pos, c);

int compare (const char* p, size_t pos) const which needs a default value on last argument.

Needs to be: int compare (const char* p, size_t pos = 0) const

int compare (char c, size_t pos, size_t rep = 1) const;) which must be removed altogether.

The above correction has been made in later printings