C ++ basic knowledge

Source: Internet
Author: User
Tags variable scope

 

 

 

 

 

 

 

 

The data type of a class refers to the combination of member variables and processes. Through this aggregation, the class can complete the task of modeling objects.

The most challenging and creative work in object-oriented programming is the design of classes. At the same time, object-oriented programming is also based on the design of classes.

Another important aspect of object-oriented design and object-oriented programming is the relationship between classes and between objects.

In the return statement, return value I:
Return Value Transfer: the temporary bucket that is copied first by I, and a copy of I is obtained by calling
Return for reference: copy the I value directly to the accepted space (caller)

One benefit of returning a reference is that if a function returns a reference, the function call can be displayed on the left of the value assignment statement.

Because the function returned by reference is an actual unit, you must ensure that the Unit is still valid (not a temporary variable) when the function is returned)
Int & f ()
{
Int I;
Return I;
} // Error

However:
Int f ()
{
Int I;
Return I;
} // Correct

The keyword inline is used in the function life to request to expand the function in an inline manner. That is, the implementation code of the function is inserted in each place where the function is called.
This is similar to a macro. This avoids overhead. However, if the function is very large or used in many places, the executable code of the program becomes very large.
Unlike macros, inline functions are implemented through compilers. Semantics should be considered (macros do not check semantics)

All parameters without default values are placed at the beginning of the parameter, followed by parameters with default values !!

Overload: differences between the number and type of parameters

In C ++, the reason for adding extern c {} is to block the differences in the symbols they generate during compilation.

Void (double, int) and void (int, double) are overload functions.
But the returned values are different. They are not overload functions: int s (int) and double s (int)

 

In C ++, struct is passed by value:

Let's look at the following example:

Result:

To verify the result and compare it with the reference transfer, modify the following:

Result After modification:

In this case:

The result is expected:

Let's take a look at the following situation:

Result:

Summary:

In C ++, struct is implemented by passing values. We can see from Example 1 and Example 2 that, although Example 3 and Example 4 have the same results, however, it also supports the correctness of our research results.

In Example 3, a struct is transmitted, but its variable does not change. The final value changes because the variable pointing to the struct changes.

It also shows that reference is the safest way.

 

Be careful when combining function overloading with default variables:

See the following example:

Result:

You know, if this reload is used, the compiler cannot find the function you want to call. I don't know which one you want to call? I just want two people called Ninghua, a male, and a female. The two of them are obviously different individuals, but if you do not add subdivision, you will simply call them Ninghua. They will not know who they are, of course others will not know, so we should distinguish from other places, for example, a female, a female, a male, and a male. This is not to say that Ninghua is a gender, nor that she will change, but only to distinguish two different individuals.

 

Everyone is familiar with the linked list of C language. When talking about Huawei Zi, she knows that she is a female. When she hears that she is a male, she knows that she is a male. What about the linked list of C ++?

Although there is no difference in nature, I should be familiar with it. In addition, I am familiar with the C ++ memory allocation problem. The following example is replaced:

 

^_^, Hahahahahahahahahahahahahahahahahahahahahahahahaha^_^

 

Let's take a look at the knowledge points that are as cheap as Wang Xiaoji: Exception Handling

This one is relatively simple. It can be embedded in a box and then programmed normally. One example:

C ++ supports combination without a name. Here we will introduce the features of association: Examples:

Result:

Compare the following results in the example:

Result:

 

You will surely find that the order of values assigned to two variables is different. This is not an explanation: common sense!

An anonymous Union member can be used as a common variable.

Within the same scope, a variable with the same name as a member in an anonymous union cannot be defined.

And: it is not without defining a variable of the Data Type "anonymous union !!

To a certain extent, anonymous union and {} are more like! It is just a slight difference in variable scope.

Let's look at an example that supports the viewpoint:

Result:

Clear at a glance, not explained. The essence is already clear.

 

You are familiar with struct! Now let's look at its alternative usage. This is the same as female!

Errors in the following example are common. Note the following:

In this example, the output is no longer written, and the two rows with errors are not commented out. The demo is correct. Let's take a closer look:

Note that f_ptr is not a pointer to float, but a float member in Class C. This limits whether security or variable protection are of great benefit.

 

Because C ++ differs from C, and function pointers are added to the struct, we use an example to solve it:

 

The basic knowledge of C ++ is as follows:

1. Differentiate the using command and the using declaration:

Using namespace + namespace ==========> using command

Using + namespace: namespace element ==========> using Declaration

2. Before using a standard input/output stream for any keyboard input and video output, it must include:

# Include <iostream>

For the purpose of using features, it is usually as follows:

# Include <iostream>

Using namespace std;

3. using namespace std and iostream. h *. h cannot coexist. If it is an old version, *. h is used and cannot be used together. Otherwise, an error occurs.

4. <and> only one data can be output or one data can be input. If you want to have more than one data, there must be one before each data. <or>

5. The operator

Cout <n <m <endl;

Yes, it should be like this:

Cout <(n <m) <endl;

6. If a parameter operator is used, the header file iomanip must be included.

7. The operator acts on the data stream, except for the operator that controls the domain width (the operator that controls the domain width automatically clears 0 after a string or number is output, the impact of all other operators is persistent. Do not mistakenly believe that all input and output settings are restored to the default value after the statement ends.

8. Mixed input/output functions of C and C ++ may cause unpredictable errors. Use the function:

Ios: sync_with_stdio ()

This hidden danger can be eliminated.

First look at this:

This is very dangerous, and the following is safe:

9. file operation group, which must contain the header file fstream

10. Force type conversion is placed in the data type to be converted, not in parentheses:

Static_cast (int) (26) // ERROR

Static_cast (int) <26) // ERROR

Static_cast (<int> <26> // ERROR

Static_cast <int> (26) // RIGHT

<> Internal is the final data type to be converted

C ++ has four types of data conversion:

Static_cast <> () converts one data type to another

Const_cast <> () removes the constant of the Data Type

Reinterpret_cast <> (), used to change the pointer type. Use it with caution.

Dynamic_cast <> () is used for type conversion in the inheritance level.

11. C ++ allows variables to be used and defined at any time. However, the scope of the action is very messy. Be sure:

In particular, the temporary variables defined in the for loop are only valid in the for loop.

12. open () functions related to file operations accept C-style strings.

String s = "myfile. dat ";

Ifstream infile;

Infile. open (s );

Yes, it should be:

Ifstream infile;

Infile. open ("myfile. dat ");

Or:

String s = "myfile. dat ";

Ifstream infile;

Infile. open (s. c_str ());

12. When you use the operator <to read a string into a string type string, it will end when a space is reached! To read a row, use getline ()

13. The value assignment operator can be used for the string type. The right side of the operator can be a string, a C-style string, or a character. But the left side must be a string:

Similarly, the + = operator is the same. The left side can only be a string, the right side can be a string, a C-style string, or a character

+ The operator is the same, but note that the two operators on the right of the + operator cannot be strings. But it can all be string strings. It can be a C string, a string, a string, or a character.

String s1, s2 = "W. C.", s3 = "Fields ";

S1 = s2 + s3; // OK

S1 = s2 + "Fileds"; // OK

S1 = "W" + s3; // OK

S1 = "W. C." + "Fields"; // ERROR

14. For the following functions of the string type: erase, insert, replace, and substr, the first parameter passed to them cannot be greater than or equal to the length of the string.

If the length of an error exceeds the length range, the program terminates abnormally, even though exceptions can be caught.

15. It is invalid to call an undeclared function. Each system function must also be declared, usually containing the corresponding standard header file.

16. It is incorrect to specify a return value for a function.

17. The following two definitions of the main function are portable:

Int main (){}

Int main (int argc, char * argv [])

18. Because the function returns the actual unit by referencing the return method, you must ensure that the Unit still exists after the function returns.

19. The keyword inline is used for function declaration rather than function definition:

The following program is incorrect:

You need to modify the following code. The following code is correct:

Because the inner function swap is visible only at the end of the file defined by swap, it is invisible to main, so the compiler will notice that the swap function cannot be found.

 

20. default parameters should be provided in the function declaration rather than the function definition:

The following code is incorrect:

Let's see what went wrong and what went wrong:

Look, if you don't know it, you should write it like this:

21. For objects that are dynamically allocated, it is a bad programming habit to delete an object without using delete before the object exceeded its scope: dynamically applying for an object in the action, delete in another scope (not necessarily)

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.