C + +is a language, just its grammar, characteristics, standard class library is already a very advanced curriculum, so in the beginning of learning, must first lay a good foundation. Know what to focus on when we're learning about it.
One, #include "filename.h" and the difference between # include
#include "filename.h" means that the compiler will find this file starting from the current working directory
#include means that the compiler will start looking for this file from the standard library directory
Second, the role of the head file
Enhanced safety testing
It is possible to easily invoke library functionality through a header file without having to care about how it is implemented
Three, *, & modifier position
For the * and & modifiers, it is best to close the modifier to the variable name in order to avoid misunderstandings
Iv. If statement
Do not compare Boolean variables with any values, which can be very error-prone.
Shaping variables must be compared with values of the same type
Floating-point variables are best less than points, even if they have a value to limit.
Pointer variables are compared to NULL, not to Boolean and reshape
V. Comparison of Const and # define
Const has a data type, #define没有数据类型
The const in the individual compiler can be debugged, #define不可以进行调试
There are two ways of defining constants in a class
1, in the class in the declaration constant, but does not assign the value, in the constructor initialization table to assign the value;
2. Use enumerations instead of const constants.
Vi. how values are passed in C + + functions
There are three ways: value passing (pass by value), pointer passing (pass by pointer), reference passing (pass by reference)
- void Fun (char c)//pass by value
- void Fun (char *str)//pass by pointer
- void Fun (char &str)//pass by reference
If input parameters are passed as values, it is best to use reference passing instead, because reference passing eliminates the construction and destruction of temporary objects
The type of the function can not be omitted, even if there is no need to add a void
The pointer or reference constant in the function body cannot be returned
- Char *func (void)
- {
- Char str[]= "Hello Word";
- This cannot be returned because STR is a specified variable, not a normal value, and will be written off after the function is finished.
- return str;
- }
Pointer variables in a function body do not automatically release as the function dies
Implementation of 八、一个 memory copy function
- void *memcpy (void *pvto,const void *pvfrom,size_t size)
- {
- ASSERT ((pvto!=null) && (pvfrom!=null));
- Byte *pbto= (byte*) pvto; Prevent the address from being changed
- Byte *pbfrom= (byte*) Pvfrom;
- while (size-->0)
- pbto++ = pbform++;
- return pvto;
- }
Nine, the memory allocation method
There are three ways to assign, remember, maybe someone will ask you that question when you go to the interview that day.
1, static storage, is at the time of the program compile has been allocated well, during the entire run, such as global variables, constants.
2, the allocation of the stack, the local variables within the function is allocated from this, but the allocation of memory is easy to be limited.
3, heap allocation, also known as dynamic allocation, such as we use New,malloc to allocate memory, with Delete,free to release memory.
Ten, memory allocation considerations
When allocating memory with new or malloc, you must assign an initial value to this pointer.
After releasing memory with delete or free, you must point the pointer to null
Pointer data to a constant cannot be modified
XI. content Replication and comparison
- Array......
- Char a[]= "Hello word!";
- Char b[10];
- strcpy (B,a);
- if (strcmp (A, b) ==0)
- {}
- Pointer......
- Char a[]= "Hello word!";
- Char *p;
- P=new Char[strlen (a) +1];
- strcpy (P,a);
- if (strcmp (p,a) ==0)
- {}
12, the problem of sizeof
Remember that C + + cannot know the size of the object The pointer refers to, and the size of the pointer is always 4 bytes
- Char a[]= "Hello world!"
- Char *p=a;
- count<
- count<
Also, in the function, the array parameter is degraded to a pointer, so the following content is always output as 4
- void Fun (char a[1000])
- {
- count<
- }
13. About Pointers
1. The pointer must be initialized when it is created
2. The pointer must be null after free or delete
3, the length of the pointer is 4 bytes
4, when releasing memory, if it is an array pointer, you must release all the memory, such as
- Char *p=new char[100];
- strcpy (P, "Hello World");
- delete []p; Note the previous [] number
- P=null;
5. The contents of an array pointer cannot exceed the maximum ease of array pointers.
Such as:
- Char *p=new char[5];
- strcpy (P, "Hello World"); Error target not big enough
- delete []p; Note the previous [] number
- P=null;
14, about Malloc/free and New/delete
The Malloc/free is a c/c+ memory-allocation character, and New/delete is a C + + memory specifier.
Note: Malloc/free is a library function, New/delete is an operator
Malloc/free cannot execute constructors and destructors, and New/delete can
New/delete can't run on C, so Malloc/free can't be eliminated.
Both must be used in pairs
You can use the _set_new_hander function to define the handling of memory allocation exceptions in C + +
XV, C + + features
C + + is newly added with overloads (overload), inline (inline), const,virtual four mechanism overloads and inline: can be used for global functions, or for class member functions;
Const and virtual: can only be used for class member functions;
Overloading: A function with the same name as the function in the same class. The function is called by different parameters. The function may not have the virtual keyword. Functions with the same name as global functions are not called overloads. If you call a global function with the same name in a class, you must use the global reference symbol:: Reference.
Overrides refer to a derived class function overriding a base class function
Function name is the same;
the same parameters;
The base class function must have the virtual keyword;
Different scopes (derived classes and base classes).
Hidden refers to the same function with the same name as a derived class that masks the base class
1, the function name is the same, but the parameters are different, at this time regardless of the base class has the virtual keyword, the base class function will be hidden.
2, the function name is the same, the parameters are the same, but the base class no virtual keyword (there is overwrite), the base class function will be hidden.
Inline: The inline keyword must be placed with the definition body, not simply in the declaration.
Const:const is the abbreviation of constant, the meaning of "constant unchanged". The const-modified items are protected against accidental changes and can improve the robustness of the program.
1, parameters for input with the pointer-type parameters, plus the const can prevent accidental changes.
2. When making input parameters by the user type referenced by value, it is better to pass the value to the reference and add the Const keyword, which is to improve the efficiency. There is no need to do this if the data type is an internal type:
Change void func (a A) to void func (const a &a).
void func (int a) does not need to be changed to void func (const int &a);
3, the return value of the pointer type of the function plus const, will make the function return value can not be modified, the assigned variable can only be const type variable. such as: the function const char*getstring (void); char *str=getstring () will be faulted. and the const char *str=getstring () will be correct.
4, the const member function refers to the function body can only call the const member variable, improve the program's key strong. such as declaring the function int getcount (void) const, this function body can only call the const member variable.
Virtual: Virtual functions: Derived classes can be overridden by functions, pure virtual functions: Just an empty function, no function implementation body;
16. What is the role of extern "C"?
Extern "C" is a connection-exchange-specified symbol provided by C + + that tells C + + that this code is a C function. This is because C + + compiled library function name will become very long, inconsistent with C generation, resulting in C + + cannot directly call C function, plus extren "C", C + + can directly invoke C function.
Extern "C" is used primarily for referencing and exporting of regular DLL functions and for C + + or C-header files in C + +. Use the extern "C" keyword in front of you.
17. Constructors and destructors
The constructor of the derived class should call the constructor of the base class in the initialization table;
Destructors for derived and base classes should be added to the virtual keyword.
Do not underestimate the constructors and destructors, in fact, it is not easy to make up.
- #include
- Class Base
- {
- Public
- Virtual ~base () {cout<< "~base" << Endl;}
- };
- Class Derived:public Base
- {
- Public
- Virtual ~derived () {cout<< "~derived" << Endl;}
- };
- void Main (void)
- {
- Base * PB = new Derived; Upcast
- Delete PB;
- }
The output is:
~derived
~base
If the destructor is not virtual, the output is
~base
18, #IFNDEF/#DEFINE/#ENDIF有什么作用
Copying the header file is repeatedly referenced
Through the above analysis, I think we all know something about the things that should be noticed. C + + is a common programming language for static data type checking, which supports multiple programming paradigms. It supports procedural programming, data abstraction, object-oriented programming, making icons, and other generic programming styles. I hope you will have something to gain.
C + + Learning Focus Analysis