C Language

A Better C

Many claims have been made for C++, but one thing seems certain: C++ is a "better" - if more complex - version of C. C++ adds some important language features missing in C; for example, reference parameters, inline functions, and templates to define generic functions and classes. These features aid clearer programming and can reduce - but not eliminate - the need for macros in C++ programs.

What C++ doesn't do is eliminate any of C's traps. C++ was intentionally designed to be an almost complete superset of C; that is, almost any ANSI C program - even one using dangerous C techniques that have better alternatives in C++ - will compile as a C++ program. Thus, you can still be burned by typing = instead of == in a C++ program (I discuss this in Chapter 2). C++ also continues the heavy use of special characters, rather than keywords, in its syntax. The problems that arise from C's use of * for "pointer" or "contents of" and & for "address of" are compounded by new C++ notations, such as a trailing & for "reference."

C++ also introduces facilities for object-oriented programming (OOP). The primary new C++ concept is the "class," which is a facility to package functions and variable declarations together so that new data types can be defined and used in C++ programs. C++ also provides for "inheritance," a facility for deriving a new class definition from an existing class. The OOP capabilities of C++ are quite powerful, and when you work with a well-designed C++ class library, many implementation details can safely be ignored. But creating new classes is a different matter; and, if you write many non-trivial programs in C++, eventually you'll have to construct some of your own classes. As Chapter 8 points out, there are some very slippery slopes to climb as you write C++ classes.

The question frequently arises of whether a programmer who doesn't know either C or C++ should learn C or C++ first. That's a hard call, and the best answer may be to learn one of the object-oriented extensions of Pascal or Smalltalk first, the idea being to learn the OOP concepts with a language not so laden with assembly language baggage, then learn how to do it in C++. In any case, you can't completely skip over learning about problem areas in C because most of these still exist in C++. As a result, much of this tutorial is directed at problem areas common to both languages.


by BrainBellupdated
Advertisement: