This appendix explains how to transfer your Java programming skills to a substantial subset of C++. This is necessary for students who take their first programming course in Java and the second course in C++. Naturally, it would be easiest if the second course were also offered in Java, but learning to move from one language to another is a fact of life for today's software professionals. Fortunately, C++ has many features in common with Java, and it is easy for a Java programmer to gain a working knowledge of C++. Nevertheless, C++ is a much more complex language than Java. This appendix does not attempt to cover all features of C++. But if you master all of the constructs described in this appendix, you will be able to use C++ effectively..
We only cover the differences between Java and C++. In particular, control flow (if, while, for) is essentially identical in C++ and Java, and it is not covered here.
This appendix describes ANSI C++. Some older C++ compilers lack essential features described here. To use those compilers, you will need to learn more about the parts of C++ that were inherited from C. That is beyond the scope of this discussion.
A3.1. Data Types and Variables
The data types in C++ are similar to those in Java. Like Java, C++ has int and double types. However, the range of the numeric types such as int is machine-dependent. On 16-bit systems such as PCs running DOS or Windows 3.x, int are 2-byte quantities with a much more limited range than the 4-byte Java int type. On those machines, you need to switch to long whenever the int range is not sufficient.
C++ has short and unsigned types that can store numbers more efficiently. It is best to avoid these types unless the added efficiency is crucial.
The Boolean type is called bool in C++.
The C++ string type is called string. It is quite similar to the Java String type. However, pay attention to these differences:
1. C++ strings store ASCII characters, not Unicode characters
2. C++ strings can be modified, whereas Java strings are immutable.
3. The substring operation in C++ is called substr. The command s.substr(i, n) extracts a substring of length n starting at position i.
4. You can only concatenate strings with other strings, not with arbitrary objects.
5. To compare strings, use the relational operators == != < <= > >=. The last four operators perform lexicographic comparison. This is actually more convenient than the use of equals and compareTo in Java.
A3.2. Variables and Constants
In C++, local variables are defined just as in Java.
int n = 5;
There is, however, a major difference between C++ and Java. The C++ compiler does not check whether all local variables are initalized before they are read. It is quite easy to forget initializing a variable in C++. The value of the variable is then the random bit pattern that happened to be in the memory location that the local variable occupies. This is clearly a fertile source of programming errors.
As in Java, classes can have data fields and static variables. Furthermore, variables can be declared outside functions and classes. These so-called global variables can be accessed from any function in a program. That makes them difficult to manage. C++ programs should avoid global variables.
In C++, constants can be declared anywhere. (Recall that in ... Read more

