About the basic content of C + +

Source: Internet
Author: User

A. C and C + + basic differences: C language is simple, but the use of more complex, C + + syntax is more difficult, focus on thought, use simple (object-oriented)

A. Header file:
The header file for the C + + standard library does not take. h, the most commonly used is iostream
C-Language header files can still be used, stdio.h or Cstdio

B. Input and output

cout<< output
CIN >> Input
Endl; change the line;
cout and no longer use placeholders, he can automatically recognize placeholders
scanf and printf can continue to use

C.

Introduced the concept of namespaces
using namespace Std;  

Two. Data types in C + +
1. C + + is fully compatible with all data types in C (c + + compatible with all grammars)
2. The bool type is added, the number of bytes is 1, the value is true (not 0), False (0);
3. Before displaying the bool variable, use Boolalpha to be true or false in the form of a real bool value
4. void* conversions with other types of pointers are restricted
Void* cannot assign values directly to other types of pointers and must be cast
Any type of pointer can be assigned directly to void*

Three. Union, enumeration, structure in C + +
1. In C + + once the design is successful, the use of the keyword is not required after
2. Enumeration is more restrictive (enumerations in C + + are a separate data type)
3. Struct members can have functions that directly access their members in the function,
4. Structure objects can continue to use. Or, access members, member functions.
5. Members of the structure have added access properties:
Public: Open, the members he has decorated can be accessed in both people and places
Private: A member that has been modified by him can only be used in member functions
Protected

6. The structure will automatically call a function-constructor when it is created;
A. No return value
B. The same name as the structure
C. Parameters can be arbitrarily
7. The structure will automatically release a function when it is created-the hook function;
A. No return value, parameter
b.~ structure Name


Four. References
1. A reference is an alias, a "nickname", another name for an identifier
2. The reference must be initialized,
3. References can refer not only to identifiers, but also to immediate numbers (rvalue, literal, constant), but must be added with the Const property
4. You cannot define a null reference, a "wild reference" or "dangling reference" is present (data that references push memory and should not be used when heap memory is freed).
5. The reference can only be one-time and cannot be changed (once the reference succeeds, he is a variable, and the identity remains the same)
6. A reference can also be used as an argument to a function, the object he refers to is the argument of the function, and the reference can achieve the effect of the pointer
A. Sharing variables between functions
B. Improve the efficiency of the transfer of parameters (higher than the pointer)
C. When parameters are used
Note: You can use references to stop using pointers.
References can not replace pointers;
7. The application can also be used as the return value of a function, but must not return a reference to a local variable


Five. Memory management operators in C + +
1. Using New/delete in C + + to manage memory
2. New allows memory to be initialized in memory
3. Constructor is automatically called when creating a struct object using new, and the destructor is called automatically when delete

Summarize:
1. Malloc/free is a function in the standard library, you need to add a header file when you use it, New/delete is an operator in the C + + language
2. The malloc return value is a void* pointer, and new returns a pointer of type
3. The struct function is not called when malloc is created, and free does not call the hook function, but new/delete automatically calls
4. Use malloc to calculate its own number of bytes when using memory, while using new will automatically calculate
4. [] can request and release arrays with new and delete
New[] cannot be mixed with Delete, must be released using delete[]
How many objects will be called using new[]/delete[] to create the number of times the hook and construct
5. New/delete and Malloc/free cannot be mixed
6. Delete cannot be released repeatedly, but can release null pointers
7. If using new to request memory failure will produce an exception "Std::bad_alloc", the exception of the capture and processing after the explanation

Six. Functions in C + +
1. Functions in C + + can be executed in front of the main function, not in the C language
2. Function names in C + + can be the same as long as the parameters are different (function overloading, not just parameters)
The function can have the same name, not the same name as the true meaning, except that the name of the function is added in the compilation period (parameters of the function) and the return value is not counted.
3. Inline functions
The inline function compiles the function into binary, and then copies the function directly in the desired position, rather than jumping over
This can increase the execution efficiency of the program by reducing the number of stacks and calls in the stack, but this increases the size of the executable file.
The difference between an inline function and a macro function is essentially the same, but inline functions can check for parameters, or they can have return values.
Inline return value function name (parameter)
{

}
4. The parameters of a function in C + + can have a default value,
1. The default parameter of the function is to the right
2. Default parameters should be used sparingly, because overload errors may be referenced
3. The default argument for a function is that the compiler helps assign values at compile time, so the parameters can be assigned only by global variables, static variables, and literal values.

About the basic content of C + +

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.