"21 days proficient in C ++" first week study notes

Source: Internet
Author: User

Day 1: The design of Object-oriented Programs encapsulates data in a secure shell and makes them active, variables can actively operate on themselves rather than passively waiting for program code to operate on them. When you need to print the variable content, you do not need to print them, you just need to tell the variable to print itself.

The variables mentioned above are no different from the objects mentioned in object-oriented theory, except that the C ++ language makes data behave.

In C ++, an object is usually a class variable or structure variable of the User-Defined data type. You can add a function to this data type to make this variable behave.

The next day: There are three methods to define your data types before learning C ++: Structure: struct; enumeration: enum; union: union is generally referred to as the collection data type.

A warning is triggered when a variable is defined with a value that has never been compiled, but the program can pass.

Functions are the main part of the C ++ program. The program is divided into several independent modules, which are called functions. functions can modularize the structure of the program.

All functions must be declared as prototypes, which can be placed in header files. Informs the compiler of the number and type of parameters to be used. The format is as follows:

Function Name (parameter type 1 parameter 1 [, parameter type 2 parameter 2] [,...]);
The first line of a function is the same as the function prototype declaration. The difference is that there is no semicolon at the end.

Function parameter transfer:

One method is to pass values, which is easy to understand. The other method is to pass real parameters, and when the function method is referenced, the parameter address instead of the actual value of the parameter will be passed to the function. The benefit is that the function can return multiple values.
Function prototype prevents potential errors during program output.

The main function does not need prototype description. Because it is regarded as a function that automatically describes the prototype, the main function is the first function to be executed and does not have the problem of being called by other functions.

Day 3: When I/O streams are used, the header file IOSTREAM is required. At that time, the stream operator and format mark must contain IOMANIP.

The real function of a pointer is to use a pointer to pass parameters between two functions and to dynamically allocate memory in the heap.

Void pointer is a full pointer, which can point to any data type. In addition to const and volatile pointers, any type of pointer variables can be assigned to void pointers, it even includes function pointers.

Int I; // defines an integer variable.
Int * ptrl; // defines a pointer to an integer, or an integer pointer that is not strict.
I = 10;
Ptrl = & I; // & is the address operator. In this example, the address of the integer variable I is placed in the ptrl variable.

Note: void type pointers must be forcibly converted when being referenced, and they must be converted when assigned to other types. In turn, a common type pointer can be directly assigned to the void type. The asterisks and types must be enclosed in parentheses during forced type conversion. Iptr = (int *) vptr;

The above discussion is also suitable for passing parameters between two functions using different types of pointers.



Day 4: Reference is an automatic pointer that can be indirectly referenced.

Automatic indirect reference means that a reference value can be automatically obtained without using the indirect reference operator *. Reference another alias that generates a variable. References are used to pass and return parameters between functions by referencing.

Int I = 9; // defines an integer variable I and assigns 9 to it.
Int * iptr = & I; // defines an integer pointer variable iptr and assigns the address of the integer variable to it.
Int & rptr = I; // define a reference rptr and point it to the integer variable I, that is, rptr is an alias of I. All operations on rptr are
File: // rptrThe operation of referenced variable I.

If you want to use the variable I made by iptr, you must use * to indirectly reference the pointer, and use the variable I referenced by rptr to do nothing without using rptr directly.

Use & to define a reference;
Use a reference like a pointer for self-use indirect reference;
A pointer can be referenced to simplify syntax references of multiple pointers;
Initialization is required when defining a reference;
Do not use * to indirectly reference a reference;
A reference is only attached to an alias of the variable it refers to. This attachment remains unchanged within the scope of the reference.

Const int myage = 18;
File: // indicatesPointer to a constant: defines a pointer pointing to a constant (although not necessarily a constant ).
Const int * aptrage = & myage;
File: // alwaysNumber pointer: defines a pointer that cannot be changed, but the value it points to can be changed. It must be initialized when defining a constant pointer like a constant.
Int * const aptrage = & myage;
File: // indicatesConstant pointer to a constant: the first two are combined.
Const int * const aptrage = & myage;

Use const to protect value that should not be changed;
A constant cannot be changed through a pointer;
Constant pointers cannot be changed;
You cannot change a constant pointer pointing to a constant and the constant it points.
Read-only reference (read-only alias): A Reference pointing to a constant;

Int iv = 18;
Const int & rv = ic; // defines a reference pointing to a constant iv. The iv cannot be changed through rv because it is a read-only reference.

Exercise:

# Include
Main (){
Int monthdays [] = {, 30, 31 };
Int * const mpmonth = monthdays;
For (int I = 0; I <12; I ++ ){
File: // cout<(Mpmonth + I) <cout <(I + 1) <"months:" <* (mpmonth + I) <"days" <}
Return 0;
}



Day 5: new and delete are memory allocation operators. new is used to allocate memory, and delete is used to release memory.

Heap is a large unused memory in the computer (excluding the memory occupied by the operating system and the applications being used). Its size is changed at any time, so it is dynamic memory.

When allocating memory with new, you do not need to convert the returned pointer type. Allocating a single variable, such as int or float, is meaningless. When allocating an array, you only need one pointer pointing to multiple elements.

Char * eName = new char [9];
Delete [] eName; // release all memory allocated for eName.

The heap does not initialize itself. It must be initialized with its own data.
Initialization should be performed during memory allocation

Char * eName = new char (a); // This is a character.
Char * eName = new char [9]; // This is an array with nine elements (if it is a string, it also contains NULL0 space ).
Strcpy (eName, "base wood"); // an example of array initialization.

An array of two or more dimensions is called a multi-dimensional array or a matrix (an array of more than one dimension, also known as a table ).
DataType (* matrixName) [numELs]... // matrix variable definition.
DataType // any data type, including user-defined
MatrixName // name of the matrix variable
NumELs // The dimension size after the first dimension.

Example:

Float (* table) [6]; // defines matrix Variables
Table = new float [5] [6]; // allocate 30 floating point memory to the matrix variable.
Float * table [6] = new float [5] [6]; // It can be merged:
Delete [] table; // release all

Exception Handling is a term that refers to the function automatically executed when an error (an exception) occurs. The exception handling function of VC ++:
_ Set_new_handler (): when a new fails, the user can force the exception handler to execute its own error function. _ Set_new_handler () Automatically detects all new operations and interferes with them when necessary.



Day 6: In VC ++, function parameters can be passed by value or by reference.

If the receiving function changes the value of the parameters sent to them, and these changes are identified in the called function, it is considered to be transmitted by address.

If the parameters of the function to be called remain unchanged in the receiving number, they are passed by value.

All arrays are automatically transmitted by address instead of by value. The array name is a pointer. The pointer is always equal to the data address.

Any non-array variable can be passed by reference. You only need to insert an & symbol in the receiving parameter table to indicate a variable for reference transfer.

Reference transmission is highly efficient and secure. You can add a const before receiving the reference parameter to prevent the function from inadvertently changing the reference parameter.

You can declare the default parameter table in the function prototype to simplify programming. A function can declare not only one default parameter, but also multiple parameters as needed. However, the default parameter must be followed by all common parameters in the parameter table.

DefFun (int, flost, int = 12, char = a); // standard function declaration without the parameter variable name. The variable name can be directly copied to the first line of the function, but the first line of the function does not require the default value.

The value passed in when a program starts to run is the command line parameter. When you input one or more command parameters to a program, these parameters are described using two variables. One is an integer variable used to save the number of command parameters, the other is the character pointer array used to save these parameters. Each array element points to a parameter. Example

Main (int argc, * argv [])

Argc and argv are standard names. * agrv [] is a pointer array, and an array name is a constant pointer. Therefore, argv is a pointer to other pointers. While argc saves the actual number of parameters plus 1. The first one is used to save the dos path and file name.

The Seventh Day: functions are similar, but parameters must have different types and function overloading.
Describe the prototype for all overloaded functions. Do not just change the return value of the overload function. Only different parameter tables can distinguish the overload function.

The user gives the function a name, but the VC will extend the name to another name during the call to distinguish the overload function.
If you do not want to extend the function call, you need to insert a list of non-extended functions at the beginning of the program.
Extern "c "{
Void cfun (int I, float x );
Void cfun2 (float x, float y );
}

Overload operator: operator... () //... indicates the selected operator,? : Condition operator,: field operator, * indirect reference operator,. Member operator.

Implements internal operations on Custom Data Types and uses heavy-load operators. You cannot define overload operations for internal data types or change the priority of operators.

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.