C++ study notes one-hello world basic learning and the details of the use of namespaces

  实在惭愧,由于自己基础薄弱,所以打算趁这段时间提高自己基础水平。
 废话不多说下面进入hello world 的基础学习,以下是hello world 的基础代码块。
# include <iostream>
using namespace std;
int main()
{
    
    
int age;
cout << "hello world"<<endl;
cin >> age;
cout << "your age is "<< age<<endl;

}

The result of the operation is as follows.
Insert picture description here
If you look at this code, it feels very simple. I will analyze the details of the code sentence by sentence.
First, start with the #include at the beginning. The iostream here is called the input output stream, which is also the input and output stream. One more thing I want to add here is
the difference between C++ and C header file references. In C language, we will add .h when referencing system header files , such as include <sting.h>, but in C++, we refer to header files in a "cut head and tail" method, that is, remove .h and Add c at the beginning, it will become include for the above example. This is the difference between the header file C++ and the C language.
The second sentence is the namespace using namespace std;

In fact, this std is a very important thing. There are many variables and functions defined in this namespace. It will report an error to the most basic cout cin in C++, because it is named

The function has been defined under the space. If the program using namespace std; is deleted, the error will be as follows.
Insert picture description here
So what is the role of namespace? Simply put, it is to prevent the variable definition from being repeated. When multiple people collaborate on a certain part of the project at the same time, there will be repeated definitions of variables when defining variables. For example, if the variables i, j, k, etc. are all used, it will be troublesome to change them at that time. For this reason, use the variable under the namespace and block the variable, then this phenomenon can be avoided. The definition and use of namespaces will be introduced below. The basic format is namespace space name {variable/function}, remember not to add; symbol. For example, a namespace ming contains age and can display age.
namespace ming { int age = 12; void show() { cout << "ming age is " << age << endl; } }
It is worth noting that the namespace can be defined countless times, and you can add what you want to it. For example, the above code can also be written as

namespace ming {
    
    
 int age = 12;
 
}
namespace ming {
    
    
 void show() {
    
    
  cout << "ming age is  " << age << endl;
 }
}

As long as you want to add data, you can call it at any time hahaha, that's why there are so many function variables in std. This is the definition method of the namespace, so how to use it? The usage can be divided into the following three points for use:
1. Add the namespace name before the data:, this format is almighty, it can also be said to be the most basic usage, when Which variable I use, just use that variable directly. For example, in the case of the above name namespace, use the two variables inside.

int main(){
    
    
cout << ming::age;
ming::show();


}

The results of the operation are as follows
Insert picture description here

2. Use the using statement, using space name:: data.
The first method is omnipotent, but every time you want to use the variables in the space, you have to write more variable names, which will be very cumbersome. Therefore, with this kind of declaration, it can be achieved once and for all. code show as below

using ming :: age;
int main()
{
    
    
cout << age << endl;
}

The results are as follows
Insert picture description here

Then there is a problem at this time. If I define a space as hong, and the space variables and functions in it are the same as those of ming, how should I call it when I call? How does the system tell which space I call the space variable? What? First of all, if you use the first type of space name:: The way of variables will definitely not be a problem, but if you want to use the second way, there will be ambiguity. The effect is as follows
Insert picture description here

So when you want convenience is relative, if one saves trouble, the other needs to be troublesome.

3 using namespace space name
This method maximizes the convenience. You don't need to declare specific variables, but all are universal, but this way we can also see that it increases the risk of ambiguity. Once there is ambiguity, please return to the first method of use and write down the variables honestly. This is the second sentence using namespace std;

After talking about the second part, the content in the main function is described below, which mainly contains two input cin >> and output cout <<. Remember the direction of the arrow. The input and output functions are also well understood c input and c output, which are simple input and output functions. Add endl after the end, that is, endline, which will automatically wrap after the end.
The above is the main content of the simple program.
Remember to come on. Han deder.

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/106456353