A History Lesson 3
In parallel with the standardization of C was an effort by Bjarne Stroustrup to create a
more object-oriented approach to using C. Eventually C++ emerged, and then eventually C++
underwent a standardization process.
C++ should be viewed as a completely new language, based largely on C. It includes mod-
ern constructs to support object-oriented programming. Several design issues have greatly influ-
enced C++.
First and foremost, C++ was intended to be upward compatible with C, as much as possi-
ble. Thus legal C programs should be legal C++ programs, though this is not 100% true. (Obvi-
ous exceptions include C programs that use as identifiers any reserved word that was added to
C++, such as class.) As a result, numerous constructs from C that perhaps we could live with-
out are part of C++, as is the entire idea of using pointers to do arrays. Additionally, because
each new reserved word breaks existing code, the C++ community is reluctant to add new
reserved words, preferring instead to overuse existing words, such as const and static. So
whereas Java has throw and throws, in C++ we simply use throw both to throw an excep-
tion and to list exceptions.
The second crucial philosophy of C++ was in part political: C++ programs should be as
fast as C programs if they do the same thing. Otherwise, it was thought, nobody would want to
learn a more complicated language, just to write slower code. As part of this philosophy, in C++,
if you do not use a feature, you should not have to pay a cost a runtime. Among other things, this
still means, no runtime bounds checks, no dynamic dispatch by default, and no garbage collec-
tion.
The third thing to know about C++ is that it has continually evolved. The first widely-used
version of C++ supported classes and inheritance. Templates, discussed in Chapter 7, were
added later. Exceptions were added even later. At another time came namespaces. The templates
got fancier, with templates inside of templates. This has lead to compilers being perennially
behind the official specification, buggy with new features, and especially bug-prone when sev-
eral new features are used at the same time, much to the chagrin of programmers and textbook
authors. Fortunately, C++ has more or less stabilized and the latest compilers are quite good at
meeting (and in some cases doing more than required by) the language specification.
Java was developed by Sun Microsystems, and marketed as a modern alternative to C++.
Thus, cosmetically, there are many similarities. However, the designers of Java had (at least)
two main goals. First, they attempted to allow the programmer to write the same set of programs
that C++ allows, using simpler syntax. In cases where C++ has several ways to do the same
thing, Java attempts to pick the idiom most widely endorsed by the C++ community and support
it with a language feature. In cases where C++ features are abused, Java does not allow the fea-
ture. Thus some C++ features are not found in Java. Second, they have attempted to make it
harder for incorrect code to run, requiring the compiler to detect many sets of errors when it first
compiles the program, and then the Virtual Machine to throw an exception when bad things hap-
pen at runtime. By detecting most occurrences of bad behavior, not only are honest program-
c++book.mif Page 3 Saturday, July 12, 2003 10:53 AM