3-19 (C++ foundation)

1. Getting started with C++

Keywords: C++ has a total of 63 keywords.

1. Namespace;

In c/c++, variables, functions, and classes exist in large numbers. The names of these variables, functions, and classes are all in the global scope, and the names may also cause conflicts. The purpose of using namespaces is to localize the names of identifiers.


The namespace keyword deals with namespaces .

Namespace definition:

The namespace space name {}; {} is the members of the namespace, that is, these members belong to the space. These member variables cannot be used arbitrarily, and they need to be used according to the namespace usage rules.

Such as namespace N1

{

int a;

int add(int ret)

{

return 0;

}

}

Namespaces can also nest namespaces.


The use of namespaces: 3 methods.

The first type : add the namespace name and scope qualifier.

For example, N1::a, :: is the scope qualifier.

The second : use using to introduce namespace members.

Such as using n1::a, so that a can be used casually.

Using is to expand.

The third type : expand all members of the namespace

Such as: using namespace N1;., so that all members in N1 can be used directly.


2. Input/output

When using cout standard output (console) and cin standard input (keyboard) i, you must use the header file <iostream>, and the std standard namespace.

using namespace std; means that all things in c++ can be used, std is the name of all things in c++, and when using is expanded, you can use it at will. .

Such as

cin>>a;//Enter a

cin>>a>>b; enter a, b

cout<<a<<b<<end1; means output a, b, and wrap, end1 means wrap.



3. Default parameters (spare tire parameters)

The default parameter is to specify a default value for the parameter of the function when it is declared or defined, and then when the function is called, if no value is assigned to the actual parameter, the formal parameter parameter is used, otherwise the actual parameter is used.

classification:

Full default parameters: all parameters are assigned;

Semi-default parameters: Some parameters are not assigned values.


Note: The semi-default parameters must be given in order from right to left and cannot be assigned at intervals.

Default parameters cannot appear at the same time as declaration and definition.

The default parameters must be constants or global variables .

C language is not supported.


4. Function overloading

In C++, several functions of the same name with similar functions are declared in the same scope, and the parameter lists (number/type/order) of these functions with the same name must be different.

Function overloading has nothing to do with the return value . If only the function return value is different, it cannot be said to be function overloading.


Why C++ can function overloading, but C language can’t, mainly because C++ has name modification rules.



extern"C"

Sometimes C ++ project which may need to be some function in accordance with the c style to compile, in front of the function add extern "C", meaning that tells the compiler to the c function in accordance with rules to compile ,



5. Quote

Reference is not a new definition of a variable, but an alias for an existing variable , the compiler will not open up memory space for the reference variable, it shares the same space with the variable it refers to.


Format: reference type & reference variable name (object name) = reference entity

int& ra=a; ra is a reference to a, its variable name.

Note: The reference type must be the same type as the reference entity.


Reference characteristics:

The reference must be initialized when it is defined.

A variable can have multiple references.


Often quoted:

const int a=10;

int& ra=1; //wrong writing, because a is read-only, ra is readable and writable, this will change the value of a

const int& ra=a; correct writing.

The referenced authority must be less than or equal to the authority of the referenced entity.

Permissions do not adapt to assignments with ordinary variables, adapt to references and pointers.

Non-const can be assigned to const and non-const, and const can only be assigned to const.


scenes to be used:

Make parameters:

Do the return value:

If a function wants to be quoted as the return value, it must require its return variable to exist after going out of this scope . If it does not exist, then the reference cannot be used as the return value.

Using reference return can reduce the creation of temporary variables and improve efficiency.

Variables declared by static will have a longer life cycle and will be in the static area. static variables defined only once, only once in the definition.


Comparison of the efficiency of passing parameters and passing references:

Take the finger as the parameter or return value type. During the transfer and return, the function does not directly pass the actual parameter or the variable itself directly returns, but passes the actual parameter or a temporary copy of the return variable , so the efficiency is very low.




The difference between references and pointers:

References must be initialized when they are defined; pointers are not required;

A reference cannot refer to another entity after initializing an entity; and a pointer can point to any entity of the same type at any time;

There are no references to NULL; there are NULL pointers.

The meaning in sizeof is different: the result of the reference is the size of the type of the referenced entity, but the pointer always occupies 4/8 of the bytes in the address space.

Adding by reference means that the referenced entity is incremented by 1; by means of adding a pointer, the pointer is shifted backward by the size of the type.

There are multi-level pointers, but no multi-level references.

Different access entity types: pointers need to be dereferenced, and the reference compiler handles it by itself

References are safer than pointers.


6, inline function

A function modified by inline is called an inline function. The c++ compiler will expand where the inline function is called during compilation. There is no overhead of function pushing on the stack. Inline function improves the efficiency of program operation.


characteristic:

Inline is the practice of changing space for time, which saves the overhead of calling functions, so functions with very long codes are not suitable for inline functions.

Inline is just a suggestion to the compiler, the compiler will automatically optimize and choose the right one.

The declaration and definition of inline should be together.


Interview test site

The advantages and disadvantages of macros:

advantage:

Enhance the reusability of the code.

Improve performance.

Disadvantages:

It is not convenient to debug.

This leads to poor code readability, poor maintainability, and misunderstandings.

There is no type safety check.

C++ which technologies can replace macros:

Constant definition-"const

Function definition-"inline function.




Guess you like

Origin blog.51cto.com/15085121/2666195