After learning C ++ for a long time, I found that I still had no in-depth understanding of C/C ++, So I bit my teeth and started to understand C ++ programming ideas ..
I used to learn C ++ directly. I didn't know C very well, but I learned C ++ in VC, until I saw the idea of C ++ programming, I found that I should have learned it again from the beginning...
1. Differences between defined declarations:
The Declaration declares that the existence of the identifier does not allocate space for it, and the definition is to allocate a certain storage space for it.
Int * p; // This is a pointer declaration and definition
Int p; // This is the declaration and definition of the integer variable
Exern void show () ;== void show ();
For a function, as long as there is no function body, extern is declared to indicate that the function is defined externally.
We write down
Int I;
Int I; // there will be an error in the definition of the punch. the compiler will understand it as "What kind of I should I use? "
Extern int I;
Int I; // then there will be no error. The reason is that the first I is a declaration of the variable and the bucket is not allocated for the second time.
2. memcpy function void * memcpy (void * dest, const void * src, size_tcount );
This function is often used in C. In fact, it is similar to strcpy that it copies n memory blocks to the destination address.
Note that the parameter here is void * because we operate on memory blocks, it can be any data type.
There should be no type difference in programmer's understanding of memory. However, when we use the memory, the binary code is digitalized.
3. malloc function and calloc function assert Function
Void * malloc (size_t size );
Void * calloc (size_t num, size_tsize );
Void assert (int expression); // If the parameter is FALSE, force the program to stop running, that is, exit without 0.
Both functions allocate the specified void * memory region in the heap. Only calloc can be initialized.
The malloc function is not initialized. When the malloc function is used for the first time, the data of each unit may be 0.
However, if the allocated memory area is used, any number may be used.
Here we will ask what the free function has done?
The free function only tells the compiler that the memory area allocated with malloc can be reused. To a certain extent, it can be understood as releasing
Now let's think about the FILE struct used by our fopen and fclose functions.
4. Standard Database issuance form
We use string. h and other header files in VC to use the defined functions. We can find String. h, but we cannot find its implementation file CPP (that is, the source file)
In the previous C library compilation, we usually put a set of data types and functions with unified functions and classification in a header file, and then implement them in the original file. However, the implementation of these corresponding files is not found below the VC provided by Microsoft. This is because for Microsoft, we use VC users. Although we are software developers, we use it on Microsoft's platform.
Microsoft provided me with an excuse that the header file + dynamic link library form is not the header file + source file form.
For Microsoft, the implementation part of our database will not be provided. We only need to provide dynamic link libraries for us to use the functions and types they define.
5. Repeated Definitions
# Ifndef test
# Define final
# Endif
In a large project, we may use multi-head file setup. we include a header file in the Multi-head file, so when the compiler compiles the link
The problem of repeated data type or function definition occurs. To solve this problem, we need to use pre-compiled commands.
If the header file of lib. h contains int I, we should define it in lib. cpp to prevent repeated definitions.
# Ifndef lib_H _
Int;
# Endif;
This prevents duplicate definitions of unified data.
6. C ++ Encapsulation
The encapsulation of C ++ is ambiguous?
Some people use encapsulation to hide the interpretation, and I think it is appropriate to use this abstract data type to interpret the class,
In the previous C struct, we combined a set of related attributes into a struct, so this struct is only used as Data encapsulation and has no other practical significance.
But it is different in C ++.
Classes can be added to functions, and other operational behaviors make object-oriented ideas well implemented. A class represents a class of things, and an instance of a class represents an object, this object has its own attributes and behaviors.
We can encapsulate the operation functions in the class to avoid repeated function names in C.
6. simple and practical Union enum Enumeration
For the Union, its size is the largest storage of data type bytes among the members. We can use one of the data types to save storage space.
Enum actually defines some data members starting from 0. Of course, we can assign values to enumeration members as follows:
# Include <iostream>
Using namespace std;
Enum Day
{
One = 433,
Two,
Three
};
Union Me
{
Int;
Char buf [100];
};
Void main ()
{
Day a = one;
Day B = two;
Cout <a <endl;
Cout <B <endl;
Me me1, me2;
Me1.a = 100;
Strcpy (me2.buf, "dsfds ");
Cout <"number in Union:" <me1.a <endl;
Cout <"string in Union:" <me2.buf <endl;
Cout <"the Union size is:" <sizeof (Me) <endl;
}
From the column yue7603835