[C++] Basic introduction (1): domain, namespace, C++ input & output


Table of contents

1. domain

1.1. Use of Domains

1.2. The :: scope qualifier

2. Namespace

2.1. Nature of Namespaces

2.1.1. In addition to defining variables, functions and types can also be defined in the namespace.

2.1.2. Namespaces can be nested multiple times

2.1.3. During the compilation stage, namespaces with the same name at the same level can be automatically merged

2.2. Use of namespaces

2.2.1. Designated access

2.2.2. Expand all

2.2.3. Partial expansion

3. C++ input & output


Before introducing the namespace, let's introduce the concept of domain :


1. domain

Scope is related to access and life cycle.

1.1. Use of Domains

  1. Variables with the same name cannot be defined repeatedly in the same domain.
  2. Variables with the same name can be defined in different domains.

Take c language as an example:

  If you define an a variable in the global domain, you cannot continue to define a variable in the global domain:

 But you can define a variable in the local domain:

 Local priority principle:


1.2. The :: scope qualifier

  Note:  If the :: scope qualifier is left blank, it means that the global scope is accessed by default.


2. Namespace

Why does C++ need namespaces?

Since C is very prone to naming conflicts in large projects , there is the origin of the C++ namespace.

A namespace is to define a domain: a namespace domain.

What if the variable name we defined conflicts with the name in the library?

 We can use namespaces to solve:

 Let's demonstrate a compiler lookup rule :

 We can access variables in namespace scopes through scope scope qualifiers:


2.1. Nature of Namespaces

2.1.1. In addition to defining variables, functions and types can also be defined in the namespace.


2.1.2. Namespaces can be nested multiple times


2.1.3. During the compilation stage, namespaces with the same name at the same level can be automatically merged

  Multiple namespaces with the same name are allowed in the same project, and the compiler will finally merge them into the same namespace.

  In order to avoid conflicts, C++ defines the things in the standard library into its own official namespace. The name of this namespace is std, and std is the namespace defined by the content of the official C++ library.


2.2. Use of namespaces

Three ways to use namespaces:

2.2.1. Designated access

Before the accessed object, add the namespace name and scope qualifier.

 


2.2.2. Expand all

Use using namespace + namespace name to import.


2.2.3. Partial expansion

Use using to introduce a member in the namespace.


3. C++ input & output

  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 by namespace usage.

  <<   stream insertion operator   >> stream extraction operator

  In fact, cout and cin are objects of type ostream and istream respectively. >> and << also involve operator overloading and other knowledge, which will be introduced in a later blog.

  cout and cin are global stream objects , and endl is a special C++ symbol that represents newline output. They are all included in the <iostream> header file.

cout is the abbreviation of console out, the console output is characterized by automatic identification type.

cin is the abbreviation of console in, the console input can also automatically identify the type.

 endl newline means endline, newline.

  It is more convenient to use C++ 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.

  

Disadvantages:

  When outputting, it is troublesome to specify the number of output digits such as the double type. In this case, it is recommended to use c language to print.

  All in all, when it is finally implemented, the one that is more convenient will be used first.


at last

  I finally stepped into C++, keep going! !

Guess you like

Origin blog.csdn.net/vpurple_/article/details/126861398
Recommended