B. Stroustrup - The C++ Programming Language (794319), страница 8
Текст из файла (страница 8)
C++’s emphasis on modularity, strongly typed interfaces, and flexibility pays off here. However, as programs get larger, the problems associated with their development and maintenance shiftfrom being language problems to being more global problems of tools and management.This book emphasizes techniques for providing general-purpose facilities, generally usefultypes, libraries, etc. These techniques will serve programmers of small programs as well as programmers of large ones.
Furthermore, because all nontrivial programs consist of many semi-independent parts, the techniques for writing such parts serve programmers of all applications.I use the implementation and use of standard-library components, such as vector, as examples.This introduces library components and their underlying design concepts and implementation techniques. Such examples show how programmers might design and implement their own libraries.However, if the standard library provides a component that addresses a problem, it is almost alwaysbetter to use that component than to build your own. Even if the standard component is arguablyslightly inferior to a home-built component for a particular problem, the standard component islikely to be more widely applicable, more widely available, and more widely known.
Over thelonger term, the standard component (possibly accessed through a convenient custom interface) islikely to lower long-term maintenance, porting, tuning, and education costs.You might suspect that specifying a program by using a more detailed type structure wouldincrease the size of the program source text (or even the size of the generated code). With C++,this is not so. A C++ program declaring function argument types, using classes, etc., is typically abit shorter than the equivalent C program not using these facilities. Where libraries are used, a C++program will appear much shorter than its C equivalent, assuming, of course, that a functioning Cequivalent could have been built.C++ supports systems programming.
This implies that C++ code is able to effectively interoperate with software written in other languages on a system. The idea of writing all software in asingle language is a fantasy. From the beginning, C++ was designed to interoperate simply andefficiently with C, assembler, and Fortran. By that, I meant that a C++, C, assembler, or Fortranfunction could call functions in the other languages without extra overhead or conversion of datastructures passed among them.C++ was designed to operate within a single address space. The use of multiple processes andmultiple address spaces relied on (extralinguistic) operating system support. In particular, Iassumed that a C++ programmer would have the operating systems command language availablefor composing processes into a system.
Initially, I relied on the UNIX Shell for that, but just aboutany ‘‘scripting language’’ will do. Thus, C++ provided no support for multiple address spaces andno support for multiple processes, but it was used for systems relying on those features from theearliest days. C++ was designed to be part of large, concurrent, multilanguage systems.Section 1.3Learning C++171.3 Learning C++No programming language is perfect. Fortunately, a programming language does not have to beperfect to be a good tool for building great systems. In fact, a general-purpose programming language cannot be perfect for all of the many tasks to which it is put.
What is perfect for one task isoften seriously flawed for another because perfection in one area implies specialization. Thus, C++was designed to be a good tool for building a wide variety of systems and to allow a wide variety ofideas to be expressed directly.Not everything can be expressed directly using the built-in features of a language. In fact, thatisn’t even the ideal. Language features exist to support a variety of programming styles and techniques. Consequently, the task of learning a language should focus on mastering the native and natural styles for that language – not on understanding of every little detail of every language feature.Writing programs is essential; understanding a programming language is not just an intellectualexercise. Practical application of ideas is necessary.In practical programming, there is little advantage in knowing the most obscure language features or using the largest number of features.
A single language feature in isolation is of little interest. Only in the context provided by techniques and by other features does the feature acquiremeaning and interest. Thus, when reading the following chapters, please remember that the realpurpose of examining the details of C++ is to be able to use language features and library facilitiesin concert to support good programming styles in the context of sound designs.No significant system is built exclusively in terms of the language features themselves.
Webuild and use libraries to simplify the task of programming and to increase the quality of our systems. We use libraries to improve maintainability, portability, and performance. Fundamentalapplication concepts are represented as abstractions (e.g., classes, templates, and class hierarchies)in libraries. Many of the most fundamental programming concepts are represented in the standardlibrary. Thus, learning the standard library is an integral part of learning C++. The standard libraryis the repository of much hard-earned knowledge of how to use C++ well.C++ is widely used for teaching and research.
This has surprised some who – correctly – pointout that C++ isn’t the smallest or cleanest language ever designed. It is, however:• Sufficiently clean for successfully teaching basic design and programming concepts• Sufficiently comprehensive to be a vehicle for teaching advanced concepts and techniques• Sufficiently realistic, efficient, and flexible for demanding projects• Sufficiently commercial to be a vehicle for putting what is learned into nonacademic use• Sufficiently available for organizations and collaborations relying on diverse developmentand execution environmentsC++ is a language that you can grow with.The most important thing to do when learning C++ is to focus on fundamental concepts (suchas type safety, resource management, and invariants) and programming techniques (such asresource management using scoped objects and the use of iterators in algorithms) and not get lost inlanguage-technical details. The purpose of learning a programming language is to become a betterprogrammer, that is, to become more effective at designing and implementing new systems and atmaintaining old ones.
For this, an appreciation of programming and design techniques is far moreimportant than understanding all the details. The understanding of technical details comes withtime and practice.18Notes to the ReaderChapter 1C++ programming is based on strong static type checking, and most techniques aim at achieving a high level of abstraction and a direct representation of the programmer’s ideas.
This can usually be done without compromising run-time and space efficiency compared to lower-level techniques. To gain the benefits of C++, programmers coming to it from a different language mustlearn and internalize idiomatic C++ programming style and technique. The same applies to programmers used to earlier and less expressive versions of C++.Thoughtlessly applying techniques effective in one language to another typically leads to awkward, poorly performing, and hard-to-maintain code. Such code is also most frustrating to writebecause every line of code and every compiler error message reminds the programmer that the language used differs from ‘‘the old language.’’ You can write in the style of Fortran, C, Lisp, Java,etc., in any language, but doing so is neither pleasant nor economical in a language with a differentphilosophy.
Every language can be a fertile source of ideas about how to write C++ programs.However, ideas must be transformed into something that fits with the general structure and typesystem of C++ in order to be effective in C++. Over the basic type system of a language, onlyPyrrhic victories are possible.In the continuing debate on whether one needs to learn C before C++, I am firmly convincedthat it is best to go directly to C++. C++ is safer and more expressive, and it reduces the need tofocus on low-level techniques. It is easier for you to learn the trickier parts of C that are needed tocompensate for its lack of higher-level facilities after you have been exposed to the common subsetof C and C++ and to some of the higher-level techniques supported directly in C++. Chapter 44 isa guide for programmers going from C++ to C, say, to deal with legacy code.