Team LiB
Previous Section Next Section

Chapter 1. Getting Started

第一章 快速入门


Section 1.1 Writing a Simple C++ Program


Section 1.2 A First Look at Input/Output


Section 1.3 A Word About Comments


Section 1.4 Control Structures


Section 1.5 Introducing Classes


Section 1.6 The C++ Program


Chapter Summary


Defined Terms


This chapter introduces most of the basic elements of C++: built-in, library, and class types; variables; expressions; statements; and functions. Along the way, we'll briefly explain how to compile and execute a program.

本章介绍 C++ 的大部分基本要素:内置类型、库类型、类类型、变量、表达式、语句和函数。在这一过程中还会简要说明如何编译和运行程序。

Having read this chapter and worked through the exercises, the reader should be able to write, compile, and execute simple programs. Subsequent chapters will explain in more detail the topics introduced here.


Learning a new programming language requires writing programs. In this chapter, we'll write a program to solve a simple problem that represents a common data-processing task: A bookstore keeps a file of transactions, each of which records the sale of a given book. Each transaction contains an ISBN (International Standard Book Number, a unique identifier assigned to most books published throughout the world), the number of copies sold, and the price at which each copy was sold. Each transaction looks like

要学会一门新的程序语言,必须实际动手编写程序。在这一章,我们将缩写程序解决一个简单的数据处理问题:某书店以文件形式保存其每一笔交易。每一笔交易记录某本书的销售情况,含有 ISBN(国际标准书号,世界上每种图书的唯一标识符)、销售册数和销售单价。每一笔交易形如:

   0-201-70353-X 4 24.99

where the first element is the ISBN, the second is the number of books sold, and the last is the sales price. Periodically the bookstore owner reads this file and computes the number of copies of each title sold, the total revenue from that book, and the average sales price. We want to supply a program do these computations.

第一个元素是 ISBN,第二个元素是销售的册数,最后是销售单价。店主定期地查看这个文件,统计每本书的销售册数、总销售收入以及平均售价。我们要编写程序来进行这些计算。

Before we can write this program we need to know some basic features of C++. At a minimum we'll need to know how to write, compile, and execute a simple program. What must this program do? Although we have not yet designed our solution, we know that the program must

在编写这个程序之前,必须知道 C++ 的一些基本特征。至少我们要知道怎么样编写、编译和执行简单的程序。这个程序要做什么呢?虽然还没有设计解决方案,但是我们知道程序必须:

  • Define variables


  • Do input and output


  • Define a data structure to hold the data we're managing


  • Test whether two records have the same ISBN

    测试是否两条记录具有相同的 ISBN。

  • Write a loop that will process every record in the transaction file


We'll start by reviewing these parts of C++ and then write a solution to our bookstore problem.

我们将首先考察 C++ 的这些部分,然后编写书店问题的解决方案。

Team LiB
Previous Section Next Section