Concepts of Programming Languages

(Sean Pound) #1

548 Chapter 12 Support for Object-Oriented Programming


and the derivation access controls, and also the possibility of friend functions
and classes, the C++ programmer has highly detailed control over the access to
class members. Although C++ provides multiple inheritance and Smalltalk does
not, there are many who feel that is not an advantage for C++. The downsides
of multiple inheritance weigh heavily against its value. In fact, C++ is the only
language discussed in this chapter that supports multiple inheritance. On the
other hand, languages that provide alternatives to multiple inheritance, such as
Objective-C, Java, and C#, clearly have an advantage over Smalltalk in that area.
In C++, the programmer can specify whether static binding or dynamic
binding is to be used. Because static binding is faster, this is an advantage for
those situations where dynamic binding is not necessary. Furthermore, even
the dynamic binding in C++ is fast when compared with that of Smalltalk.
Binding a virtual member function call in C++ to a function definition has a
fixed cost, regardless of how distant in the inheritance hierarchy the definition
appears. Calls to virtual functions require only five more memory references
than statically bound calls (Stroustrup, 1988). In Smalltalk, however, messages
are always dynamically bound to methods, and the farther away in the inheri-
tance hierarchy the correct method is, the longer it takes. The disadvantage of
allowing the user to decide which bindings are static and which are dynamic
is that the original design must include these decisions, which may have to be
changed later.
The static type checking of C++ is an advantage over Smalltalk, where all
type checking is dynamic. A Smalltalk program can be written with messages to
nonexistent methods, which are not discovered until the program is executed.
A C++ compiler finds such errors. Compiler-detected errors are less expensive
to repair than those found in testing.
Smalltalk is essentially typeless, meaning that all code is effectively generic.
This provides a great deal of flexibility, but static type checking is sacrificed. C++
provides generic classes through its template facility (as described in Chapter 11),
which retains the benefits of static type checking.
The primary advantage of Smalltalk lies in the elegance and simplicity of
the language, which results from the single philosophy of its design. It is purely
and completely devoted to the object-oriented paradigm, devoid of compro-
mises necessitated by the whims of an entrenched user base. C++, on the other
hand, is a large and complex language with no single philosophy as its founda-
tion, except to support object-oriented programming and include the C user
base. One of its most significant goals was to preserve the efficiency and flavor
of C while providing the advantages of object-oriented programming. Some
people feel that the features of this language do not always fit well together and
that at least some of the complexity is unnecessary.
According to Chambers and Ungar (1991), Smalltalk ran a particular set
of small C-style benchmarks at only 10 percent of the speed of optimized C.
C++ programs require only slightly more time than equivalent C programs
(Stroustrup, 1988). Given the great efficiency gap between Smalltalk and C++,
it is little wonder that the commercial use of C++ is far more widespread than
that of Smalltalk. There are other factors in this difference, but efficiency is
Free download pdf