Instructions for use To whom is this tutorial directed?

This tutorial is for those people who want to learn programming in C++ and do not necessarily have any previous knowledge of other programming languages. Of course any knowledge of other programming languages or any general computer skill can be useful to better understand this tutorial, although it is not essential.

It is also suitable for those who need a little update on the new features the language has acquired from the latest standards.

If you are familiar with the C language, you can take the first 3 parts of this tutorial as a review of concepts, since they mainly explain the C part of C++. There are slight differences in the C++ syntax for some C features, so I recommend you its reading anyway.

The 4th part describes object-oriented programming. The 5th part mostly describes the new features introduced by ANSI-C++ standard.

Structure of this tutorial

The tutorial is divided in 6 parts and each part is divided on its turn into different sections covering a topic each one. You can access any section directly from the section index available on the left side bar, or begin the tutorial from any point and follow the links at the bottom of each section.

Many sections include examples that describe the use of the newly acquired knowledge in the chapter. It is recommended to read these examples and to be able to understand each of the code lines that constitute it before passing to the next chapter.

A good way to gain experience with a programming language is by modifying and adding new functionalities on your own to the example programs that you fully understand. Don't be scared to modify the examples provided with this tutorial, that's the way to learn!

本教材章节之间相互独立,互不影响。

第一段代码:

image-20221005165153035

Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

以(#)开头的行是用于预处理器的指令。

它们不是带有表达式的常规代码,而是编译器预处理器的指示。

在本例中,指令 # include 告诉预处理器包含iostream标准文件。

这个特定的文件(iostream)包含c++中基本标准输入输出库的声明,之所以包含它,是因为稍后将在程序中使用它。

All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.

标准c++库的都在命名空间中声明,命名空间的名称为std。

因此,为了访问其功能,我们用这个表达式声明将使用这些组件。

这一行在使用标准库的c++中非常常见,实际上它将包含在本教程中包含的大多数源代码中。

int main ()

This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.

The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.

Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.

这一行对应于main函数定义的开头。

主函数是所有c++程序开始执行的点,与它在源代码中的位置无关。

在此函数之前或之后是否定义了其他名称的函数并不重要——此函数定义中包含的指令总是在任何c++程序中最先执行的指令。

出于同样的原因,所有c++代码都必须有一个main函数。

“Hello World”