Structure of a program
プログラミング言語を学ぶ一番いい方法はプログラムを書くことですので、最初のプログラムを以下に載せます。
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Hello World!
最初のパネルはプログラムのソースコードです。次のパネルはプログラムをコンパイルし実行した結果です。プログラムを編集したりコンパイルする方法はあなたが使っているコンパイラに依ります。開発環境があるかないかや、またコンパイラのバージョンにもまた左右されます。C++でのコンソールアプリケーションをどうやってコンパイルすればいいのかが分からない場合は、Compilersを参考にしたり、あなたが使っているコンパイラのマニュアルやヘルプも参考にしてください。
このプログラムはプログラマーが新しい言語などを習得するときに最初に書く典型的なプログラムで、スクリーンに"Hello World!"と1行表示します。C++で書ける最も単純なプログラムですが、どのC++のプログラムも持っている基本的な要素はすでに含まれています。1行1行コードをおっていきます。
- // my first program in C++
- 1行コメントです。スラッシュが2つ(//)で始まっている全ての1行はコメントと解釈され、プログラムの振る舞いになんら影響を及ぼしません。プログラマーはコメントを使ってソースコード中に短く説明や意図を含ませることができます。ここでは、このプログラムが何であるか概要を解説しています。
- #include <iostream>
- #で始まっている1行はプリプロセッサに指示を与えます。つまり、#で始まっている行はなんらかの式としての1行ではなくコンパイラのプリプロセッサに対して指示を与えます。ここでは、#include <iostream>の指示はプリプロセッサに iostream のヘッダファイルをインクルードすることです。この iostream のヘッダファイルはC++で基本的な入力と出力を宣言が記述されています。そして、後のプログラム中で必要となる機能のためにインクルードされています。
- using namespace std;
- 標準的なC++のライブラリ要素全てのnamespaceと呼ばれるものを宣言しています。ここでのnamespaceはstdを使われるようにするためです。つまり、stdの機能を使うために、using namespaceの式を宣言することでライブラリの実体を使えるようになります。標準的なライブラリをつかうためにこのusingの1行は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 of 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.
- cout << "Hello World";
- This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.
- cout represents the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (which usually is the screen).
cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.
Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).
- return 0;
- The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code 0). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ program.
You may have noticed that not all the lines of this program perform actions when the code is executed. There were lines containing only comments (those beginning by //). There were lines with directives for the compiler's preprocessor (those beginning by #). Then there were lines that began the declaration of a function (in this case, the main function) and, finally lines with statements (like the insertion into cout), which were all included within the block delimited by the braces ({}) of the main function.
The program has been structured in different lines in order to be more readable, but in C++, we do not have strict rules on how to separate instructions in different lines. For example, instead of
int main ()
{
cout << " Hello World ";
return 0;
}
We could have written:
int main () { cout << "Hello World"; return 0; }
All in just one line and this would have had exactly the same meaning as the previous code.
In C++, the separation between statements is specified with an ending semicolon (;) at the end of each one, so the separation in different code lines does not matter at all for this purpose. We can write many statements per line or write a single statement that takes many code lines. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it.
Let us add an additional instruction to our first program:
// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
Hello World! I'm a C++ program
In this case, we performed two insertions into cout in two different statements. Once again, the separation in different lines of code has been done just to give greater readability to the program, since main could have been perfectly valid defined this way:
int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; }
We were also free to divide the code into more lines if we considered it more convenient:
int main ()
{
cout <<
"Hello World!";
cout
<< "I'm a C++ program";
return 0;
}
And the result would again have been exactly the same as in the previous examples.
Preprocessor directives (those that begin by #) are out of this general rule since they are not statements. They are lines read and discarded by the preprocessor and do not produce any code by themselves. Preprocessor directives must be specified in their own line and do not have to end with a semicolon (;).
Comments
Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.
Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.
C++ supports two ways to insert comments:
// line comment /* block comment */
The first of them, known as line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, known as block comment, discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including more than one line.
We are going to add comments to our second program:
We are going to add comments to our second program:
/* my second program in C++
with more comments */
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! "; // prints Hello World!
cout << "I'm a C++ program"; // prints I'm a C++ program
return 0;
}
Hello World! I'm a C++ program
If you include comments within the source code of your programs without using the comment characters combinations //, /* or */, the compiler will take them as if they were C++ expressions, most likely causing one or several error messages when you compile it.