1.C++ Primer(第5版)草译

最近公司效益不好,没什么项目,只能翻翻旧代码,看看资料,百无聊赖的时候,弄了一本《C++ Primer》(第5版)复习一下,同时顺便练习一下久违的英语翻译。由于我并不是英语专业出身的,而且也只是闲暇时草草地翻译,所以难免出现差错,欢迎提出批评指正。(^__^)

Chapter 1. Getting Started

第1章 快速入门

Contents

内容

Section 1.1 Writing a Simple C++ Program

1.1 编写一个简单的C++程序

Section 1.2 A First Look at Input/Output

1.2 初窥Input/Output

Section 1.3 A Word about Comments

1.3 关于注释

Section 1.4 Flow of Control

1.4 流程控制

Section 1.5 Introducing Classes

1.5 类的介绍

Section 1.6 The Bookstore Program

1.6 书店管理程序

Chapter Summary

本章总结

Defined Terms

术语定义

This chapter introduces most of the basic elements of C++: types, variables, expressions, statements, and functions. Along the way, we’ll briefly explain how to compile and execute a program.

本章将介绍C++中最基本的一些元素,包括:类型、变量、表达式、语句和函数。同时,还对如何编译和执行一个程序进行简要的介绍。

After having read this chapter and worked through the exercises, you should be able to write, compile, and execute simple programs. Later chapters will assume that you can use the features introduced in this chapter, and will explain these features in more detail.

阅读完本章并且做完了后面的习题后,你应该可以学会如何编写、编译并执行一些简单的程序。后面的章节将会假设你已经掌握了本章介绍的功能,并且我们将对这些功能作更详细的阐述。

The way to learn a new programming language is to write programs. In this chapter, we’ll write a program to solve a simple problem for a bookstore.

学习一门新的程序设计语言的方法是用这门语言去编写程序。因此在这一章里,我们将为一家书店编写程序以解决一个简单的问题。

Our store keeps a file of transactions, each of which records the sale of one or more copies of a single book. Each transaction contains three data elements:

我们的书店保存了一份交易文档,其中的每一笔交易都记录了某一种书的的销售情况(如,每笔交易卖了一本或多本)。并且,每一笔交易都包括三种数据元素(data elements)。

0-201-70353-X 4 24.99

The first element is an ISBN (International Standard Book Number, a unique book identifier), the second is the number of copies sold, and the last is the price at which each of these copies was sold. From time to time, the bookstore owner reads this file and for each book computes the number of copies sold, the total revenue from that book, and the average sales price.

第一个数据元素是ISBN号(国际标准书号,每一种独特的书的标记号),第二个数据元素是这本书的销售量,最后一个数据元素是这些书相应的售价。书店的店主每隔一段时间就会查阅这个交易文档,计算每一种书的销售量、相应的总收入和平均售价。

To be able to write this program, we need to cover a few basic C++ features. In addition, we’ll need to know how to compile and execute a program.

为了编写这个程序,我们必须简单了解一下C++的一些基本功能。此外,我们还要知道怎样编译和执行一个程序。

Although we haven’t yet designed our program, it’s easy to see that it must

尽管我们还没设计我们的书店管理程序,但不难知道,有一些步骤是必不可少的,包括:

• Define variables

•定义变量

• Do input and output

•进行输入、输出操作

• Use a data structure to hold the data

•用数据结构来存储数据

• Test whether two records have the same ISBN

•检测两笔交易记录是否含有相同的ISBN号

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

•加入一个循环结构以处理交易文档中的每一条记录

We’ll start by reviewing how to solve these subproblems in C++ and then write our bookstore program.

首先,我们先探讨一下如何用C++解决这些子问题,然后再编写我们的书店管理程序。

1.1. Writing a Simple C++ Program

1.1.编写一个简单的C++程序

Every C++ program contains one or more functions, one of which must be named main. The operating system runs a C++ program by calling main. Here is a simple version of main that does nothing but return a value to the operating system:

每个C++程序都包含一个或多个函数,而且这些函数中必须有一个被命名为main。操作系统以调用main函数的方式来运行C++程序。下面是main函数的一个简单版本,它除了返回一个值给操作系统外,就没做其它事情了。

1 int main()
2 {     
3     return 0;
4 }

A function definition has four elements: a return type, a function name, a (possibly empty) parameter list enclosed in parentheses, and a function body. Although main is special in some ways, we define main the same way we define any other function.

函数定义的四要素:一个返回类型,一个函数名,一个被圆括号括着的形参表(这个表可能为空)和一个函数体。从某种方式来看,尽管main函数有一些特殊的意味,但我们还是以定义其它任何函数的方式来定义main函数。

In this example, main has an empty list of parameters (shown by the () with nothing inside). § 6.2.5 (p. 218) will discuss the other parameter types that we can define for main.

在这个例子中,main函数只有一个空的形参表(正如main后面一对括号()所示,里面没有任何东西)。§6.2.5 (第218页)将会讨论我们可以为main函数定义的其它形参类型。

The main function is required to have a return type of int, which is a type that represents integers. The int type is a built-in type, which means that it is one of the types the language defines.

main函数必须有一个int的返回类型,int表示了一个整数类型。int是一个内置类型(built-in type),这就意味着int是由C++语言定义的类型之一。

The final part of a function definition, the function body, is a block of statements starting with an open curly brace and ending with a close curly:

最后一部分是函数定义,即函数体,它是一个首尾均被花括号括着的语句块。

1 {    
2     return 0;
3 }

The only statement in this block is a return, which is a statement that terminates a function. As is the case here, a return can also send a value back to the function’s caller. When a return statement includes a value, the value returned must have a type that is compatible with the return type of the function. In this case, the return type of main is int and the return value is 0, which is an int.

这个语句块中仅有的一个语句是return语句,它是一个使函数终止的语句。如这个例子所示,return语句可以返回一个值给函数的调用者。当return语句带上一个值时,这个返回值的类型必须与函数的返回类型相兼容。在这个例子中,main函数的返回类型是int;相应的返回值是0,也是一个int类型的值。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Note

注解

Note the semicolon at the end of the return statement. Semicolons mark the end of most statements in C++. They are easy to overlook but, when forgotten, can lead to mysterious compiler error messages.

请注意一下return语句尾部的分号。在C++中,分号是大多数语句的结束标记。分号很容易被忽视,然而一旦忘记了写上分号,就会导致一些莫名其妙的编译器错误信息。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

On most systems, the value returned from main is a status indicator. A return value of 0 indicates success. A nonzero return has a meaning that is defined by the system. Ordinarily a nonzero return indicates what kind of error occurred.

在大多数系统上,main函数的返回值可作为一个状态指示器。返回值为0就意味着成功。一个非零的返回值所表达的意义则由系统来定义。通常,一个非0的返回值表明发生了某些错误。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Key Concept: Types

关键概念:类型

Types are one of the most fundamental concepts in programming and a concept that we will come back to over and over in this Primer. A type defines both the contents of a data element and the operations that are possible on those data. The data our programs manipulate are stored in variables and every variable has a type. When the type of a variable named v is T, we often say that “v has type T” or, interchangeably, that “v is a T.”

类型是编程领域中最基本的概念之一,而且在本书中会反复涉及到这个概念。类型不仅定义了数据元素的内容,而且还定义了在这些数据上可以进行的操作。我们的程序所处理的数据被存储在变量中,而且每个变量都有一个类型。假设一个名为v的变量的类型是T,于是我们经常会这样说,“v具有一个类型T”,或是变通地说,“v是一个T”。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

原文链接: https://www.cnblogs.com/caesarchow/archive/2013/01/28/2880496.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/77040

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午5:45
下一篇 2023年2月9日 下午5:45

相关推荐