⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter02.html

📁 《C++编程思想》中文版。。。。。。。。。。。。。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIC2Vone.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:09/27/2001
Translation Time:05:25:25
Translation Platform:Win32
Number of Output files:22
This File:Chapter02.html
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>2: Making &amp; Using Objects </TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview-head.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README-HTML.txt">Viewing Hints</a> ]
    [ <a href="http://www.mindview.net/CPPServices/SolutionGuide.html">Exercise Solutions</a> ]
    [ <a href="http://www.mindview.net/ThinkingInCPP2e.html">Volume 2</a> ]
    [ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/CPPServices/#PublicSeminars">Seminars</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#SeminarsOnCD">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in C++, 2nd ed. Volume 1</FONT></H2></FONT>
  <H3><FONT FACE="Verdana">&copy;2000 by Bruce Eckel</FONT></H3></FONT>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chapter01.html">Previous Chapter</a> ] 
    [ <a href="Contents.html">Table of Contents</a> ] 
    [ <a href="DocIndex.html">Index</a> ]
     [ <a href="Chapter03.html">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>This chapter will introduce enough
C++ syntax and program construction concepts to allow you to write
</FONT><BR><FONT FACE="Verdana" SIZE=4>and run some simple object-oriented
programs. In the subsequent chapter we will cover the basic syntax of C and C++
in detail.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">By reading this chapter first,
you&#8217;ll get the basic flavor of what it is like to program with objects in
C++, and you&#8217;ll also discover some of the reasons for the enthusiasm
surrounding this language. This should be enough to carry you through Chapter 3,
which can be a bit exhausting since it contains most of the details of the C
language.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The user-defined data
type<A NAME="Index253"></A><A NAME="Index254"></A>, or
<I>class<A NAME="Index255"></A></I>, is what distinguishes C++ from traditional
procedural languages. A class is a new data type that you or someone else
creates to solve a particular kind of problem. Once a class is created, anyone
can use it without knowing the specifics of how it works, or even how classes
are built. This chapter treats classes as if they are just another built-in data
type available for use in programs. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Classes that someone else has created are
typically packaged into a library<A NAME="Index256"></A>. This chapter uses
several of the class libraries that come with all C++ implementations. An
especially important standard library is iostreams, which (among other things)
allow you to read from files and the keyboard, and to write to files and the
display. You&#8217;ll also see the very handy <B>string </B>class, and the
<B>vector </B>container from the Standard C++ Library. By the end of the
chapter, you&#8217;ll see how easy it is to use a pre-defined library of
classes.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In order to create your first program you
must understand the tools used to build
applications.</FONT><A NAME="_Toc462979713"></A><A NAME="_Toc472654717"></A><BR></P></DIV>
<A NAME="Heading66"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
The process of language translation</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All computer languages are translated
from something that tends to be easy for a human to understand (<I>source
code</I>)<I> </I>into something that is executed on a computer (<I>machine
instructions<A NAME="Index257"></A></I>). Traditionally, translators fall into
two classes: <I>interpreters</I> and
<A NAME="Index258"></A><I>compilers</I>.</FONT><A NAME="_Toc462979714"></A><A NAME="_Toc472654718"></A><BR></P></DIV>
<A NAME="Heading67"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Interpreters<A NAME="Index259"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">An interpreter translates source code
into activities (which may comprise groups of machine instructions) and
immediately executes those activities. <A NAME="Index260"></A>BASIC, for
example, has been a popular interpreted language. Traditional BASIC interpreters
translate and execute one line at a time, and then forget that the line has been
translated. This makes them slow, since they must re-translate any repeated
code. BASIC has also been compiled, for speed. More modern interpreters, such as
those for the <A NAME="Index261"></A>Python language, translate the entire
program into an intermediate language that is then executed by a much faster
interpreter</FONT><A NAME="fnB25" HREF="#fn25">[25]</A><A NAME="Index262"></A><FONT FACE="Georgia">.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Interpreters have many advantages. The
transition from writing code to executing code is almost immediate, and the
source code is always available so the interpreter can be much more specific
when an error occurs. The benefits often cited for interpreters are ease of
interaction and rapid development (but not necessarily execution) of
programs.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Interpreted languages often have severe
limitations when building large projects (Python seems to be an exception to
this). The interpreter (or a reduced version) must always be in memory to
execute the code, and even the fastest interpreter may introduce unacceptable
speed restrictions. Most interpreters require that the complete source code be
brought into the interpreter all at once. Not only does this introduce a space
limitation, it can also cause more difficult bugs if the language doesn&#8217;t
provide facilities to localize the effect of different pieces of
code.</FONT><A NAME="_Toc462979715"></A><A NAME="_Toc472654719"></A><BR></P></DIV>
<A NAME="Heading68"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Compilers<A NAME="Index263"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A compiler translates source code
directly into assembly language or machine instructions. The eventual end
product is a file or files containing machine code. This is an involved process,
and usually takes several steps. The transition from writing code to executing
code is significantly longer with a compiler.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Depending on the acumen of the compiler
writer, programs generated by a compiler tend to require much less space to run,
and they run much more quickly. Although size and speed are probably the most
often cited reasons for using a compiler, in many situations they aren&#8217;t
the most important reasons. Some languages (such as C) are designed to allow
pieces of a program to be compiled independently. These pieces are eventually
combined into a final <I>executable</I> program by a tool called the
<I>linker<A NAME="Index264"></A></I>. This process is called <I>separate
compilation<A NAME="Index265"></A><A NAME="Index266"></A></I>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Separate compilation has many benefits. A
program that, taken all at once, would exceed the limits of the compiler or the
compiling environment can be compiled in pieces. Programs can be built and
tested one piece at a time. Once a piece is working, it can be saved and treated
as a building block. Collections of tested and working pieces can be combined
into <A NAME="Index267"></A><I>libraries</I> for use by other programmers. As
each piece is created, the complexity of the other pieces is hidden. All these
features support the creation of large
programs</FONT><A NAME="fnB26" HREF="#fn26">[26]</A><A NAME="Index268"></A><A NAME="Index269"></A><FONT FACE="Georgia">.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Compiler debugging<A NAME="Index270"></A>
features have improved significantly over time. Early compilers only generated
machine code, and the programmer inserted print statements to see what was going
on. This is not always effective. Modern compilers can insert information about
the source code into the executable program. This information is used by
powerful <A NAME="Index271"></A><I>source-level debuggers</I> to show exactly
what is happening in a program by tracing its progress through the source
code.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Some compilers tackle the
compilation-speed problem by performing <A NAME="Index272"></A><I>in-memory
compilation</I>. Most compilers work with files, reading and writing them in
each step of the compilation process. In-memory compilers keep the compiler
program in RAM. For small programs, this can seem as responsive as an
interpreter.
</FONT><A NAME="_Toc462979716"></A><A NAME="_Toc472654720"></A><BR></P></DIV>
<A NAME="Heading69"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The compilation process<A NAME="Index273"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To program in C and C++, you need to
understand the steps and tools in the compilation process. Some languages (C and
C++, in particular) start compilation by running a
<I>preprocessor<A NAME="Index274"></A></I> on the source code. The preprocessor
is a simple program that replaces patterns in the source code with other
patterns the programmer has defined (using <I>preprocessor
directives<A NAME="Index275"></A></I>). Preprocessor directives are used to save
typing and to increase the readability of the code. (Later in the book,
you&#8217;ll learn how the design of C++ is meant to discourage much of the use
of the preprocessor, since it can cause subtle bugs.) The pre-processed code is
often written to an intermediate file.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Compilers usually do their work in two
passes. The first pass <I>parses<A NAME="Index276"></A></I> the pre-processed
code. The compiler breaks the source code into small units and organizes it into
a structure called a <I>tree<A NAME="Index277"></A></I>. In the expression
&#8220;<B>A + B</B>&#8221; the elements &#8216;<B>A</B>&#8217;,
&#8216;<B>+,</B>&#8217; and &#8216;<B>B</B>&#8217; are leaves on the parse
tree.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <I>global
optimizer<A NAME="Index278"></A></I> is sometimes used between the first and
second passes to produce smaller, faster code.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In the second pass, the <I>code
generator<A NAME="Index279"></A></I> walks through the parse tree and generates
either assembly language code or machine code for the nodes of the tree. If the
code generator creates assembly code, the assembler must then be run. The end
result in both cases is an object module<A NAME="Index280"></A> (a file that
typically has an extension of <B>.o</B> or <B>.obj</B>). A <I>peephole
optimizer<A NAME="Index281"></A></I> is sometimes used in the second pass to
look for pieces of code containing redundant assembly-language
statements.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The use of the word

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -