1. What is the use of const in C + +?
Don't say a constant when you hear a const, giving the examiner a feeling of talking to a layman. It should be said that the content of the Const modifier can not be changed, the definition of constants is just a way to use, there are const data members, const parameters, const return values, const member functions, and so on, the const modification of things are enforced protection, to prevent accidental changes, Can improve the robustness of the program.
In addition, the const is better than the macro in C, const is a constant with data type, and the macro constant is not, the compiler can be static type security check, the latter is only a character substitution, there is no type security check, and the character substitution may produce unexpected errors (marginal effect).
2. What is the difference between a reference and a pointer in C + +?
A reference is an alias to an object, an action reference is a manipulation of the object, it must be initialized at the same time it is created (referencing a valid object, NOT NULL), the initialization is no longer immutable, the reference has the efficiency of the pointer, and the convenience and intuition of variable use, At the language level, as with the use of objects, references at the binary level are generally implemented by pointers, but the compiler helps us to complete the conversion. References are used to do the right thing with the appropriate tools, and the principle of least privilege is embodied.
3. How to allocate memory in C + +?
1) allocation from a static storage area. Memory is allocated when the program is compiled, and the entire runtime of the program is present, such as global variables, static variables.
2) Create on the stack. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.
3) allocation from the heap (dynamic memory allocation) The program uses malloc or new to request any amount of memory at run time, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by itself and is very flexible to use.
4. What is the difference between New/delete and malloc ()/free ()?
malloc () and free () are standard library functions of the C language, and New/delete are C + + operators that can be used to request and release memory, and malloc () and no are not within the control of the compiler and cannot impose constructors and destructors on them. New will automatically call the constructor, and delete will call the destructor automatically.
5. What is the difference between #include <a.h> and # include "A.h"?
For # include <a.h>, the compiler starts searching from the standard library path A.H for # include "A.h", the compiler starts searching from the user's work path A.H
6, in C + + program calls by the C compiler compiled function, why add extern "C"?
The C + + language supports function overloading, which does not support function overloading. Functions are compiled in C + + with different names in the library than in the C language. Suppose a function is prototyped as: void foo (int x, int y), the function is compiled by the C compiler in the library with the name _foo, and the C + + compiler produces names like _foo_int_int. C + + provides a connection exchange with the specified symbol extern "C" to resolve the name matching problem.
7. What is polymorphism in C + +? How is it implemented?
Polymorphism is the third basic feature of object-oriented programming language following data abstraction and inheritance. It is the polymorphism that occurs at run time, implemented by derived classes and virtual functions. The same function name is used in the base class and derived classes to accomplish different operations to isolate the other type of interface, that is, "W h a T" from "H o W" separated. Polymorphism improves the organization and readability of code, and virtual functions are isolated according to the different types.
8, what is the dynamic characteristics?
In the vast majority of cases, the function of the program is determined at compile time, which we call static characteristics. Conversely, if the function of the program can be determined at runtime, it is called dynamic characteristics. In C + +, virtual functions, abstract base classes, dynamic bindings, and polymorphism form excellent dynamic properties.
9. What is encapsulation? How is it implemented in C + +?
Encapsulation comes from the idea of information hiding, which is to create new data types by combining features and behaviors to isolate interfaces from specific implementations. C + + is implemented through classes, in order to try to avoid the behavior of a module to interfere with other modules in the same system, should let the module only expose the interface must be known to the outside world.
10. What is a copy constructor?
It is a constructor of a single parameter whose argument is a (constant) reference to an object of its genus class, and a reference to an existing object of the same type that is used as a parameter to create a new object from a preexisting object. When passing by value or returning an object, the compiler automatically calls the copy function.
C + + Learning Summary