C ++ programming skills

xiaoxiao2021-03-06  70

C programming skill C language is an object-oriented language, the code written in C is more simple, efficient, more maintainable and reusability. But many people use C language but feel that C and C programming have no difference. This is actually due to the characteristics and characteristics of the C language, and the use is not caused. In fact, no programmer uses C language to exceed C language. First, use New and Delete to dynamically Memory allocation and release operators New and Delete are C new over-account, providing storage dynamic allocation and release. Its role is equivalent to the function malloc () and free () of the C language, but performance is more superior. Using New is the following advantages to using malloc (): (1), New Automatic calculation To assign the size of the type, do not use the SizeOF operator, more expensive, can avoid errors. (2, it automatically returns the correct pointer type, no need for mandatory pointer type conversion. (3), you can initialize the allocated object. Used: (1), int * p; p = new int rt [10 ]; // Assign a shaped array delete [] p; // delete this array (2), int * p; // dynamically allocate an integer and initialize the second, use Inline Inline Function Alternative Macro Cancer For frequently used functions, C language recommendations use macro call instead of function call to speed up code execution, reduce call overhead. But macro has a lot of drawbacks, may cause undesired side effects. For example Hong: # Define ABS (A) ((a) <0? (- a): When using ABS (i ), this macro will be wrong. So in C , INLINE internal functions should be used instead of macro call, This will reach the function of macro, and avoid the disadvantages of macro. Use the internal function to place the inline keyword in front of the function return type. For example: Inline Int Add (int A, int b); // Declare add () is the compiler that the compiler is encountered when encountered the add () function, but directly embed a function code to speed up the execution of the program. 3. Using the function overloaded in the C language, two The name of a function cannot be the same, otherwise it will cause compilation errors. In C , the two functions of the function name and the parameters are interpreted as overload.

For example: void Puthz (CHAR * STR); // Output Chinese character void Putz (int X, int y, char * str) in the current location; // Output Chinese characters in X, Y can help programmers can pay more Multi-complexity, avoid using complicated function names such as INTABS (), FABS (), DABS (); in large programs, make function names easy to manage and use, without having to race the brain juice to deal with function name . 4. Use the reference (Reference) instead of the pointer to pass in the C language, if a function needs to modify the variable value used as a parameter, the parameters should be declared as the pointer type. For example: Void add (int * a) {(* a) ;} But for complex programs, the use pointer is easy to make mistakes, and the program is also difficult to read. In C , reference to the above case can be used instead of the pointer, making the program more clearly understood. The reference is an alias for variables, and the reference is operated, which is equivalent to operating the original variable. For example, using the referenced function is defined as: Void Add (INT & A); // a reference to an integer is the same as the function of the previous function of the pointer, however the code is more concise Clear and easy to understand. V. Use the default parameter in C can use the default parameters, for example: void Putzxy (char * STR, INT x = -1, int y = -1) {IF (x == - 1) x = wherex ( ); if (y == - 1) y = wherey (); MoveTo (x, y) Puthz (STR);} can have two ways to call functions Puthzxy (), for example: Puthzxy ("C language"); / / Use the default parameters, output Puthzxy ("C Language", 10, 10) in the current location; // No use of default parameters, one function should have greater flexibility as much as possible, using the default The parameters provide a valid method for programmers to handle greater complexity and flexibility. Therefore, the default parameters are used in large quantities in C code. It should be noted that all default parameters must appear on the right of the non-default parameter. That is, once the parameters of the default value are started, the non-default parameters are not described. For example: void Puthzxy (CHAR * STR, INT X = -1, int y = -1); // correct void Putzxy (int x = -1, int y = -1, char * str); // error six, The use of "class" to seal the C language is a modular program language, and a certain data package function is implemented by a single compilation of the function of the function. However, C is better than C by a lot of data packages, inheritance, etc. by using the powerful feature of "class". By using the "class", a well-defined interface is established to make the programmer when using a class when using a class, without having to care about its implementation. Since the function can also achieve the encapsulation of the data to a certain extent, when the function is written when writing C programs, when the class is used, it is difficult to grasp the initiator of C .

转载请注明原文地址:https://www.9cbs.com/read-121468.html

New Post(0)