没有合适的资源?快使用搜索试试~ 我知道了~
首页C++编程语言第三版英文原版:严谨学习指南
C++编程语言第三版英文原版:严谨学习指南
需积分: 9 22 下载量 124 浏览量
更新于2023-07-11
收藏 2.83MB PDF 举报
"C++程序设计语言(特别版)英文原版"是一本由Bjarne Stroustrup编写的经典著作,他是C++语言的设计者之一。该书第三版针对的是C++编程语言的专业学习者,特别是那些对翻译版本质量有高要求、希望深入理解和掌握英文原版概念的读者。作为英文原版书籍,它提供了详尽的理论基础和实践指导,适合有一定英语水平的开发者阅读。
本书全面覆盖了C++语言的核心概念,包括类与对象、模板、异常处理、泛型编程、STL(Standard Template Library)等内容,以及C++11和后续版本的特性。作者通过清晰的解释和丰富的示例,帮助读者理解并掌握C++的设计哲学、语法规则以及高效编程策略。书中还包含了对C++标准的深度解析,这对于理解和遵循编码最佳实践至关重要。
此外,书中强调版权保护,注明制造商和销售商可能会使用特定设计标识来区分产品,并声明对此类标识的尊重。同时,尽管作者和出版商对书籍内容的准确性进行了精心校对,但并不提供任何形式的明确或隐含的保修,也不承担因使用书中的信息而产生的偶然或间接损害的责任。
对于批量订购的优惠,有兴趣的读者可以联系Addison-Wesley Publishing Company的Corporate & Professional Publishing Group获取更多详情。这本书不仅是一份技术参考,也是C++程序员的成长指南,无论是在学术研究还是职业发展上,都能提供扎实的基础支持。
阅读这本英文原版的C++程序设计语言,将有助于读者深入理解C++语言的本质,提升编程技能,并紧跟编程领域的最新动态。对于想要成为专业C++开发者的人来说,这是一本不可或缺的学习资料。
6 Notes to the Reader Chapter 1
1.1.2 Exercises
Exercises are found at the ends of chapters. The exercises are mainly of the write-a-program vari-
ety. Always write enough code for a solution to be compiled and run with at least a few test cases.
The exercises vary considerably in difficulty, so they are marked with an estimate of their diffi-
culty. The scale is exponential so that if a (∗1) exercise takes you ten minutes, a (∗2) might take an
hour, and a (∗3) might take a day. The time needed to write and test a program depends more on
your experience than on the exercise itself. A (∗1) exercise might take a day if you first have to get
acquainted with a new computer system in order to run it. On the other hand, a (∗5) exercise might
be done in an hour by someone who happens to have the right collection of programs handy.
Any book on programming in C can be used as a source of extra exercises for Part I. Any book
on data structures and algorithms can be used as a source of exercises for Parts II and III.
1.1.3 Implementation Note
The language used in this book is ‘‘pure C
++
’’ as defined in the C
++
standard [C
++
,1998]. There-
fore, the examples ought to run on every C
++
implementation. The major program fragments in
this book were tried using several C
++
implementations. Examples using features only recently
adopted into C
++
didn’t compile on every implementation. However, I see no point in mentioning
which implementations failed to compile which examples. Such information would soon be out of
date because implementers are working hard to ensure that their implementations correctly accept
every C
++
feature. See Appendix B for suggestions on how to cope with older C
++
compilers and
with code written for C compilers.
1.2 Learning C
++
The most important thing to do when learning C
++
is to focus on concepts and not get lost in
language-technical details. The purpose of learning a programming language is to become a better
programmer; that is, to become more effective at designing and implementing new systems and at
maintaining old ones. For this, an appreciation of programming and design techniques is far more
important than an understanding of details; that understanding comes with time and practice.
C
++
supports a variety of programming styles. All are based on strong static type checking, and
most aim at achieving a high level of abstraction and a direct representation of the programmer’s
ideas. Each style can achieve its aims effectively while maintaining run-time and space efficiency.
A programmer coming from a different language (say C, Fortran, Smalltalk, Lisp, ML, Ada, Eiffel,
Pascal, or Modula-2) should realize that to gain the benefits of C
++
, they must spend time learning
and internalizing programming styles and techniques suitable to C
++
. The same applies to pro-
grammers used to an earlier and less expressive version of C
++
.
Thoughtlessly applying techniques effective in one language to another typically leads to awk-
ward, poorly performing, and hard-to-maintain code. Such code is also most frustrating to write
because every line of code and every compiler error message reminds the programmer that the lan-
guage used differs from ‘‘the old language.’’ You can write in the style of Fortran, C, Smalltalk,
etc., in any language, but doing so is neither pleasant nor economical in a language with a different
philosophy. Every language can be a fertile source of ideas of how to write C
++
programs.
The C++ Programming Language, Special Edition by Bjarne Stroustrup. Copyright 2000 by AT&T.
Published by Addison Wesley, Inc. ISBN 0-201-70073-5. All rights reserved.
Section 1.2 Learning C
++
7
However, ideas must be transformed into something that fits with the general structure and type
system of C
++
in order to be effective in the different context. Over the basic type system of a lan-
guage, only Pyrrhic victories are possible.
C
++
supports a gradual approach to learning. How you approach learning a new programming
language depends on what you already know and what you aim to learn. There is no one approach
that suits everyone. My assumption is that you are learning C
++
to become a better programmer
and designer. That is, I assume that your purpose in learning C
++
is not simply to learn a new syn-
tax for doing things the way you used to, but to learn new and better ways of building systems.
This has to be done gradually because acquiring any significant new skill takes time and requires
practice. Consider how long it would take to learn a new natural language well or to learn to play a
new musical instrument well. Becoming a better system designer is easier and faster, but not as
much easier and faster as most people would like it to be.
It follows that you will be using C
++
– often for building real systems – before understanding
every language feature and technique. By supporting several programming paradigms (Chapter 2),
C
++
supports productive programming at several levels of expertise. Each new style of program-
ming adds another tool to your toolbox, but each is effective on its own and each adds to your
effectiveness as a programmer. C
++
is organized so that you can learn its concepts in a roughly lin-
ear order and gain practical benefits along the way. This is important because it allows you to gain
benefits roughly in proportion to the effort expended.
In the continuing debate on whether one needs to learn C before C
++
, I am firmly convinced
that it is best to go directly to C
++
. C
++
is safer, more expressive, and reduces the need to focus on
low-level techniques. It is easier for you to learn the trickier parts of C that are needed to compen-
sate for its lack of higher-level facilities after you have been exposed to the common subset of C
and C
++
and to some of the higher-level techniques supported directly in C
++
. Appendix B is a
guide for programmers going from C
++
to C, say, to deal with legacy code.
Several independently developed and distributed implementations of C
++
exist. A wealth of
tools, libraries, and software development environments are also available. A mass of textbooks,
manuals, journals, newsletters, electronic bulletin boards, mailing lists, conferences, and courses
are available to inform you about the latest developments in C
++
, its use, tools, libraries, implemen-
tations, etc. If you plan to use C
++
seriously, I strongly suggest that you gain access to such
sources. Each has its own emphasis and bias, so use at least two. For example, see [Barton,1994],
[Booch,1994], [Henricson,1997], [Koenig,1997], [Martin,1995].
1.3 The Design of C
++
Simplicity was an important design criterion: where there was a choice between simplifying the
language definition and simplifying the compiler, the former was chosen. However, great impor-
tance was attached to retaining a high degree of compatibility with C [Koenig,1989] [Strous-
trup,1994] (Appendix B); this precluded cleaning up the C syntax.
C
++
has no built-in high-level data types and no high-level primitive operations. For example,
the C
++
language does not provide a matrix type with an inversion operator or a string type with a
concatenation operator. If a user wants such a type, it can be defined in the language itself. In fact,
defining a new general-purpose or application-specific type is the most fundamental programming
The C++ Programming Language, Special Edition by Bjarne Stroustrup. Copyright 2000 by AT&T.
Published by Addison Wesley, Inc. ISBN 0-201-70073-5. All rights reserved.
8 Notes to the Reader Chapter 1
activity in C
++
. A well-designed user-defined type differs from a built-in type only in the way it is
defined, not in the way it is used. The C
++
standard library described in Part III provides many
examples of such types and their uses. From a user’s point of view, there is little difference
between a built-in type and a type provided by the standard library.
Features that would incur run-time or memory overheads even when not used were avoided in
the design of C
++
. For example, constructs that would make it necessary to store ‘‘housekeeping
information’’ in every object were rejected, so if a user declares a structure consisting of two 16-bit
quantities, that structure will fit into a 32-bit register.
C
++
was designed to be used in a traditional compilation and run-time environment, that is, the
C programming environment on the UNIX system. Fortunately, C
++
was never restricted to UNIX;
it simply used UNIX and C as a model for the relationships between language, libraries, compilers,
linkers, execution environments, etc. That minimal model helped C
++
to be successful on essen-
tially every computing platform. There are, however, good reasons for using C
++
in environments
that provide significantly more support. Facilities such as dynamic loading, incremental compila-
tion, and a database of type definitions can be put to good use without affecting the language.
C
++
type-checking and data-hiding features rely on compile-time analysis of programs to pre-
vent accidental corruption of data. They do not provide secrecy or protection against someone who
is deliberately breaking the rules. They can, however, be used freely without incurring run-time or
space overheads. The idea is that to be useful, a language feature must not only be elegant; it must
also be affordable in the context of a real program.
For a systematic and detailed description of the design of C
++
, see [Stroustrup,1994].
1.3.1 Efficiency and Structure
C
++
was developed from the C programming language and, with few exceptions, retains C as a
subset. The base language, the C subset of C
++
, is designed to ensure a very close correspondence
between its types, operators, and statements and the objects that computers deal with directly: num-
bers, characters, and addresses. Except for the n ne ew w, d de el le et te e, t ty yp pe ei id d, d dy yn na am mi ic c_ _c ca as st t, and t th hr ro ow w oper-
ators and the try-block, individual C
++
expressions and statements need no run-time support.
C
++
can use the same function call and return sequences as C – or more efficient ones. When
even such relatively efficient mechanisms are too expensive, a C
++
function can be substituted
inline, so that we can enjoy the notational convenience of functions without run-time overhead.
One of the original aims for C was to replace assembly coding for the most demanding systems
programming tasks. When C
++
was designed, care was taken not to compromise the gains in this
area. The difference between C and C
++
is primarily in the degree of emphasis on types and struc-
ture. C is expressive and permissive. C
++
is even more expressive. However, to gain that increase
in expressiveness, you must pay more attention to the types of objects. Knowing the types of
objects, the compiler can deal correctly with expressions when you would otherwise have had to
specify operations in painful detail. Knowing the types of objects also enables the compiler to
detect errors that would otherwise persist until testing – or even later. Note that using the type sys-
tem to check function arguments, to protect data from accidental corruption, to provide new types,
to provide new operators, etc., does not increase run-time or space overheads in C
++
.
The emphasis on structure in C
++
reflects the increase in the scale of programs written since C
was designed. You can make a small program (say, 1,000 lines) work through brute force even
The C++ Programming Language, Special Edition by Bjarne Stroustrup. Copyright 2000 by AT&T.
Published by Addison Wesley, Inc. ISBN 0-201-70073-5. All rights reserved.
Section 1.3.1 Efficiency and Structure 9
when breaking every rule of good style. For a larger program, this is simply not so. If the structure
of a 100,000-line program is bad, you will find that new errors are introduced as fast as old ones are
removed. C
++
was designed to enable larger programs to be structured in a rational way so that it
would be reasonable for a single person to cope with far larger amounts of code. In addition, the
aim was to have an average line of C
++
code express much more than the average line of C or Pas-
cal code. C
++
has by now been shown to over-fulfill these goals.
Not every piece of code can be well-structured, hardware-independent, easy-to-read, etc. C
++
possesses features that are intended for manipulating hardware facilities in a direct and efficient
way without regard for safety or ease of comprehension. It also possesses facilities for hiding such
code behind elegant and safe interfaces.
Naturally, the use of C
++
for larger programs leads to the use of C
++
by groups of program-
mers. C
++
’s emphasis on modularity, strongly typed interfaces, and flexibility pays off here. C
++
has as good a balance of facilities for writing large programs as any language has. However, as
programs get larger, the problems associated with their development and maintenance shift from
being language problems to more global problems of tools and management. Part IV explores
some of these issues.
This book emphasizes techniques for providing general-purpose facilities, generally useful
types, libraries, etc. These techniques will serve programmers of small programs as well as pro-
grammers 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.
You might suspect that specifying a program by using a more detailed type structure would lead
to a larger program source text. With C
++
, this is not so. A C
++
program declaring function argu-
ment types, using classes, etc., is typically a bit shorter than the equivalent C program not using
these facilities. Where libraries are used, a C
++
program will appear much shorter than its C equiv-
alent, assuming, of course, that a functioning C equivalent could have been built.
1.3.2 Philosophical Note
A programming language serves two related purposes: it provides a vehicle for the programmer to
specify actions to be executed, and it provides a set of concepts for the programmer to use when
thinking about what can be done. The first purpose ideally requires a language that is ‘‘close to the
machine’’ so that all important aspects of a machine are handled simply and efficiently in a way
that is reasonably obvious to the programmer. The C language was primarily designed with this in
mind. The second purpose ideally requires a language that is ‘‘close to the problem to be solved’’
so that the concepts of a solution can be expressed directly and concisely. The facilities added to C
to create C
++
were primarily designed with this in mind.
The connection between the language in which we think/program and the problems and solu-
tions we can imagine is very close. For this reason, restricting language features with the intent of
eliminating programmer errors is at best dangerous. As with natural languages, there are great ben-
efits from being at least bilingual. A language provides a programmer with a set of conceptual
tools; if these are inadequate for a task, they will simply be ignored. Good design and the absence
of errors cannot be guaranteed merely by the presence or the absence of specific language features.
The type system should be especially helpful for nontrivial tasks. The C
++
class concept has, in
fact, proven itself to be a powerful conceptual tool.
The C++ Programming Language, Special Edition by Bjarne Stroustrup. Copyright 2000 by AT&T.
Published by Addison Wesley, Inc. ISBN 0-201-70073-5. All rights reserved.
10 Notes to the Reader Chapter 1
1.4 Historical Note
I invented C
++
, wrote its early definitions, and produced its first implementation. I chose and for-
mulated the design criteria for C
++
, designed all its major facilities, and was responsible for the
processing of extension proposals in the C
++
standards committee.
Clearly, C
++
owes much to C [Kernighan,1978]. Except for closing a few serious loopholes in
the type system (see Appendix B), C is retained as a subset. I also retained C’s emphasis on facili-
ties that are low-level enough to cope with the most demanding systems programming tasks. C in
turn owes much to its predecessor BCPL [Richards,1980]; in fact, BCPL’s / / comment convention
was (re)introduced in C
++
. The other main source of inspiration for C
++
was Simula67
[Dahl,1970] [Dahl,1972]; the class concept (with derived classes and virtual functions) was bor-
rowed from it. C
++
’s facility for overloading operators and the freedom to place a declaration
wherever a statement can occur resembles Algol68 [Woodward,1974].
Since the original edition of this book, the language has been extensively reviewed and refined.
The major areas for revision were overload resolution, linking, and memory management facilities.
In addition, several minor changes were made to increase C compatibility. Several generalizations
and a few major extensions were added: these included multiple inheritance, s st ta at ti ic c member func-
tions, c co on ns st t member functions, p pr ro ot te ec ct te ed d members, templates, exception handling, run-time type
identification, and namespaces. The overall theme of these extensions and revisions was to make
C
++
a better language for writing and using libraries. The evolution of C
++
is described in [Strous-
trup,1994].
The template facility was primarily designed to support statically typed containers (such as lists,
vectors, and maps) and to support elegant and efficient use of such containers (generic program-
ming). A key aim was to reduce the use of macros and casts (explicit type conversion). Templates
were partly inspired by Ada’s generics (both their strengths and their weaknesses) and partly by
Clu’s parameterized modules. Similarly, the C
++
exception-handling mechanism was inspired
partly by Ada [Ichbiah,1979], Clu [Liskov,1979], and ML [Wikstr
. .
om,1987]. Other developments
in the 1985 to 1995 time span – such as multiple inheritance, pure virtual functions, and name-
spaces – were primarily generalizations driven by experience with the use of C
++
rather than ideas
imported from other languages.
Earlier versions of the language, collectively known as ‘‘C with Classes’’ [Stroustrup,1994],
have been in use since 1980. The language was originally invented because I wanted to write some
event-driven simulations for which Simula67 would have been ideal, except for efficiency consid-
erations. ‘‘C with Classes’’ was used for major projects in which the facilities for writing programs
that use minimal time and space were severely tested. It lacked operator overloading, references,
virtual functions, templates, exceptions, and many details. The first use of C
++
outside a research
organization started in July 1983.
The name C
++
(pronounced ‘‘see plus plus’’) was coined by Rick Mascitti in the summer of
1983. The name signifies the evolutionary nature of the changes from C; ‘‘++’’ is the C increment
operator. The slightly shorter name ‘‘C+’’ is a syntax error; it has also been used as the name of an
unrelated language. Connoisseurs of C semantics find C
++
inferior to ++C. The language is not
called D, because it is an extension of C, and it does not attempt to remedy problems by removing
features. For yet another interpretation of the name C
++
, see the appendix of [Orwell,1949].
C
++
was designed primarily so that my friends and I would not have to program in assembler,
The C++ Programming Language, Special Edition by Bjarne Stroustrup. Copyright 2000 by AT&T.
Published by Addison Wesley, Inc. ISBN 0-201-70073-5. All rights reserved.
剩余1020页未读,继续阅读
点击了解资源详情
点击了解资源详情
点击了解资源详情
2008-07-10 上传
2011-05-02 上传
2009-12-27 上传
2009-12-27 上传
2010-05-30 上传
2008-09-18 上传
godjing1985
- 粉丝: 5
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- CoreOS部署神器:configdrive_creator脚本详解
- 探索CCR-Studio.github.io: JavaScript的前沿实践平台
- RapidMatter:Web企业架构设计即服务应用平台
- 电影数据整合:ETL过程与数据库加载实现
- R语言文本分析工作坊资源库详细介绍
- QML小程序实现风车旋转动画教程
- Magento小部件字段验证扩展功能实现
- Flutter入门项目:my_stock应用程序开发指南
- React项目引导:快速构建、测试与部署
- 利用物联网智能技术提升设备安全
- 软件工程师校招笔试题-编程面试大学完整学习计划
- Node.js跨平台JavaScript运行时环境介绍
- 使用护照js和Google Outh的身份验证器教程
- PHP基础教程:掌握PHP编程语言
- Wheel:Vim/Neovim高效缓冲区管理与导航插件
- 在英特尔NUC5i5RYK上安装并优化Kodi运行环境
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功