Typical commercial programs have hundreds or even thousands of lines of code. In order to reduce the complexity involved in writing such large programs, they have to be broken into smaller, less complex parts. Functions, along with classes, enable the programmer to do this. Functions and classes are the building blocks of a C++ program.

典型的商用程序通常有几百甚至上千行代码。为了降低编写如此大规模代码的复杂度,程序员必须将其分解为较小、较简单的模块,例如函数和类。函数和类是构造C++程序的基本模块。

A funcion is a block of statements called by name to carry out a specific task.C++ has a varity of built-in, pre-written, functions in the standard library.

函数是用函数名来调用执行的具有特定功能的语句块。在C++的标准库中有很多固有的、预先定义的库函数。

The variables defined inside a function are auto(automatic) by default. Every time a function(including main()) is entered, storage for each auto variable is allocated. When the function is completed, the allocated storage is freed, and any values in the auto variables are lost. Such variables are known as local vaiables and are known only within the function in which they are defined. If you do not specify a storage class, auto is assumed by default.

在函数内部定义的变量是auto(自动)存储类型。每次进入函数(包括main())时,都为每个auto存储类型的变量重新分配内存空间;函数结束时,将分配的内存空间释放,存储在auto变量中的任何数值都将丢失。这样的变量称为局部变量,只能在定义它的函数内部访问。如果没有指定变量的存储类型,那么变量的存储类型,那么变量的存储类型就默认为auto。

Static variables, like auto variables, are local to the function in which they are defined. however, unlike auto variables, static variable are allocated storage only once and so retain their values even after the function terminates.

和auto变量一样,static变量对定义它们的函数而言,也是局部的。但不同于auto变量的是,static变量只分配一次存储空间,因此即使函数结束后仍然保持其值不变。

The scope of a variable refers to the part of the program in which a variable can be accessd. There are two typew of scope: block scope and global scope.

变量的作用域指的是程序中可能访问到变量的部分,有两种作用域类型:块作用域和全局作用域。

A block is one or more statements enclosed in braces { and } that also includes variable declarations. A variable declared in a block is accessible only within that block.

一个语句块就是位于花括号{和}之内包含变量声明的一条或多条语句。在一个语句块内声明的变量只能在该语句块中被访问。

Because global variables are known, and therefore can be modified, within every function, they can make a program difficult to debug and maintain. Global variables are not a substitute for function arguments. Strictly speaking, apart from its own local variables, a function should have have access only to the data specified in the function parameter list.

由于全局变量可以在任何函数中被访问,因此也可以被任何函数修改,这给程序的调试和维护带来困难。全局变量不是函数实参的替代品。严格来讲,除了局部变量之外,函数应该只能访问函数形参列表中指定的数据。

An object is a component of a program that knows how to perform certain actions and knows how to interact with other parts of the program. An object consists of one or more data values, which define the state or properties of the object, and functions that can be applied to the object. The functions associated with an object represent what can be done to the object and how the object behaves. An object can be anything such as a person, a computer or even an intangible such as a bank account.

对象是程序一个组成部分,它知道如何执行特定的操作,知道如何去和程序的其它部分进行交互。一个对象包含一个或多个数据值及应用于这个对象的一些函数,其中这些数据值定义了这个对象的状态或者属性,和对象关联的函数表示可以对该对象做些什么及该对象是如何运作的。对象可以是任何事物,例如人、计算机,甚至是像银行帐户这样的无形的东西。

That is, a class defines both the type of data and the operations that can be applied to that data. Including both the data and functions into one unit, the class, is called encapsulation.

类同时定义了数据的类型和可作用于这些数据之上的操作。类把数据和函数包含在一起,成为一个整体。这个过程称为封装。

The members of the class are divided into private and public members. The keywords private and public specify the access control level for data and function members of the class. Data members declared with private access control are accessible only to members functions of the class and unavailable to any functions that are not members of the class. This is called information hiding and prevents the data from being changed except from within the class.

类的成员有私有成员和公有成员两种。用关键字private和public指定类的数据成员和成员函数的访问控制权限。被声明为private的数据成员只能被这个类的成员函数访问,其他的非成员函数无权访问,称为信息的隐藏。其目的是防止数据在这个类外被修改。

Members declared with public access are accessible in any part of a program. The public member functions are known as the public interface of the class.

声明为public的数据成员可以在程序的任何部分被访问。公有成员函数称为类的公共接口。

C++ has a set of built-in data types such as char, int and float. Each one of these data types has a unique range of allowable values and a set of allowable operations and functions. For example, the float data type has a range of positive and negative values which are different from the range of numbers that can be held with an int data type. An allowable operation on both the int and float data types is the square root function sqrt(). A string function such as length() is not applicable to either float or int and is consequently not allowed.

C++包含一组内置的数据类型,例如字符型、整型和浮点型。每一种数据类型都有一个唯一的允许聚会范围及一组允许的操作和函数。例如,浮点型所允许的正负取值范围与整型所允许的取值范围就是不同的。平方根函数sqrt()既适用于整型数据,也适用于浮点型数据。像length()这样的字符串处理函数就不能应用于整型或浮点型数据。

The details of how negative numbers are stored or how many bits of storage are used to store a value are not necessary in order to use a float or an int type variable. All that is required to know is what functions and operations can be used with a particular data type, and what is the range of allowable values for that data type. In other words, the implementation details of the data types are hidden from the programmer. This is called data abstraction.

要使用一个整型或浮点型变量,不需要知道负数是怎样存储的或者用多少位去存储一个数据值的这些细节。对于一个特定的数据类型,只需要了解哪些函数和操作可以使用及这个数据类型的合法取值范围是什么。换句话说,数据类型的实现细节对程序员是隐藏的,称为数据抽象。