?? ch01.htm
字號:
presses a button or chooses from a menu--and the program must respond. Programs are
becoming increasingly interactive, and it has became important to design for that
kind of functionality.
<HR>
</DL>
<P>Old-fashioned programs forced the user to proceed step-by-step through a series
of screens. Modern event-driven programs present all the choices at once and respond
to the user's actions.</P>
<P>Object-oriented programming attempts to respond to these needs, providing techniques
for managing enormous complexity, achieving reuse of software components, and coupling
data with the tasks that manipulate that data.</P>
<P>The essence of object-oriented programming is to treat data and the procedures
that act upon the data as a single "object"--a self-contained entity with
an identity and certain characteristics of its own.
<H4 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">C++ and Object-Oriented
Programming</FONT></H4>
<P>C++ fully supports object-oriented programming, including the four pillars of
object-oriented development: encapsulation, data hiding, inheritance, and polymorphism.
Encapsulation and Data Hiding When an engineer needs to add a resistor to the device
she is creating, she doesn't typically build a new one from scratch. She walks over
to a bin of resistors, examines the colored bands that indicate the properties, and
picks the one she needs. The resistor is a "black box" as far as the engineer
is concerned--she doesn't much care how it does its work as long as it conforms to
her specifications; she doesn't need to look inside the box to use it in her design.</P>
<P>The property of being a self-contained unit is called encapsulation. With encapsulation,
we can accomplish data hiding. Data hiding is the highly valued characteristic that
an object can be used without the user knowing or caring how it works internally.
Just as you can use a refrigerator without knowing how the compressor works, you
can use a well-designed object without knowing about its internal data members.</P>
<P>Similarly, when the engineer uses the resistor, she need not know anything about
the internal state of the resistor. All the properties of the resistor are encapsulated
in the resistor object; they are not spread out through the circuitry. It is not
necessary to understand how the resistor works in order to use it effectively. Its
data is hidden inside the resistor's casing.</P>
<P>C++ supports the properties of encapsulation and data hiding through the creation
of user-defined types, called classes. You'll see how to create classes on Day 6,
"Basic Classes." Once created, a well-defined class acts as a fully encapsulated
entity--it is used as a whole unit. The actual inner workings of the class should
be hidden. Users of a well-defined class do not need to know how the class works;
they just need to know how to use it. Inheritance and Reuse When the engineers at
Acme Motors want to build a new car, they have two choices: They can start from scratch,
or they can modify an existing model. Perhaps their Star model is nearly perfect,
but they'd like to add a turbocharger and a six-speed transmission. The chief engineer
would prefer not to start from the ground up, but rather to say, "Let's build
another Star, but let's add these additional capabilities. We'll call the new model
a Quasar." A Quasar is a kind of Star, but one with new features.</P>
<P>C++ supports the idea of reuse through inheritance. A new type, which is an extension
of an existing type, can be declared. This new subclass is said to derive from the
existing type and is sometimes called a derived type. The Quasar is derived from
the Star and thus inherits all its qualities, but can add to them as needed. Inheritance
and its application in C++ are discussed on Day 12, "Inheritance," and
Day 15, "Advanced Inheritance." Polymorphism The new Quasar might respond
differently than a Star does when you press down on the accelerator. The Quasar might
engage fuel injection and a turbocharger, while the Star would simply let gasoline
into its carburetor. A user, however, does not have to know about these differences.
He can just "floor it," and the right thing will happen, depending on which
car he's driving.</P>
<P>C++ supports the idea that different objects do "the right thing" through
what is called function polymorphism and class polymorphism. Poly means many, and
morph means form. Polymorphism refers to the same name taking many forms, and is
discussed on Day 10, "Advanced Functions," and Day 13, "Polymorphism."
<H3 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">How C++ Evolved</FONT></H3>
<P>As object-oriented analysis, design, and programming began to catch on, Bjarne
Stroustrup took the most popular language for commercial software development, C,
and extended it to provide the features needed to facilitate object-oriented programming.
He created C++, and in less than a decade it has gone from being used by only a handful
of developers at AT&T to being the programming language of choice for an estimated
one million developers worldwide. It is expected that by the end of the decade, C++
will be the predominant language for commercial software development.</P>
<P>While it is true that C++ is a superset of C, and that virtually any legal C program
is a legal C++ program, the leap from C to C++ is very significant. C++ benefited
from its relationship to C for many years, as C programmers could ease into their
use of C++. To really get the full benefit of C++, however, many programmers found
they had to unlearn much of what they knew and learn a whole new way of conceptualizing
and solving programming problems.
<H3 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">The ANSI Standard</FONT></H3>
<P>The Accredited Standards Committee, operating under the procedures of the American
National Standards Institute (ANSI), is working to create an international standard
for C++.</P>
<P>The draft of this standard has been published, and a link is available at <TT>www.libertyassociates.com</TT>.</P>
<P>The ANSI standard is an attempt to ensure that C++ is portable--that code you
write for Microsoft's compiler will compile without errors, using a compiler from
any other vendor. Further, because the code in this book is ANSI compliant, it should
compile without errors on a Mac, a Windows box, or an Alpha.</P>
<P>For most students of C++, the ANSI standard will be invisible. The standard has
been stable for a while, and all the major manufacturers support the ANSI standard.
We have endeavored to ensure that all the code in this edition of this book is ANSI
compliant.
<H3 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Should I Learn C
First?</FONT></H3>
<P>The question inevitably arises: "Since C++ is a superset of C, should I learn
C first?" Stroustrup and most other C++ programmers agree. Not only is it unnecessary
to learn C first, it may be advantageous not to do so. This book attempts to meet
the needs of people like you, who come to C++ without prior experience of C. In fact,
this book assumes no programming experience of any kind.
<H3 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Preparing to Program</FONT></H3>
<P>C++, perhaps more than other languages, demands that the programmer design the
program before writing it. Trivial problems, such as the ones discussed in the first
few chapters of this book, don't require much design. Complex problems, however,
such as the ones professional programmers are challenged with every day, do require
design, and the more thorough the design, the more likely it is that the program
will solve the problems it is designed to solve, on time and on budget. A good design
also makes for a program that is relatively bug-free and easy to maintain. It has
been estimated that fully 90 percent of the cost of software is the combined cost
of debugging and maintenance. To the extent that good design can reduce those costs,
it can have a significant impact on the bottom-line cost of the project.</P>
<P>The first question you need to ask when preparing to design any program is, "What
is the problem I'm trying to solve?" Every program should have a clear, well-articulated
goal, and you'll find that even the simplest programs in this book do so.</P>
<P>The second question every good programmer asks is, "Can this be accomplished
without resorting to writing custom software?" Reusing an old program, using
pen and paper, or buying software off the shelf is often a better solution to a problem
than writing something new. The programmer who can offer these alternatives will
never suffer from lack of work; finding less-expensive solutions to today's problems
will always generate new opportunities later.</P>
<P>Assuming you understand the problem, and it requires writing a new program, you
are ready to begin your design.
<H3 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Your Development
Environment</FONT></H3>
<P>This book makes the assumption that your computer has a mode in which you can
write directly to the screen, without worrying about a graphical environment, such
as the ones in Windows or on the Macintosh.</P>
<P>Your compiler may have its own built-in text editor, or you may be using a commercial
text editor or word processor that can produce text files. The important thing is
that whatever you write your program in, it must save simple, plain-text files, with
no word processing commands embedded in the text. Examples of safe editors include
Windows Notepad, the DOS Edit command, Brief, Epsilon, EMACS, and vi. Many commercial
word processors, such as WordPerfect, Word, and dozens of others, also offer a method
for saving simple text files.</P>
<P>The files you create with your editor are called source files, and for C++ they
typically are named with the extension <TT>.CPP</TT>, <TT>.CP</TT>, or <TT>.C</TT>.
In this book, we'll name all the source code files with the <TT>.CPP</TT> extension,
but check your compiler for what it needs.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Most C++ compilers don't care what
extension you give your source code, but if you don't specify otherwise, many will
use <TT>.CPP</TT> by default.</P>
<P>DO use a simple text editor to create your source code, or use the built-in editor
that comes with your compiler. DON'T use a word processor that saves special formatting
characters. If you do use a word processor, save the file as ASCII text. DO save
your files with the <TT>.C</TT>, <TT>.CP</TT>, or <TT>.CPP</TT> extension. DO check
your documentation for specifics about your compiler and linker to ensure that you
know how to compile and link your programs.
<HR>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Compiling the Source
Code</FONT></H3>
<P>Although the source code in your file is somewhat cryptic, and anyone who doesn't
know C++ will struggle to understand what it is for, it is still in what we call
human-readable form. Your source code file is not a program, and it can't be executed,
or run, as a program can.</P>
<P>To turn your source code into a program, you use a compiler. How you invoke your
compiler, and how you tell it where to find your source code, will vary from compiler
to compiler; check your documentation. In Borland's Turbo C++ you pick the <TT>RUN</TT>
menu command or type</P>
<PRE><FONT COLOR="#0066FF">tc <filename>
</FONT></PRE>
<P>from the command line, where <TT><</TT>filename<TT>></TT> is the name of
your source code file (for example, <TT>test.cpp</TT>). Other compilers may do things
slightly differently.
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>If you compile the source code from
the operating system's command line, you should type the following:
<BLOCKQUOTE>
<P>For the Borland C++ compiler: <FONT COLOR="#0066FF"><TT>bcc</TT> <filename></FONT><BR>
<BR>
For the Borland C++ for Windows compiler: <FONT COLOR="#0066FF"><TT>bcc</TT> <filename>
</FONT><BR>
<BR>
For the Borland Turbo C++ <FONT COLOR="#0066FF">compiler: <TT>tc</TT> <filename>
</FONT><BR>
<BR>
For the Microsoft compilers: <FONT COLOR="#0066FF"><TT>cl</TT> <filename></FONT>
</BLOCKQUOTE>
<P>
<HR>
</BLOCKQUOTE>
<P>After your source code is compiled, an object file is produced. This file is often
named with the extension <TT>.OBJ</TT>. This is still not an executable program,
however. To turn this into an executable program, you must run your linker.
<H3 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">Creating an Executable
File with the Linker</FONT></H3>
<H3 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H3>
<P>C++ programs are typically created by linking together one or more OBJ files with
one or more libraries. A library is a collection of linkable files that were supplied
with your compiler, that you purchased separately, or that you created and compiled.
All C++ compilers come with a library of useful functions (or procedures) and classes
that you can include in your program. A function is a block of code that performs
a service, such as adding two numbers or printing to the screen. A class is a collection
of data and related functions; we'll be talking about classes a lot, starting on
Day 5, "Functions."</P>
<P>The steps to create an executable file are
<DL>
<DD><B>1.</B> Create a source code file, with a <TT>.CPP</TT> extension.<BR>
<B><BR>
2.</B> Compile the source code into a file with the <TT>.OBJ</TT> extension.<BR>
<B><BR>
3.</B> Link your OBJ file with any needed libraries to produce an executable program.
</DL>
<H3 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">The Development
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -