📄 c++ for dummys.txt
字号:
Part I
Introduction to C++ Programming
In this part . . .
Both the newest, hottest ight simulator and the
simplest yet most powerful accounting programs
use the same basic building blocks. In this part, you dis
cover the basic features you need to write your killer
application.
Chapter 1
Writing Your First C++ Program
In This Chapter
Finding out about C++
Installing Dev-CPP from the accompanying CD-ROM
Creating your first C++ program
Executing your program
Okay, so here we are: No one here but just you and me. Nothing left to do
but get started. Might as well lay out a few fundamental concepts.
A computer is an amazingly fast but incredibly stupid machine. A computer
can do anything you tell it (within reason), but it does exactly what it’s told —
nothing more and nothing less.
Perhaps unfortunately for us, computers don’t understand any reasonable
human language — they don’t speak English either. Okay, I know what you’re
going to say: “I’ve seen computers that could understand English.” What you
really saw was a computer executing a program that could meaningfully under
stand English. (I’m still a little unclear on this computer-understanding-language
concept, but then I don’t know that my son understands my advice, either, so
I’ll let it slide.)
Computers understand a language variously known as computer language or
machine language. It’s possible but extremely difficult for humans to speak
machine language. Therefore, computers and humans have agreed to sort of
meet in the middle, using intermediate languages such as C++. Humans can
speak C++ (sort of), and C++ is converted into machine language for the com
puter to understand.
10 Part I: Introduction to C++ Programming
Grasping C++ Concepts
In the early 1970s, a consortium of really clever people worked on a computer
system called Multix. The goal of Multix was to give all houses inexpensive
computer access to graphics, e-mail, stock data, pornography (just kidding),
whatever. Of course, this was a completely crazy idea at the time, and the
entire concept failed.
A small team of engineers working for Bell Labs decided to save some portion
of Multix in a very small, lightweight operating system that they dubbed Unix
(Un-ix, the single task version of Mult-ix, get it?).
Unfortunately for these engineers, they didn’t have one large machine but a
number of smaller machines, each from a different manufacturer. The standard
development tricks of the day were all machine-dependent — they would have
to rewrite the same program for each of the available machines. Instead, these
engineers invented a small, powerful language named C.
C caught on like wildfire. Eventually, however, new programming techniques
(most notably object-oriented programming) left the C programming language
behind. Not to be outdone, the engineering community added equivalent new
features to the C language. The result was called C++.
The C++ language consists of two basic elements:
Semantics: This is a vocabulary of commands that humans can under
stand and that can be converted into machine language, fairly easily.
and
Syntax: This is a language structure (or grammar) that allows humans to
combine these C++ commands into a program that actually does some
thing (well, maybe does something).
Think of the semantics as the building blocks of your C++ program and the
syntax as the correct way to put them together.
What’s a program?
A C++ program is a text file containing a sequence of C++ commands put
together according to the laws of C++ grammar. This text file is known as the
source file (probably because it’s the source of all frustration). A C++ source
file carries the extension .CPP just as a Microsoft Word file ends in .DOC or
an MS-DOS (remember that?) batch file ends in .BAT. The concept extension
.CPP is just a convention.
Chapter 1: Writing Your First C++ Program 11
The point of programming in C++ is to write a sequence of commands that can
be converted into a machine-language program that actually does what we want
done. The resulting machine-executable files carry the extension .EXE. The act
of creating an executable program from a C++ program is called compiling or
building (the subtle difference between the two is described in Chapter 22).
That sounds easy enough — so what’s the big deal? Keep going.
How do I program?
To write a program, you need two specialized computer programs. One (an
editor) is what you use to write your code as you build your .CPP source file.
The other (a compiler) converts your source file into a machine-executable
.EXE file that carries out your real-world commands (open spreadsheet, make
rude noise, deflect incoming asteroid, whatever).
Nowadays, tool developers generally combine compiler and editor into a single
package — a development environment. After you finish entering the commands
that make up your program, you need only click a button to create the exe
cutable file.
The most popular of all C++ environments is a Microsoft product, Visual
C++.NET (pronounced “Visual-C-plus-plus-DOT-net”). All programs in this
book compile and execute with Visual C++.NET; however, many of you may
not already own Visual C++.NET — and at $250 bucks a pop, street price, this
may be a problem.
Fortunately, there are public-domain C++ environments. We use one of them
in this book — the Dev-CPP environment. A recent version of Dev-CPP envi
ronment is included on CD-ROM enclosed at the back of this book (or you can
download the absolutely most recent version off the Web at www.bloodshed.
net).
You can download quite a range of public-domain programs from the Internet.
Some of these programs, however, are not free — you’re encouraged — or
required — to pay some (usually small) fee. You don’t have to pay to use Dev-
C++, but you can contribute to the cause if you like. See the Web site for details.
I have tested the programs in this book with Dev-C++ version 4.9.8.0; they
should work with other versions as well. You can check out my Web site at
www.stephendavis.com for a list of any problems that may arise with future
versions of Dev-C++ or Windows.
Dev-C++ is not some bug-ridden, limited edition C++ compiler from some fly-
by-night group of developers. Dev-C++ is a full-fledged C++ environment. Dev-
C++ supports the entire C++ language and executes all the programs in this
book (and any other C++ book) just fine, thank you.
12 Part I: Introduction to C++ Programming
Dev-C++ does generate Windows-compatible 32-bit programs, but it does not
easily support creating programs that have the classic Windows look. If you
want to do that, you’ll have to break open the wallet and go for a commercial
package like Visual Studio.NET. Having said that, I strongly recommend that
you work through the examples in this book first to learn C++ before you tackle
Windows development. They are two separate things and (for the sake of
sanity) should remain so in your mind.
Follow the steps in the next section to install Dev-C++ and build your first C++
program. This program’s task is to convert a temperature value entered by the
user from degrees Celsius to degrees Fahrenheit.
The programs in this book are compatible with Visual C++.NET and the C++
section of Visual Studio.NET (which are essentially the same thing). Use the
documentation in the Visual C++ .NET for instructions on installing C++. True,
the error messages generated by Visual C++.NET are different (and often just
as difficult to decipher), but the territory will seem mysteriously familiar. Even
though you’re using a different songbook, you shouldn’t have much trouble
following the tune.
Installing Dev-C++
The CD-ROM that accompanies this book includes the most recent version of
the Dev-C++ environment at the time of this writing.
The Dev-C++ environment comes in an easy-to-install, compressed executable
file. This executable file is contained in the DevCPP directory on the accom
panying CD-ROM. Here’s the rundown on installing the environment:
1. Navigate to and double-click the file devcpp4980.exe, or (in Windows)
click StartRun.
Double-clicking the file installs the environment automatically. (Note
that 4.9.8.0, the version number, will be different on any newer ver
sion of Dev-C++ you downloaded off the Web.)
If you clicked StartRun, type x:\devcpp\devcpp4980 in the Run
window that appears, where x is the letter designation for your
CD-ROM drive (normally D but perhaps E — if one doesn’t work,
try the other).
Dev-C++ begins with a warning (shown in Figure 1-1) that you’d better
uninstall any older version of Dev-C++ you may have hanging around,
and then reboot and start over. (Starting an installation with a threat
is an inauspicious way to begin a relationship, but everything gets
better from here.)
Chapter 1: Writing Your First C++ Program 13
Figure 1-1:
You must
uninstall
earlier
versions of
Dev-C++
before you
begin the
installation
process.
2. If you don’t have to uninstall an old version of Dev-C++, skip to Step 4;
if you do have to uninstall, abort the current installation process by
closing the Run window.
Don’t get upset if you’ve never even heard of Dev-C++ and you still get the
warning message. It’s just a reminder.
3. Okay, if you’re on this step, you’re uninstalling: Open the Dev-CPP
folder and double-click the Uninstall.exe file there.
The uninstall program does its thing, preparing the way for the new instal
lation; the End User Legal Agreement (commonly known as the EULA)
appears.
4. Read the EULA and then click OK if you can live with its provisions.
Nope, the package really won’t install itself if you don’t accept. Assuming
you do click OK, Dev-C++ opens the window shown in Figure 1-2 and offers
you some installation options. The defaults are innocuous, with two
exceptions:
You must leave the Mingw compiler system. . . option enabled.
The Associate C and C++ Files to Dev-C++ option means that double-
clicking a .CPP file automatically opens Dev-C++ rather than some
other program (such as Visual C++ .NET, for example). It is possible,
but difficult, to undo this association.
Don’t check this option if you also have Visual Studio.NET installed. Dev-
C++ and Visual Studio.NET coexist peacefully on the same machine, but
what Visual Studio has done, let no man cast assunder. You can still open
your .CPP files with Dev-C++ by right-clicking on the file and selecting Open
With. Personally, I prefer to use this option, even with Visual Studio.NET
installed. It doesn’t cause any problems, and Dev-C++ starts a lot faster
than Visual Studio.
14 Part I: Introduction to C++ Programming
Figure 1-2:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -