Key Records of C + + programming

Source: Internet
Author: User
Tags arithmetic operators function prototype strcmp types of functions variable scope

20170723 Modification: Markdown to the Code and table compatibility is not good, changed to TINYMCE editing format, so it seems not so awkward!

Objective

The first time to write a blog, but also the first time to write a blog with markdown, I think it will be more interesting! Long-term adherence to the blog should be a very good habit, as a small white, I hope I can step by step the record of their own learning things. The first writing is Tankiang teacher's "C + + program Design" Reading notes, the main record of the book some of the knowledge points, because the content of the book will be more limited and simple, but still suitable for me this non-professional programmer to see, calculate to yourself into a door and play a C + + foundation, the next step on a good bite "c++primer" This bible, the important thing is to develop the habit of reading notes, always believe that "do not accumulate kuibu, not even thousands of miles" this sentence! Haha, kind of a way to encourage yourself, come on, chris_lzy!. First draft written on July 22, 2017 afternoon sunny

The first piece of basic knowledge
    • string constant : "abc\n" contains 4 characters, but occupies 5 bytes in memory (contains "\ n" end characters).
    • coercion type conversion operator : When coercion type conversion, get a required type intermediate variable, but the original variable type did not change, eg: ' (int) x ', if x is specified as a float type value of 3.6, cast after the conversion of the int value is 3, However, the type and value of x have not changed.
    • C + + input and output : When executing the cout statement, the output is not immediately inserted into a single data, but the data is put into the buffer until the buffer is full or endl (or ' \ n ', ends flush, etc.) is encountered, and the data in the buffer is output. and empties the buffer, and when the CIN statement is executed, the system will use a space as a spacer for the data, and a return-line key as the Terminator. So these two characters cannot be entered as character variables, but the GetChar function can.
    • Precedence of operator :! (non) > Arithmetic operators > Relational operators > && (with) and | | (non) > Assignment operators
The second process-based programming
  • conditional expression : (a>b)? A:b, if (a>b) is true, this expression value takes a, otherwise take B.
  • switch statement : ' switch (expression) {} ' expression can be any type, remember break , default.
  • While and Do-while statements : While the expression is first judged, then decide whether to enter the statement, Do-while first into a statement, and then judge the expression.
  • Break and Continue: Break is the end of the entire loop, continue is the end of this cycle into the next loop
  • Built-in functions (inline): Built-in functions should not include complex control statements, such as loops and switch statements, and the function of built-in functions is to reduce the time overhead of function calls, but increases the length of the code at compile time.
  • Overloading of functions : Multiple functions are defined with the same function name, depending on the number of arguments and types of functions.
  • function Template : A general function that invokes a function to replace a virtual type in a template based on the type of the argument.
  • functions with default parameters : Default parameters must be placed at the right end of the table column of the formal parameter, and a function cannot be used as a function overload or as a default parameter, otherwise it will produce two semantics.
  • recursive invocation of a function : recursive formula + condition of the end of recursion (boundary condition).
  • variable scope, life cycle : (1) by scope can be divided into local variables (dynamic local variables, static local variables and function parameters) and global variables (static global variables, external variables), (2) According to the life time can be divided into dynamic storage (dynamic local variables, function parameters, etc.) and static storage (static local variables, static external variables, external variables, etc.)
  • intrinsic functions (static) and external functions (extern, can be omitted): Intrinsic functions can only be called by this function, external functions can be called by other files, and now commonly used methods containing header files.
  • There are two forms of the include Command : (1) ' #include < file name > ' #include <iostream> ' contains header files provided by C + + system, (2) ' #include filename ' ' For example ' #include ' c:\tan\file.c "' requires the compilation system to look for a header file in the specified path, such as ' #include ' file.c ' in the current path of the user program to find the file.
  • String handler function : (1) String connection function strcat, function prototype is ' strcat (char[],const char[]) ', (2) string copy function strcpy, function prototype is ' strcpy (char[], const Char[]) '; (3) string comparison function strcmp, function prototype is ' strcmp (const char[], const char[]) '; (4) String length function strlen, function prototype is ' strlen (const char[]) '. * * Note: You can initialize character arrays with string constants, but you cannot assign values to character arrays with string constants; You can initialize and assign string variables with string constants.
  • Pointers as Function parameters: (1) The correct example
void swap (intint *p2) {     int  temp;    Temp=*p1;     *p1=*p2;     *p2=temp;}

(2) Examples of errors

void swap (intint *p2) {    int *temp;     *temp=*p1;     *p1=*p2;     *p2=*temp;}

Analysis : Example two defines the pointer variable temp, which does not assign a value to the pointer variable (pointing to the object), but assigning a value to the *temp is dangerous and can disrupt the system's normal operation.

    • reference An array element : (1): Subscript ' A[i ' (2): Pointer method ' * (A+i) or * (P+i) ' Where A is an array name and P is a pointer to an element of an array.
    • pointers vs. self-adding :

(1) *p++ equivalent to * (p++) First to obtain the value of P-point variable *p, and then the P-pointer plus 1;
(2) * (++P) First to the P pointer value plus 1, and then take the value of P point variable;
(3) (*P) + + indicates the value of the variable that p points to plus 1.

    • pointer arrays and array pointers:

(1) pointer array: int *P[N] array consists of n pointer elements pointing to integer data;
(2) array pointer: int (*P) [n] p is a pointer variable that points to n integer elements.

    • reference : When declaring a reference-type variable, it must be initialized at the same time, declaring that it represents that variable, referencing it as the function's argument, and extending the function to pass the data, which passes the address of the argument to the parameter instead of the value of the argument. Pointer variables, as parameters, need to open up memory space, whereas reference variables are not independent variables, not exclusive memory space.
    • Shared Body (Union): The purpose of using a common body is to want to store several different types of data in the same memory segment, where the length of memory occupied by the shared body variable equals the length of the longest member, and the shared body variable can have only one member at a time in the access member, and no other members will work.
    • Enum type (enum): The format is enum Enum type name {enumeration constant list}; enumeration elements are constants, so they cannot be assigned a value.
The third object-based programming
    • How member functions are stored : Each object occupies only the space occupied by the non-static data portion of the object, not the space occupied by the function code and the static member variable, and different objects call the member function through the this pointer.
    • function Initialization list : For example ' Box::box (int n, int w, int len): Height (h), Width (w), Length (len) {} '.
    • Protection of Shared data (const):

(1) Constant object: The data member of the constant object is const type, the member function is non-const type, the external cannot call the Non-const member function of the object. `
(2) Constant member data: initialization of a constant member function can be done only through the constructor's parameter initialization list.
(3) Constant member function: Only data members of this class can be referenced, but cannot be modified

Data members Non-const member functions Const member functions
| non-const member data Can be referenced, or its value can be changed Can be referenced, but cannot be changed by its value
|const member Data Can be referenced, but cannot be changed by its value Can be referenced, but cannot be changed by its value
Member data for const objects Not allowed Can be referenced, but cannot be changed by its value

    • const with pointer (left value, right direction):

(1) const type name * Pointer variable name: pointer variable pointing to a constant object (left fixed value)
(2) class name * Const pointer variable name: constant Pointer to object (right direction)

    • to implement data sharing (static):

(1) static data members: static data members in memory only a portion of the space, it belongs to the class, not the object, the object does not match the memory space is not included in the space occupied by static members; Static member initialization can only be done outside the class body; a static data member can be referenced either by an object name or by a class name.
(2) static member functions: Static member functions are primarily used to access static data members, but not non-static members (no this pointer); Static member functions can be referenced by object names, and nights can be referenced by class names.

Key Records of C + + programming

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.