Article directory
1. Create a C++ project in Visual Studio 2019
Open Visual Studio 2019, select the "menu bar/file/new/project" option, and create a new project;
Select the console program of the C++ language on the Windows platform ;
Choose to create "Empty Project" and click the "Next" button in the lower right corner;
Enter the project name and location to create the project;
In the "Solution Explorer", right-click on "Source Files", select "Add/New Item";
In the "Add New Item" window, choose to add the "C++ File (.cpp)" file, and set the name to "hello_world.cpp";
Then, click the "Add" button in the lower right corner to add the C++ source code file;
In the "Solution Explorer", the created source code is displayed under the source file directory;
Two, C++ code writing
All C++ programs must first include the header file of the standard IO stream and use the std standard namespace;
1. iostream standard stream
Use #include "iostream"
the standard IO stream headers that include C++;
// 包含 C++ 头文件
#include "iostream"
iostream standard IO stream for processing
- standard input and output
- file input and output
etc. operation;
iostream commonly used standard input and output streams:
- cin: standard input stream , read data from the standard input device;
- cout : standard output stream , output data to standard output device;
- cerr: standard error stream , output error information to the standard error device;
- fstream : standard file input and output stream , read data from the file, output data to the file;
After including the iostream header file, the above input and output streams can be used;
2. std standard namespace
Use std
the standard namespace, in which many standard definitions are defined;
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
using namespace std;
The use standard namespace code in the above code is a preprocessing directive, which is used to inform the compiler to use std
the standard namespace in the current source code;
std
The namespace is an abbreviation of the English word "standard", which defines all elements of the standard library, such as: cout, cin, string, etc.;
- If you do not use
std
the standard namespace, you must add a prefix when using the elements , such as: , , etc.;std::
std::cout
std::cin
std::string
- If the standard namespace is used , elements such as cout , cin , and string can be used directly without adding a prefix;
std
std::
3. cout console output
In C language, use the printf function to output data to the console, and this method can still be used in C++;
// 使用 C 语言的方式在控制台输出文本
printf("printf Hello World\n");
It is more convenient to use cout standard stream output in iostream in C++;
cout
Standard output stream, which can output data to standard output (console) , where its role is to perform standard output and output content to the console;
The left shift operator << operator is used to send the string content data on the right to the stream on the left, that is, to "cout Hello World"
send the string data to cout
the standard output stream;
endl
The role of the operator is to refresh the output stream, print the content to the console and enter a new line;
// 使用 C++ 的方式在控制台输出文本
// cout 的作用是进行标准输出 , 向控制台输出内容
// C++ 中的 左移操作符 <<
// 在 C++ 语言中进行了操作符重载 进行了功能增强
// endl 的作用是 将内容打印到控制台 并且回车换行
cout << "cout Hello World" << endl;
4. Code example
Code example:
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
int main()
{
// 使用 C 语言的方式在控制台输出文本
printf("printf Hello World\n");
// 使用 C++ 的方式在控制台输出文本
// cout 的作用是进行标准输出 , 向控制台输出内容
// C++ 中的 左移操作符 <<
// 在 C++ 语言中进行了操作符重载 进行了功能增强
// endl 的作用是 将内容打印到控制台 并且回车换行
cout << "cout Hello World" << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
}
Results of the :
printf Hello World
cout Hello World
Press any key to continue . . .