Writing the first program in C++

In a previous article Installing the Microsoft Visual Studio environment 2015 Express (IDE)we with you learned how to create a project in the development environment and add to it a new element (файл с расширением .cpp). Exactly in this file we will give clear commands to our computer. Unfortunately, the computer can not understand us – it understands only one language – machine code. but program thus it is impossible, many programming languages have been created.
One of these is the C++ language. This language has a certain number of special words reserved logical, whereby in Microsoft Visual Studio (as with any other development environment) you can write commands to our computer. And then using the built-in compiler, this code will be converted into machine code, that will be processed, and we will show the result of the work program.

Well, start writing program. To do this, you need to open your created project. You go in the Microsoft Visual Studio. Before you open Home.

Here is there, where it is written last, must be a newly created project – click on it. If you have not yet created projects – create, as shown in this article. Start to write the following code. Strongly recommend not copy code, and manually type. Without practice in programming it is hard… ))

написание первой программы на c++

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

// эта программа будет выводить на экран любое

// сообщение, которое мы напишем

#include <iostream>

usingnamespacestd;

intmain()

{

setlocale(LC_ALL,"rus");

cout<<"Пишем нашу первую программу на языке С++"<<endl;

return0;

}

Let's understand now, what we wrote. In 1-St and 2-nd lines are comments to our code. The entire text of the line, what is behind the double slash – // – completely ignored by the compiler. It simply does not see it. So we can anywhere in our code to leave comments – any information, which we deem necessary. Something like notes, to remember or pay attention. Comments there are multiline, because if a comment is big, It is uncomfortable in front of the beginning of each line a double slash. To create a multi-line comment, need a whole text or code, you want to ignore, placed between /* and */

In string 4 of program we see #include <iostream> – this preprocessor directive. While we will not delve into – just write so each new program. The same applies to the line 5 – this is, the so-called, namespace connection std that contains the word commands, that we will use when writing each program. In strings 7 – 14 located the main function: main ( ) { /*program code * / } When you run the program, it is always executed first and it between the two braces { } our teams are. That's a record in a row 9 – function setlocale(LC_ALL, “rus”);– will properly display on the screen Cyrillic signs. Prescribe the function in those programs, where you will be required to display the Russian text. Moving to a string 11 – Here we entered the command cout (keyword from namespace std), which is responsible for data output to the screen, operator << followed by the text. The text should be placed in quotation marks. So we show the compiler, that it is not a command for it, and the usual string. In line write any text. When the text is written, quotation marks are closed again and write the operator << for which again the compiler command – endl; (the transition to the next line). Semicolon ; to put definitely. It said tocompiler, the command finished and can proceed to the next command. Well, at the end of the block function main() visible command return 0; It said tocompiler, that at the end of the programme it is necessary to return a value 0. Also didn't hesitate so far on how, what does it mean. Think of it, as a kind of rule – so it is necessary to write in each program. Any of our program in the following lessons, will be contain such records:

C++

1

2

3

4

5

6

7

#include <iostream>

usingnamespacestd;

intmain()

{

return0;

}

Use it as a template, before writing new programs.

How to run our program, to see the text on the screen, which has been placed in the source quoted? To do this, press Ctrl + Shift + B-will begin compiling a program. If you make some mistakes in the code, Ddbugger to detect them and notify you. At the bottom of the window you will see a list of these errors. They should be correct. Well, if there are no errors in the bottom line of the window we see Assembling: successfully: 1, with errors: 0 etc. Then press the Ctrl key + F5 and see in the window that opens, a message is, that requested:

The program has worked and performed our instructions properly. If you have any difficulties or errors, that you can not fix yourself, ask questions in the comments to this article. Together, we will look :)

seocod.ru/forum/viewforum.php?f=31 mylinuxprog.blogspot.com/2018/01/c-2.html flibusta.is/a/36262 it links. But since this site does not allow the publication of references, simply copy it into your browser.