Auto variables, namespaces, using roles and scopes in C ++

Source: Internet
Author: User

Auto variables, namespaces, using roles and scopes in C ++
Zookeeper

1. usage of the auto keyword

A: Automatic variables can automatically obtain types and outputs, similar to generic variables.

B: automatic variable, which can realize automatic loop of one-dimensional arrays

C: The corresponding constant must be used for Automatic loop.

2. auto variables. Examples of auto matching types are as follows:

3. Use the auto keyword to automatically cycle one-dimensional arrays

# Include

# Include

# Include

Usingnamespacestd;

Voidmain ()

{

// Define a one-dimensional array. The following array name is a pointer constant.

Intnum [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Cyclically traverse the Array

For (autodata: num)

{

Cout <

}

Cout <

// When it becomes a two-dimensional array, it cannot be output directly through auto.

// Auto automatically loops begin endl; must be a constant of an array

Doublenum2 [2] [5] = {1.0, 2.0, 3.0, 4.5, 5, 6, 7, 8, 9, 10 };

For (autodata: num2) // Generic C ++ syntax, looping a one-dimensional array, is a constant

{

Cout <

For (inti = 0; I <5; I ++)

{

Std: cout <

}

Cout <

}

System ("pause ");

}

The running result is as follows:

4. header files

In C ++, The. h file header is not used to distinguish C ++ and C ++.

5. C ++ focuses on type. It is a strong type language and strictly checks the type.

6. Output Function in C ++, wide character output, initial value assignment method, output operator,: Domain Controller

# Include // In C ++, The. h file header is not used to distinguish between C ++ and C ++.

# Include

Usingnamespacestd;

Voidmain ()

{

Inta = 5;

// Assign A value in C ++: A variable name (value) B: variable name = value. Use these two methods.

Intb (5 );

Cout <

// Assign values using parentheses

Doublec (3.5 );

Cout <

// Assign values by equal sign

Char x pStr = "1234 ";

Cout <* pStr <"" <

// Assign values using parentheses

Char * str ("china ");

Cout <* str <"" <

// Wide character, man, awesome

Wchar_t * str2 (L "china ");

Wcout <* str2 <"" <

System ("pause ");

}

# Include

Voidmain ()

{

// Output character <

// Std namespace

//: Domain Controller

Std: cout <"hello world ";

System ("pause ");

}

7. When a file uses the variables declared in another file, C ++ requires that the extern keyword be added to the display to call this global variable.

8. Call a function in the namespace where the variable already exists.

A: namespace

# Include "iostream"

# Include "stdlib. h"

Namespacemyspace

{

Inta (15 );

Voidprint ()

{

Std: cout <"" <std: endl;

}

}

NamespacemyspaceA

{

Inta (25 );

Voidprint ()

{

Std: cout <"AAAAAAAAAAAA" <

}

}

Voidmain ()

{

Inta (5 );

// If You Want to directly access the namespace without being affected by the above a variable

// The variable in. The namespace name must be added here.

Std: cout <"myspace a =" <

// Call the variable a defined in myspaceA

Std: cout <"myspaceA a =" <

Std: cout <"main a =" <

Std: cout <std: endl;

// Call the functions in the namespace

Myspace: print ();

MyspaceA: print ();

System ("pause ");

}

The running result is as follows:

B: unnamed namespace

# Include "iostream"

// You can directly call parameters in a namespace without naming them.

Namespace

{

Inta (3 );

Voidprint ()

{

Std: cout <"gogogo ";

}

}

Voidmain ()

{

// It indicates that a can be called directly without a namespace.

Std: cout <a <

Print ();

Getchar ();

}

The output result is:

3

Gogogo

C: all the data, functions, classes, and objects in the namespace are common, and the internal structure is common by default.

The example is as follows:

# Include

// All data, functions, classes, and objects in namespace are shared.

Namespacerunmove

{

Inty (5 );

Int (* padd) (int, int); // function pointer interface

Inty1 (5 );

Classmyclass

{

// Variables in the class are private by default.

Public:

Inta;

};

}

// Corresponding to the above function pointer

Intadd (inta, intb)

{

Returna + B;

}

Intaddp (inta, intb)

{

Std: cout <a <"" <

Returna + B;

}

// The struct is transparent by default (public)

StructMyStruct

{

Intb;

Private:

Inta; // Private

};

Voidmain ()

{

// All data, functions, classes, and objects in namespace are shared.

MyStructstruct1; // The internal structure is public by default.

Struct1. B = 20;

Std: cout <struct1. B <std: endl;

Std: cout <runmove: y <"" <

Runmove: padd = addp;

Std: cout <runmove: padd (5, 6) <std: endl;

Getchar ();

}

9. When using the using Keyword, it must be behind the namespace

Scope:

A: When using is used in the function body, the using scope starts from the using line of code to the end of the function body.

B: When the function uses using in vitro, the scope starts from the using line of code to the end of the file.

Case study:

# Include

# Include

Namespacemydata

{

Inta (6 );

}

Namespaceyourdata

{

Inta (8 );

}

// If you want to use the explicit space of mydata and use the using Keyword, put the using

// After the mydata namespace

Usingnamespacemydata;

Usingnamespaceyourdata;

// If the value of using is the same as that of another variable, an ambiguous error will occur. Add the namespace modifier.

Voidgo ()

{

// If the namespace is inside the block statement, the scope is from the definition to the end of the statement.

Std: cout <mydata: a <std: endl;

}

// Using namespacemydata; // The scope is from the beginning to the end of the Code.

Voidmain ()

{

Std: cout <mydata: a <std: endl;

Std: cout <yourdata: a <std: endl;

System ("pause ");

}

10. nested namespace: alias for namespace, expansion of namespace

# Include

Namespacerunrunrunrun

{

Inta (10 );

Char * str ("gogogogo ");

Namespacerun // namespace nesting

{

Inta (9 );

}

}

// Expand the namespace

Namespacerunrunrunrun

{

Int y (5 );

// Int a (15); incorrect redefinition

}

// Note that r is an alias, not the latter.

Namespacer = runrunrunrun; // alias for the namespace

Voidmain ()

{

// The namespace can be nested.

Std: cout <r: run: a <std: endl;

Std: cout <r: y <std: endl;

Std: cout <r: a <std: endl;

System ("pause ");

}

11. Questions about default parameters

A: default parameters must be placed on the right.

B: The default parameters are not allowed.

# Include

# Include

# Include

// Default parameters must be placed on the right

// Non-default values are not allowed in the default parameters.

Voidprint (intc, inta = 1, intd = 2, intb = 3)

{

Std: cout <a <

}

Voidprintf (doublec)

{

Std: cout <"hello, world" <

}

Voidmain ()

{

// Print (1, 2, 3 );

// The function pointer does not have default parameters. All input data is required.

Void (* pt1) (intc, inta, intd, intb) = print;

Pt1 (, 1,); // function pointer call, no default

Print (100 );

System ("pause ");

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.