C++ introductory guide and actual combat second step HelloWorld and detailed explanation

review

In the previous section, we wrote the following code to complete the writing of the HelloWorld program:

#include<iostream>
using namespace std;

int main(){
    
    
	cout <<"Hello World";
	return 0;
}

This section will explain the code and explain the professional terms.
Insert picture description here

Paraphrase

#include<iostream>
In the above code, #include means to introduce a header file, which is a "tool library". When we are writing code, we often use some functions. These functions are in some header files (tool library). At this time, through the #include code, the specified header file (tool library) can be introduced into Among the currently written code.
In the HelloWorld program, the functions that need to be completed are the tools needed to display HelloWorld and HelloWorld when the program is running, in the header file iostream. Because the syntax (referring to the usage method) of #include is #include<header file name>, you only need to fill in the iostream in angle brackets and write as #include<iostream>.

int main(){}
In the above code, int main() represents the entry of a program, we mainly look at main, and we will explain in detail after int. There can only be one main function in the entire program, which represents an entry point for your program. When the computer executes your program, it needs to know where your program started. This main indicates where the program you are currently writing starts from. Main is followed by a pair of parentheses () followed by a pair of curly braces {}, write code within the curly brackets.

cout <<"Hello World";
The above code is the first sentence in the brackets of the main entry. Cout means output. In other words, cout means display, followed by two << symbols, followed by double quotes and a string of "Hello World". In C++, single letters or symbols such as a, b, c, d, ?, [, etc. become characters, combined to form a string, and need to be enclosed in double quotation marks. Here cout is the display string, the syntax is cout <<“你要显示的字符串”, if you want to display Hello World, write it as cout <<"Hello World";.

return 0;
This code will be explained later.

using namespace std;
In order to prevent the same "tool" names in different header files, use namespaces to distinguish which area these tools belong to. For example, in a school, there are three people named Xiao Ming, one in the first class of the sixth grade, one in the second class of the sixth grade, and one in the first class of the fifth grade. These Xiao Mings are on the playground. The principal calls the names. When Xiao Ming is called, he needs to add a grade class. Otherwise, the three Xiao Mings don’t know who they are called. Among them, the nickname of the new year class is like a namespace. This distinction is the role of the same name in different regions.
Above using namespace std;indicate using namespace std, using namespace syntax for the namespace name. (Namespace will be explained in detail later) In this way, the introduction of this namespace does not require the use of this cout "tool" such as std::cout (we use std as a region name, and cout represents this tool). We can delete the imported namespace code, and then use std::cout to output:

 #include<iostream>

int main(){
    
    
	std::cout <<"Hello World";
	return 0;
}

The above code will report an error if std is deleted, because you don’t know which cout output tool of the toolbox you want to use:
Insert picture description here
if you want to omit std::, just introduce a namespace under the header file:

#include<iostream>
using namespace std;

int main(){
    
    
	cout <<"Hello World";
	return 0;
}

The above code structure is the top part is the introduction of the header file, and then the introduction of a namespace, the following is a main entry, write the code you want to achieve in the curly braces of the entry. This structure is not a complete representation of the structure, in the following learning will gradually understand this process.

Simply put, when writing code, you generally use some "commands" provided by the system. These "commands" become keywords, such as cout, main, namespace, etc. These keywords have specific functions. The fixed use of these keywords "format", we call it grammar. The code is written through the formulated grammar, and the program is finally formed, which fulfills our needs.

For example, in the above code, I want to change Hello World to Hello Xiaoming, just change the display content. code show as below:

#include<iostream>
using namespace std;

int main(){
    
    
	cout <<"Hello Xiaoming";
	return 0;
}

In the end, it shows what I replaced:
Insert picture description here

Programming tips

In C++ code writing, most statements need to end with a semicolon to indicate the end. For example cout <<"Hello Xiaoming";, and return 0;this is the need to pay attention. And the special punctuation used in programming must be in English. Many beginners will make mistakes in this step, and this is one of the reasons. And when writing code, you need to pay attention that the program should be aligned in the same style, which will make the program clear and readable, and facilitate your own debugging and interpretation.

Knowledge points

After studying this article, you must understand the following knowledge points:
1. What is the header file and how to use it
2. Namespace and the "tools" in the namespace
3. What is main? What does the program entry mean?
4. How to display the data?
5. What is the end of most statements after the end of the sentence?
6. What brackets are the entry programs written in?

Guess you like

Origin blog.csdn.net/A757291228/article/details/107433571