Turn from: 18 Questions to be aware of to learn C + +
One, # include "Filename.h" and #i nclude <filename.h> difference# include "filename.h" means the compiler will look for this file from the current working directory # # <filename.h> means the compiler will start looking for this file from the standard library directorysecond, the role of the head fileEnhanced security detection It is possible to easily invoke library functionality through a header file without having to care about how it is implementedThree, *, & modifier positionFor the * and & modifiers, it is best to close the modifier to the variable name in order to avoid misunderstandingsIv. If statementDo not compare Boolean variables with any values, which can be very error-prone. The shaping variable must have the same type of value to compare the floating-point variable with the best less than the point, even if it has to have a value to limit the pointer variable to be compared to NULL, not compared to Boolean and shapingv. Comparison of Const and # defineConst has a data type, #define没有数据类型个别编译器中const可以进行调试, #define不可以进行调试在类中定义常量有两种方式1, declare a constant in a class, but do not assign a value in the constructor initialization table; 2. Use enumerations instead of const constants.Vi. How values are passed in C + + functionsThere are three ways: value passing (pass by value), pointer passing (pass by pointer), reference passing (pass by reference) void Fun (char c)//pass by valuevoid Fun (char *str) Pass by Pointervoid Fun (char &str)//pass by reference if the input parameters are passed by value, it is best to use reference passing instead, because reference passing eliminates the need for temporary object construction and the type of the destructor to be omitted. If not, add a void.the pointer or reference constant in the function body cannot be returnedChar *func (void) {char str[]= "Hello Word";//This is not returned, because STR is a specified variable, not a normal value, the function will be written off after the return str;} Pointer variables in a function body do not automatically release as the function diesimplementation of 八、一个 memory copy functionvoid *memcpy (void *pvto,const void *pvfrom,size_t size) {assert (Pvto!=null) && (Pvfrom!=null)), Byte *pbto= ( byte*) Pvto; Prevents the address from being changed by byte *pbfrom= (byte*) pvfrom;while (size-->0) pbto++ = Pbform++;return pvto;}Nine, the memory allocation methodThere are three ways to allocate, remember, maybe someone will ask you that question when you go to the interview. 1, static storage, is at the time of the program compile has been allocated well, throughout the 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.10, memory allocation considerationsWhen 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 to not modify pointer data to a constant11. Content Replication and comparisonArray ... 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 sizeofRemember 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;cout << sizeof (a) << Endl; 12 bytes cout << sizeof (p) <<endl; 4 bytes and, in the function, the array parameter is degraded to a pointer, so the following content is always output to 4void fun (char a[1000]) {cout<<sizeof (a) <<endl;//Output 4 instead of 1000}13. About Pointers1. The pointer must be initialized at the time of Creation 2, the pointer must be set to NULL3 after free or delete, the pointer length is 4 byte 4, when the memory is freed, if it is an array pointer, you must release all memory, such as Char *p=new char[100];strcpy (P, " Hello World ");d elete []p; Note that the previous [] number p=null;5, the contents of the array pointer cannot exceed the maximum length of the array pointer. such as: Char *p=new char[5];strcpy (P, "Hello World"); Error target length is not large enough delete []p; Note the previous [] number p=null;14, about Malloc/free and New/deleteL Malloc/free is the memory new/delete of C, which is the memory allocation of C + +. Note: Malloc/free is a library function, New/delete is operator 2 Malloc/free cannot execute constructors and destructors, and New/delete can be 3 New/delete cannot run on C, so Malloc/free cannot be eliminated 4 Both must be paired. Using the _set_new_hander function in 5 C + + to define the handling of memory allocation exceptions15. Features of C + +C + + is newly added with overloads (overload), inline (inline), const,virtual four mechanism overloads and inline: can be used for global functions, also for class member functions, const and virtual: Only for class member functions; overloading: In the same class, function with the same name as the function. The function is called by different parameters. The member function of a subclass can be unavailable to 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. overriding refers to a derived class function that overrides a base class function with the same name; the same argument; the base class function must have the virtual keyword; different scopes (derived classes and base classes). Hiding means that a derived class masks a function of the same name as the base class 1, the function name is the same, but the parameters are different, at which point the base class function is hidden regardless of whether the base class has the virtual keyword. 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. If the data type is internal, it is not necessary to do this, for example: void Func (a a) is changed to void func (const a &a). void func (int a) does not have to be changed to void func (const int &a), 3, a function with a return value of pointer type is const, the function return value can not be modified, the assigned variable can only be const type variable. such as: 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 destructorsThe constructor of the derived class should call the constructor of the base class in the initialization table, and the destructor for the derived class and base class should be added to the virtual keyword. Do not underestimate the constructors and destructors, in fact, it is not easy to make up. # include <iostream.h>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;//Upcastdelete PB;} The output is: ~derived~base If the destructor is not virtual, the output result is ~base18, #IFNDEF/#DEFINE/#ENDIF有什么作用Copying the header file is repeatedly referenced
Learn the concepts that C + + must masterfirst, the concept of pointersChar str[] = "ABCDEFG"; char *pc = str; The PC is a pointer to string str short x = 33; Short *px = &x; PX is a pointer to a short x cout <<*pc <<endl; This statement will print the character ' A ' pc + = 4; The pointer moves right 4 points to the 5th character cout <<*pc <<endl; This statement will then print the character ' E ' pc--; Move the pointer to the left cout <<*pc <<endl; This statement will then print the character ' D ' cout <<*px + 3 <<endl; This statement prints 36 because =33+3 in the C program, suppose we have defined the following variables and functions: int k, TEM, *P1, *P2, a[5], F (), *p3 (); What are the grammatical errors in the following set-up narrative (assignment statements)? And please explain its cause 1. P1 = &K;2.P2 = A;3.P3 = F;4.P1 = &A[3];5.P1 = P2; Answer: (1) P1 = &k; P1 is a pointer variable, so P1 the table address, and K represents a general variable, &k means to remove the address of k, so it is correct. (2) P2 = A; A is the name of the array, which can represent the starting address of the array in memory, and P2 is the pointer variable, so it is correct. (3) P3 = f; F represents the name of the function, which represents the call function F, and therefore contains the return value, and P3 is the pointer variable, so there is an error in the formula. (4) P1 = &a[3]1 table pointer variable, representing the address, and &a[3] table takes the index (index) to 3 of the array element address, it is correct. (5) P1 = P2; P1,P2 is the pointer variable represents the address, this description refers to the P2 address is assigned to P1, it is correct. second, the concept of structureA struct is a type whose member defaults to public.struct Student//defines a structure Student the data used to hold the student {int id;//number char name[30];//name}student s = {555, "Davis, S Amuel "}; Initializes an instance of student Scout <<s.id "" <<s.name <<endl; This one will print "555 Davis,samuel"Iii. The concept of inheritance of classesClass Base{private:int A;protected:int b;public:int C;}; Class Sub1:public base {...}; Class Sub2:private base{...}; Explains the data members that can be used in base,sub1,sub2. and point to the access mode (private, protected, or public) of the data member. Answer:class data members Access modebase a privateb protectedc publicsub1b protectedc publicsub2b Privatec PrivateIv. virtual functions and abstract classesOne of the core concepts of polymorphic (polymorphism) object-oriented design is polymorphism--it enables a group of similar behaviors to be called by the same name, but each object can construct the implementation details of this same name action in a way that suits its needs, and the key to C + + polymorphism is the function of the so-called virtual function. Virtual functions, which are derived classes can redefine the member functions of a base class, if you want to create a virtual function in a C + + program (and then do polymorphism), simply declare the function with the virtual keyword (see below) virtual void Display (); The use of virtual functions for objects that share the same base class can have a more consistent usage attitude, for example, you might define a base class named shape with a draw virtual member function, and then derive the Circle class and the square class from it. And they each have their own draw member function. Each object derived from these class derivations can call the draw member function; However, the compiler ensures that each version of the draw function should be called. is a base class or a derived class. An example of an important idea: pointers to the parent class can also be used to point to subcategories # include iostream.h>class baseclass{public:virtual void Display () {cout "\ n";}}; Class derivedclass:public baseclass{public:virtual void Display () {cout "\ n";}}; void Print (baseclass* PBC) {Pbc->display ();} int main () {baseclass* PBC = new baseclass;derivedclass* PDC = new DerivedClass; Print (PBC);//Display 100Print (PDC);//display 200return 0;} V-table (virtual function table) when a C + + program calls a non-virtual function, it uses the same static binding as the C program call function to call the function. However, if a C + + program calls a virtual function by pointing to a pointer to a class, the compiler calls the function using the so-called late binding (late binding) or static binding (static bind) technique. virtual function table for C + + virtual function table), or V-table to implement dynamicbinding, the so-called V-table is an array of function pointers, which the compiler builds for each class that uses virtual functions. A pure virtual function, a member function that can not only be redefined, but must be redefined, is called a pure virtual function, and you can convert a virtual member function to a pure virtual member function by simply specifying a function with a value of 0 (a more efficient argument is a null pointer). virtual void Printdata () = 0, as shown below: Abstract class When a class contains at least one pure virtual function, this class is called an abstract class, and you cannot derive an object from this class . v. C + + template classesGeneral Declaration and use of class Collection{...int a[10];} Collection object, template declaration and use templates <class t>//Note here Class collection{... T A[10]; }//generic Declarationcollection <int> object; Note here collection <char> object; Watch this.
< templates >
Used to enclose the type parameter table in a class template or function template definition. Used to enclose the argument type when instantiating a class template. For example:
Template<class t>//Surround type parameter table
Class c{
T x;
...
};
C<int> OC; enclosing argument types
18 Questions to be aware of to learn C + +