- Re-use of code. Linking of code to objects and explicit specification of relations between objects allows related objects to share code. Encapsulation and weak coupling between objects means class definitions are more likely to be re-used in other applications. Objects as well as procedures (focus of C libraries) become likely candidates for re-use. The enforcement of a consistent interface to objects lessens code duplication.
- Ease of comprehension. Structure of code and data structures in it can be set up to closely mimic the generic application concepts and processes. High-level code could make some sense even to a non-programmer. The analysis/design/coding phases in development become more seamless since they can all deal in the same concepts.
- Ease of fabrication and maintenance (redesign and extension) facilitated by encapsulation, data abstraction which allow for very clean designs. When an object is going into disallowed states, only its methods need be investigated. This narrows down search for problems.
- C++ Objectives
- extend C to allow for object-oriented programming
- other improvements - some resulting in deprecation of some C facilities
- remain compatible and comparable (syntax, performance, portability, design philosophy - don't pay for what you don't use, don't get stuck with things you don't need) with C
- emphasize compile-time type checking
- C++ is multi-paradigm. It provides for the object-oriented approach but doesn't enforce its use. This makes it a good transition language and gives it flexibility when a particular situation doesn't fit the object-oriented philosophy.
- With this object-oriented approach, C++ overcomes certain shortcomings of C:
- Lack of encapsulation means that if an object is getting trashed, it's difficult to find the code responsible. Many procedures may have had idiosyncratic interactions with the object.
- Doesn't recognize relationships between types. Pointer casting necessary. In C++, pointer casting can just about always be dispensed with. Pointer casting is a kludge. Compiler can't check if you are doing it correctly. No type safety (see definition below).
- Not easy to extend existing libraries; for example, make it so printf() can handle new types.
- Except for FILEs, there are no well-developed objects (like stacks and lists) in the standard libraries.
- C's future is as a portable "universal" assembler, a back end for code generators.
- While any C++ compiler should be able to compile a C program successfully with minor changes, several aspects of C programming are discarded in the transition to C++: new facilities are supplied for I/O, memory allocation and error handling; macros and pointer casts become obsolete for the most part.
2004-06-29