Previous | Table of Contents | Next |
by Andrew Koenig
C++ is the result of an unusual strategy in programming language design: It is intended to allow programmers to express high-level solutions to problems without compromising their ability to get at low-level machine facilities efficiently. For example, although many programming languages allow direct access to the machines memory, and many programming languages support a variable-length string type, few languages support both at once. Not only does C++ support both, but it does so by making it possible to implement variable-length strings efficiently in the library, rather than by building them into the compiler. As another example, although C++ does not offer garbage collection as a standard part of the language, it is possible to use C++ to implement flexible data structures that are almost as easy to use as their garbage-collected counterparts are.
This chapter assumes that the reader is an experienced programmer, preferably one who knows more than one programming language thoroughly, but it does not assume any knowledge of C or C++. It uses examples of C++ programs in several styles to give enough of a feel for the language to make it possible to understand substantial C++ programs, and perhaps even to write them. To that end, we will emphasize the parts of the language that are most important in practical use; other sources cover the language in exhaustive detail.
Although this chapter discusses the most important concepts in the C++ standard library, it emphasizes the core language. With a thorough understanding of the core language and an overview of the key ideas behind the library, it is possible to find appropriate library facilities in a more detailed reference. Moreover, because the standard library is not aimed at any particular application area, and because the library is a more recent development than the core language, many C++ programs are written in terms of application-specific libraries, rather than relying exclusively on the standard library.
Section 7.1 describes the most common styles for using C++ and explains a small, but complete, C++ program that solves an interesting problem. Section 7.2 describes the most fundamental parts of the language, on which the rest of C++ is built. The descriptions in section 7.2 do not assume a prior knowledge of C.
Section 7.3 covers the facilities that C++ uses to support data abstraction, with an emphasis on programs that deal with objects of types that are known at the time the program is written. Section 7.4 covers programs whose knowledge of types is deferred until the time the program is compiled. Such programs are often called generic programs, and the C++ template facility is the fundamental tool for supporting them.
Section 7.5 discusses object-oriented programming, which is a technique for writing programs that deal with types that are not completely known until the program actually runs. The discussion includes a complete example program, which manipulates expression trees.
Finally, Section 7.6 gives a high-level overview of the standard library.
Of course, a chapter of this size cannot convey everything there is to know about C++. However, it is intended to give enough information that it can serve as a basis for future study. Section 7.7 recommends books that are likely to be useful for such study. Of course, the way to learn a skill is to use it, so any study of C++ should include writing programs.
In addition to supporting programs written with different degrees of abstraction, C++ also supports programs written with different kinds of abstraction.
For example, many C++ programs are what might be called bread and-butter programs. They may use library facilitieseven sophisticated onesbut they dont do much in the way of defining their own abstractions. Prosaic though they might be, such programs constitute many of the most productive applications of any programming language.
Another important programming style that C++ supports well is classical data abstraction. This phrase refers to programs that define individual abstract data types that other parts of the programor other programsthen use. The best known example of this style in C++ is using the language to define a data type that represents variable-length character strings. Although such strings are part of the standard library, we can reimplement them without relying on the library, which makes their construction a useful subject for study.
A kind of abstraction that has become associated with C++ in recent years is generic programming. We talk about programs as being generic if they deliberately try to ignore as much as possible about the data types they use. For example, we might talk about a generic sorting algorithm, referring to one that is capable of sorting the elements of any sequence, regardless of the specific structure of the sequence or the types of the elements.
Finally, there is object-oriented programming, which is programming with inheritance and dynamic binding. This style is particularly useful for simulationeither of things that exist or things that do not. For example, we can think of the buttons, icons, and other elements of a graphical user interface as simulating an imaginary reality. The point of object-oriented programming is to be able to ignore the differences between the types of the objects in a system, but only when it is useful to do so. So, for example, when we move a graphical element around, we would like not to have to care what kind of element it is, but when it comes time to display it, the details become crucially important.
Previous | Table of Contents | Next |