Данный материал взят с сайта old.shatalov.su и является его зеркалом

Game programming. Game creation with C++/DirectX

рус eng esp
Внимание! Данный сайт не обновляется. Новая версия: shatalov.su

First program - Hello World on C++

Creation date: 2012-09-29 14:09:16
Last edited on: 2012-10-05 17:14:29

Hello World program listing

Now we look at the source code of Hello World program on C++. All that this program does is printing Hello World!!! on the screen.

!1? // My first program #include <iostream> #include <conio.h> using namespace std; int main () { cout << "Hello world!!!\n"; _getch(); return 0; }?1!

Let's look at the different parts of the Hello World and what they does.

Comments in C++

!1? // My first program ?1!

The first string is comment. Compiler doesn't pay attention to such strings. Compiler differs comments because of double slash. Such comments active to the end of the string.

There is another way to comment text /* */. It works like this:

!1? /* First string of comment ... Compiler skip all these strings ... Last string of comment */?1!

Comments are using for explanations. Comments are very useful in large programs consists of thousands strings. Also, sometimes it is useful to comment some code for testing purposes.

Directives in C++

Two strings #include follows comment in our Hello world program:

!1? #include <iostream> #include <conio.h>?1!

Symbol # says that this string is preprocessor directive, and keyword include says compiler that it is needed paste files iostream and conio.h instead of these strings. For example we have two files:

!1? file1.txt first file text file2.txt #include <file1.txt> second file text?1!

Before compilation processor replace string #include with content of the first file. At the end second file looks like this:

!1? file2.txt first file text second file text?1!

Besides include there is others preprocessor directives. Preprocessor directive #define MY_CONSTANT 1 says compiler substitute in the program all MY_CONSTANT with one. We'll discover this directive more in the next tutorials.

Preprocessor is a special part of compiler which process it's directives before source code compilation.

In the example above used text files (.txt). Usually with include directive place special files - header files. Standard header files in C++ doesn't have extension, and files with .h extension inherited from C language.

In file iostream there is definition of the object cout which we will use for output data on the screen.

In file conio.h there is definition of the function _getch().

After that directive using namespace follows. This is not preprocessor directive. I will explain this directive and namespaces in next lessons when we'll learn multifile programs. This directive is needed to use functions from files conio.h and iostream.

main() function in C++

Function is one of the most important notions in all programming languages.

In C++ you meet functions from the first program. Because all code is in the function main().

You need to define function before using it. Definition of the function includes: function header and function body.

Function header consist of function name, in this case - main, after which stays round brackets. Word befor function name says which data type function will return. In our case main must return integer number.

Function body begins after function header and enclosed in culry braces - {}. Function body consists of operators. Operators are instructions to processor (not to compiler!) to do some actions. Processor is a piece of iron inside a computer.

Our program has three operators:

!1? cout << "Hello world!!!\n"; _getch(); return 0;?1!

Usualy each operator located on one string. Though this is not required, I recommend you always put each operator on separate string.

Each operator end with semicolon ;. It is obligatory. Don't forget about it.

First string of function body:

!1? cout << "Hello world!!!\n";?1!

This operator output text on the screen. Operator << is called insertion operator and it's copy what is located at the right in the left part. That is Hello world!!!\n is copied in cout.

cout is object (we will meet object later) which is used to work with standard output stream. Just think that it prints text to the screen.

"Hello world!!!\n" is a string literal. String literals enclosed in double quotes. Now, what about \n? \n symbol sequence in the string literal says, that next symbols will be printed in the new string. That's why console cursor in our program is on the next string after Hello world!!! \n is cursor transition to the beginning of the next string.

\n is the example of escape-sequences.

Our operator ends with semicolon.

Next operator:

!1? _getch();?1!

In this string function _getch() is called. This function asks user to push keyboard button. After input symbol is not printed on the screen.

If we didn't put this operator, then we wouldn't see anything: our program would started, printed message and immediately closed. Amd with that operator program waits for user to press any key.

_getch is a function and before using, it must be defined (like function main()). It has been done already and we just can use it. But compiler need to know about function definition. What's why we add to our program header file conio.h.

Functions are easy to use. You need to print function name, round brackets and semicolon.

And the last operator:

!1? return 0;?1!

This operator is required for any function and main() is not exception. Function returns it's value (in this case - zero) to operation system or compiler (if it is debugging).

Only one program and how much information! By now you need to understand next things:

Program running

At this moment in your text editor must be full listing of the Hello world program. To change source code in ready application, we need to compile and link it.

Process of compilation and linkage is called building.

To build program press F7. To run, press F5. After running console window will open and you'll see text string.

Excercises

1. Add to Hello World program output of one more string. Output operator must be placed before return operator.

2. After each string stop program with calling _getch() function.