Stroustrup himself (from the horse's mouth):
C++ Design Goals:
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:
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":
References (pointers) to objects in C, C++, and Java:
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.) |