Introduction to C++ (Part 1)

Table of contents

1. Namespace

1.1 Basic concepts of namespaces

1.2 Definition of Namespace

1.3 Use of namespaces

Two, C++ input && output


1. Namespace

1.1 Basic concepts of namespaces

In C/C++, there are a large number of variables, functions, and classes to be learned later. The names of these variables, functions, and classes will all exist in the global scope, which may cause many conflicts
. The purpose of using the namespace is to localize the name of the identifier
to avoid naming conflicts or name pollution. The appearance of the namespace keyword is aimed at this kind of problem.

#include<iostream>
using namespace std;

int rand=0;
int main()
{
	cout << rand << endl;
}

The result of running the above code is as follows:

 From such a result, it can be found that the naming conflicts with the rand function in the C++ standard library. Such conflicts cannot be resolved in the C language, so C++ introduces a new method --> namespace keyword: namespace

For example, the objects provided in the C++ standard library are all stored in the std namespace, just like the cin, cout, endl, and swap functions.

So we will see using namespace std in C++ programs; the function of this sentence is mainly to expand the content in the namespace (like putting it in the global area). Seeing this, there must be doubts, why should the namespace What about unfolding? Reason: The compiler will not take the initiative to find the content in the namespace, so it will report an error when compiling and linking

namespace lx
{
	int a = 0;
}
 
int main()
{
	cout << a << endl;
	return 0;
}

1.2 Definition of Namespace

①General definition of namespace

namespace lx //定义命名空间lx
{
	int a = 0; //成员变量
	void  print(int x) //成员函数
	{
		cout << x << endl;
	}
}

Both variables and functions can be defined in the namespace

②Nested definition of namespace

namespace lx //定义命名空间lx
{
	int a = 0; //成员变量
	void  print(int x) //成员函数
	{
		cout << x << endl;
	}
	namespace lx2  //嵌套定义lx2
	{
		int b = 1;
		int Add(int x,int y)
		{
			return x + y;
		}
	}
}

③ Define discontinuous namespaces

namespace lx 
{
	int a = 0; 
	void  print(int x) 
	{
		cout << x << endl;
	}
	namespace lx2  
	{
		int b = 1;
		int Add(int x,int y)
		{
			return x + y;
		}
	}
}

double c = 1.0;
namespace lx
{
	int c = 1;
}

Namespaces can be discontinuous, just as multiple namespaces with the same name can exist in the same project, and finally the compiler will merge these namespaces into one namespace

1.3 Use of namespaces

As mentioned above, if the namespace is not expanded, it will not be accessed. The content in the namespace is only limited to the namespace, and will not be directly called outside the namespace.

① Use using namespace to expand the content in the namespace

namespace lx
{
	int c = 1;
}
using namespace lx;

 
int main()
{
	cout << c << endl;
	return 0;
}

②Use namespace identifiers and scope qualifiers to introduce namespace members

namespace lx
{
	int c = 1;
}


 
int main()
{
	cout << lx :: c << endl;
	return 0;
}

Namespace identifier is "lx" Scope qualifier is "::"

③ Use using to expand some members of the namespace

namespace lx 
{
	int a = 0; 
	void  print(int x) 
	{
		cout << x << endl;
	}
	namespace lx2  
	{
		int b = 1;
		int Add(int x,int y)
		{
			return x + y;
		}
	}
}
using lx::print;
using lx::lx2::Add;

 
int main()
{
	int a = 0;
	int b = 1;
	print(a);
	cout << Add(a, b) << endl;
	return 0;
}

Note: The above introduces the print function and the Add function through using. When using the member functions in the namespace, the namespace identifier "lx" and the scope qualifier "::" are no longer added, which will be more convenient when writing programs. . In addition, it should be noted that when a variable with the same name as the variable in the namespace is defined in the main function, the variable in the main function will be accessed first when accessing. If you need to access the variable in the namespace, you need to use the namespace identifier specifier and scope qualifier specify access to variables in the namespace

like:

namespace lx 
{
	int a = 0; 
	void  print(int x) 
	{
		cout << x << endl;
	}
	namespace lx2  
	{
		int b = 2;
		int Add(int x,int y)
		{
			return x + y;
		}
	}
}
using lx::print;
using lx::lx2::b;
using lx::lx2::Add;

 
int main()
{
	int a = 0;
	int b = 1;
	print(b);
	cout << Add(a,lx::lx2:: b) << endl;
	return 0;
}

 

Two, C++ input && output

Note:
1. When using the cout standard output object (console) and the cin standard input object (keyboard), you must include the < iostream > header file
and use std according to the usage method of the namespace.
2. cout and cin are global stream objects, and endl is a special C++ symbol that means newline output. They are all included in the
header file containing <iostream>.
3. << is a stream insertion operator and >> is a stream extraction operator.
4. It is more convenient to use C++ for input and output, and does not need to manually control the format like printf/scanf input and output.
The input and output of C++ can automatically identify the variable type.
5. In fact, cout and cin are objects of type ostream and istream respectively. >> and << also involve operator overloading and other knowledge. We will learn this knowledge
later, so we just simply learn how to use them here. Later we will have
a chapter to learn more about the usage and principle of IO flow.
Note: In the early standard library, all functions were implemented in the global domain, declared in the header file with the
suffix of . , and in order to correctly use the namespace,
it is stipulated that the C++ header file does not contain .h; the old compiler (vc 6.0) also supports the <iostream.h> format, and the subsequent compiler does not support it,
so use <iostream>+std The way.


int main()
{
	int a = 0;
	int b = 1;
	double c = 2.0;
   cout<< a << b << c << endl;//自动识别变量类型
	return 0;
}

Conventions for using the std namespace:

std is the namespace of the C++ standard library. How to expand std to make it more reasonable?

1. In daily practice, it is recommended to directly use namespace std, which is very convenient.
2. When using namespace std is expanded, the standard library is fully exposed. If we define a type/object
/function with the same name as the library, there will be conflicts. This problem rarely occurs in daily practice, but
it is easy to occur when there are many codes and large scale in project development. Therefore, it is recommended to use it in project development. When using it like std::cout, specify the namespace +
using std::cout to expand common library objects/types, etc.
 

Today's knowledge is shared here! ! ! Thanks for the support!

Guess you like

Origin blog.csdn.net/m0_72532428/article/details/130310349