CS 3723
 Programming Languages 
   C++, C, Java  


Stroustrup himself (from the horse's mouth):

  • Bjarne Stroustrup's homepage. This is loaded with useful and interesting information about C++. Very sophisticated, but also practical and realistic.
  • Stroustrup has his own C++ tutorial book, now in its third edition, which he recommends: The C++ Programming Lanuage, Special Edition by Bjarne Stroustrup, Addison-Wesley. Approx. 1000 pages. A notoriously difficult introduction to C++, this book is not for the faint-hearted or for novices
  • Stroustrup has provided a remarkable book about the reasoning that went on in his development of C++: The Design and Evolution of C++, Addison-Wesley, 1994. 461 pages. This book gives a unique insider view into the history and the design decisions involved C++'s development.


C++ Design Goals:

  1. Support object-oriented programming.
  2. A compatible extension of C.
  3. As efficient as C (almost).
  4. A "better C".
    1. Support object-oriented programming:

    This was the main reason for creating a new language. The original name was "C with Classes". Stroustrup did not copy Smalltallk's O-O features for C++, but based them on the old Simula 67 language (from 1967). Smalltalk also was based on Simula 67, so that Stroustrup regards C++ as a sibling of Smalltalk and not an offspring.

    The principle competitor of C++ was Objective C, another extension of C that grafted Smalltalk's features onto C, particularly the classes and message passing as communication between classes. More occurs at run-time in Objective C than in C++ -- in particular the message passing takes place at run-time. For this reason Objective C is more flexible (and perhaps easier to use) than C++, but not as efficient. Objective C was used in the very successful project to develope the upper parts of the NeXT operating system, and Objective C is still used in parts of Apple's OS X operating system, which was derived from NeXT.

    C++ provides a rich set of O-O features, including most of what Java has, and even the controversial multiple inheritance.

    2. A compatible extension of C:

    This was a difficult goal, partly because there were many C dialects. In the end, C++ is compatible with reasonable and modern features of C, but drops some old-fashions C features, ones that today's students usually don't learn anyway. For example, old-style C function parameters are not legal in C++. They look like the following:

    Old style C parameters New style C parameters
    int funct(x, y)
      int x; double y;
    {
       /* ... */
    }
    int funct(int x, double y)
    {
       /* ... */
    }

    As another example, C allows a function call without an earlier prototype -- the function is assumed to return int by default. C++ requires a prototype or definition of any function before it is called.

    The more-or-less compatibility was a big factor in C++'s popularity.

    3. As efficient as C (almost):

    It was very important to Stroustrup that C++ would execute a given C program as efficiently as C. This is major reason for the popularity of C++. Some few new features of C++ do involve a slight additional run-time overhead: polymorphism for example.

    4. A "better C":

    • Discourage all use of the preprocessor (except for #include).
      • Instead of constants defined by (for example) #define PI 3.141592653589793, use something like const double PI = 3.141592653589793;. (The second method gets syntax checked and type checked -- much less error prone.)
      • Instead of macros defined by (for example) #define SQR(x) (x)*(x), use a function declared inline. (This is much less error prone, and with a good compiler, it will be as efficient.)
    • Various outmoded features of C actually dropped -- mostly stuff you aren't familar with anyway. (Examples mentioned under 2 above.)
    • Required prototypes.
    • Strong typing more rigorously enforced.
    • Type-safe I/O.
    • A user-defined type should look just like a built-in type.

References (pointers) to objects in C, C++, and Java:

    Passing an array to a function as a parameter in C, C++, and Java: These all handle arrays as parameters by passing a pointer or reference to the array. It is not possible directly to pass an array "by value", that is, by making a copy of the array. If an array is passed to a function and its elements are changed inside the function, the array in the calling function will also be changed.

    Returning an array from a function in C, C++, and Java: Again in all three cases a pointer or reference to the array is returned.

    In Java passing a class to a function as a parameter, or returning a class from a function: Here the only option is to pass a reference (pointer) to the class and to return a reference (pointer).

    Passing a struct (C or C++) or a class (C++) to a function as a parameter, or returning a struct or class: Here a "bare" struct or class can be passed, and a "bare" class can be returned. In all cases a copy is made during the pass or return. If one wishes to pass a pointer to the class or return a pointer to the class, this must be done explicitely. These issues are illustrated in a C program: structs in C


C, C++, and Java.

Comparison between C++ and Java: Here are side-by-side listings:
Revision date: 2013-02-16. (Please use ISO 8601, the International Standard Date and Time Notation.)