Book Review
C++ for Java™ Programmers

Review

2 stars

An easy to read book written for someone who has one or more years experience of Java and who wants to learn C++. As the author points out, a book of its size can not be a complete and thorough introduction to C++. The book goes through the differences between the two languages and provides some simple examples, throughout the main text and concludes with four case studies, see chapters 14 to 17. Each chapter ends with a short quiz to test your understanding.

However the code examples are rather bitty and don't really show the reader how all the concepts in C++ fit together. Also it is not clear if the code examples comprise a working a working program. Another drawback is that the example code is presented within the describing text, and there is no attempt to identify the files the code should be put in. As the author informs in chapter 5, classes are defined in multiple files in C++, a novice Java programmer needs assistance to work out how to organise their class files, and how they should be subsequently linked. This book does not provide this information at all. For the code for case study 3, it is difficult to work out how the GUI should be implemented.

Although chapter 16 uses the Microsoft Foundation Classes (MFC) for the graphical interface, the book does not appear to use any code that is compiler/vendor specific. However I have not used any code in the book, so we can not confirm this.

At the end of the day, you are probably better off buying a book solely on C++ and learning the language from scratch rather than purchasing this book.
4 Sep 2001

Brief Description of Contents

Chap. 1.

Basic Philosophical Differences

The chapter describes the history, philosophy and background of C++, enabling the Java programmer to understand C++.
Chap. 2.

Fundamental Data Types

Discusses the fundamental data types, including;

  • integers (characters, booleans, bit fields)
  • floating point values
  • enumerated values
  • the void type
  • arrays
  • structures and unions
  • object values
  • functions (order of argument evaluation, the function main, alternative main entry points)
Chap. 3.

Pointers and References

While pointers are hidden to the Java programmer, the use of pointers in C++ is explicit and direct and the language provides a variety of different mechanisms for manipulating pointer values. The chapter covers;

  • Java Pointers
  • a review of Pointers
  • the address-of operator, &
  • pointers to simple values, (to pointers, to const, void * pointers, to functions)
  • pointers to structures
  • pointers to arrays
  • references, (pass by reference parameters, references as results)
Chap. 4.

Memory Management

In Java the task of memory management is conducted pretty much in the background. However in C++ the programmer is responsible for memory management, which leads to a variety of errors that the Java programmer may not be aware of. The chapter covers;

  • the memory model
  • stack resident memory values, (lifetime errors, size errors (array allocations, the slicing problem))
  • Heap-Resident Memory Values, (encapsulated memory management, reference counts)
Chap. 5.

Class Definition

Although both Java and C++ both use classes, this chapter introduces the many differences in the way that classes are implemented in the two languages.

  • obvious similarities and minor differences
  • separation of class implementation, (interface and implementation files, the inline directive, prototypes, external declarations)
  • forward references
  • constructors and initialisation, (default and copy constructors, initialisers, order of initialization, combining constructors)
  • the orthodox Canonical Class Form
  • visibility modifiers
  • inner classes verses nested classes
  • static initialisation
  • final classes
Chap. 6.

Polymorphism

Covers the differences between polymorphism in Java and C++.

  • virtual and nonvirtual overriding, (impact of virtual on size, obtaining type information from a dynamic value)
  • abstract classes
  • downcasting (reverse polymorphism), introduces dynamic_cast from RTTI as safer cast than the standard unchecked cast, (simulating the dynamic cast)
  • name resolution,
  • a forest, not a tree, explains that classes in C++ are not part of a single hierarchy, as in Java were all new classes are derived from the class Object
  • virtual destructors
  • private inheritance
  • inheritance and arrays
  • overloading
Chap. 7.

Operator Overloading

Something new to the Java programmer, C++ provides the ability to overload operators. The chapter describes the benefits and dangers of doing this.

  • overloaded functions or methods
  • the simple binary operators
  • the comparison operators
  • the increment and decrement operators
  • the shift operators
  • the assignment operator
  • the compound assignment operators
  • the address-of operator
  • the logical connectives
  • the comma operator
  • the arrow operator
  • pointers to members and pointers to member functions
  • conversion operators
  • memory management operators
  • disallowing operating
  • implicit functions and invocations, (implicitly created operations, implicit functions invocations)
Chap. 8.

Characters and Strings

  • Characters and literal strings, (cctype library and cstring)
  • constants and mutable values
  • the string data type
  • example program to split a line into words
Chap. 9.

Templates and Containers

The template mechanism in C++ is one of the more complex features in the language that has no correspondence in Java. A template allows a class or function to be parameterised by values or types. With the mechanism being performed statically at compile time this permits a great deal of type checking to be performed then rather than at run-time. This eliminates many of the performance reducing run-time casts typically required in Java programs.

A major use of templates has been as a tool to develop a rich set of data structures, containers and abstractions, part of which have become the Standard Template Library (STL), which is the primary data structure library used in C++ programs.

  • template classes, (template methods)
  • template functions
  • the Standard Template Library, (containers, iterators, generic algorithms, function objects)
Chap. 10.

Input/Output

The techniques used for I/O in Java are based on those developed in C++. The chapter covers the differences between the two languages, as well as the complications due to the two competing I/O systems in C++, the stdio inherited from C, and the newer C++ specific library called the Stream I/O Library.

  • The stdio Library, (formatted output)
  • The Stream I/O Facility, (stream input, string streams, file streams)
Chap. 11.

Exception Handling

Unlike Java, exception handling is a relatively new addition to C++. Before its introduction C++ programmers used a variety of ways to handling abnormal situations. So in addition to knowing the C++ exception handling mechanism, the Java programmer must know the alternatives available.

  • flags and return codes
  • the assertion library
  • the setjmp and longjmp facility
  • signals
  • exception handling, (exception types, rethrowing exceptions, no finally clause, references as exceptions, exception class clonability, no need to document exceptions, standard exceptions)
Chap. 12.

Features Found Only in C++

This section summarises the features introduced in earlier chapters that have no equivalent in Java, as well as describing some of the remaining features that are only found in C++ that have not been discussed.

  • global variables
  • the preprocessor
  • typedef statement
  • the const keyword
  • default arguments
  • friends
  • name spaces
  • multiple inheritance
  • goto statement
Chap. 13.

Features Found Only in Java

However, the number of features in Java that have no C++ equivalent are relatively small. Most have been highlighted in the book's earlier chapters and the most notable remaining ones are listed in this chapter.

  • wrapper classes
  • interfaces
  • inline classes
  • threads
  • reflection
Chap. 14.

Case Study - Fractions

A case study to illustrate the use of constructors, overloaded operators, conversion operators, assignment and the stream style of input and output, through the creation of a data abstraction of an integer fraction, or rational number. The aim is to create a data abstraction that operates as much as possible like the primitive arithmetic types such as float or integer.

Chap. 15.

Case Study - Containers

Illustrates the use of several of the container classes provides as part of the STL using two examples. The first is an algorithm for computing the shortest path between pairs of points. The second involves scanning a text document and developing a concordance.

Chap. 16.

Case Study - A Card Game

This case study examines a simple version of the card game solitaire. Covered at the creation of a general purpose, reusable containers, the implementation of the game and an introduction to the Microsoft Foundation Classes (MFC) for the graphical interface.

Note,
The application presented uses the merest fraction of the functionality provided by the MFC. The author recommends
MFC Programming from the Ground Up, Herbert Schidt, McGraw-Hill, New York, 1996 as a good introduction.

Chap. 17.

Case Study - Combining Separate Classes

A common problem encountered by programmers is how to combine elements from two or more classes when they are not permitted to make changes to the original classes, for example, the original classes might be distributed in binary form from separate vendors. However the programmer wishes to maintain these classes in a common representation and perform common tasks on them.

Because the classes are distributed in binary form it is not possible to a new member function, but this chapter describes how to write new ordinary functions that take their respective types as arguments.

Appendix A - Include Files

Describes the include header files that are provided as part of the C++ standard, based on the primary categories of services provided.

Back