The most common questions in C++ interviews (2)

1. What is the difference between overloading, rewriting and redefinition?

Overloading: It means that in the same scope, the function names of the two functions can be the same, but the parameters cannot be completely the same. They can be of different parameter types, number of parameters or order of parameters. The return values ​​can be the same or different, virtual Keywords are also optional. Decide which function to call based on the parameter list.

Overriding (also called overriding): refers to the fact that in the inheritance relationship (subclass and parent class), the subclass redefines the same virtual function in the parent class except for the function body. But it should be noted that the rewritten function cannot be static, it must be a virtual function, and the other must be exactly the same. The access modifier of the overridden function can be different. Although it is private in virtual, it
can be changed to public in derived classes.

Redefinition: The derived class redefines the non-virtual function with the same name in the parent class. The parameter list
and return type can be different. Hidden (redefined) by the function of the same name.
That is, in the inheritance relationship, the subclass implements a function with the same name as the parent class, (only focus on the function name, and has nothing to do with the parameters and return values). In this way, the function of the subclass hides the function of the same name of the parent class.

Two, the four memory areas in c++

Before running the program:
code area: store the binary code of the function body, managed by the operating system (notes are not counted). The code area is shared, and the purpose of sharing is to only need another copy of code in memory for frequently executed programs. The code area is read-only, and the reason for making it read-only is to prevent the program from accidentally modifying its instructions.
Global area: store global variables and static variables and constants.

After the program runs:
Stack area: automatically allocated and released by the compiler, storing function parameter values, local variables, etc.
Heap area: It is allocated and released by the programmer (new/delete). If the programmer does not release it, it will be reclaimed by the operating system at the end of the program.

The significance of the four areas of memory:
the data stored in different areas are endowed with different life cycles, giving us greater flexibility in programming.

3. Can a data member be both const and static? If not, please explain why.

Answer: A data member can be both const and static, expressed as a static constant.
Reasons:
(1) Static members are generally initialized outside the class.
(2) Constants are generally initialized after the constructor.
(3) Static constants are initialized outside the class, but must be declared as const while being initialized outside the class.

Fourth, the role and characteristics of the constructor?

Function:
Used to initialize the object when creating the object.

Features:
(1) The constructor is a member function, and the function can be written inside the class or outside the class.
(2) The constructor is a special function whose name is the same as the class name, does not specify a type description, and has no return value.
(3) The constructor can be overloaded.
(4) The constructor cannot be called directly in the program, and the system automatically calls the constructor when the object is created.
(5) It is not used to initialize static data members, because static data members do not belong to the class.
(6) If no constructor is given in the class, the compiler automatically generates a default constructor (no parameters, function body is empty), When a constructor is given manually in a class, the default constructor disappears.

Precautions for use:
When there are built-in types or composite types (such as arrays and pointers) or classes that manage dynamic memory in the class, the constructor must be manually defined, and the default constructor cannot be used. Otherwise, objects of user-created classes get undefined values.

Five, the role and characteristics of the destructor?

Function:
Used to perform operations such as pointer release and memory release on the object.

Features:
(1) The destructor is also a special class member function, which has no return type, no parameters, and no overload.
(2) Only one destructor can be defined in a class, and the destructor cannot be overloaded.
(3) The destructor must exist in public.
(4) The destructor can be called or called by the system. In the following two cases, the destructor will be called automatically. One is that if an object is defined in a function body, when the function ends, the destructor of the object is automatically called; the other is that when an object is passively created using the new operator, it is released using the delete operator , delete will automatically call the destructor.
(6) If no destructor is given in the class, the compiler automatically generates a default destructor (no parameters, function body is empty). The default disappears when a destructor is given manually.

Precautions for use:
When there are dynamic memory variables in the class, the default destructor cannot be used, and the destructor must be manually designed.

Six, the role of the const keyword?

(1) Define constants: the variable value of type modified by const is immutable.
(2) Modified pointer: You can specify that the pointer itself is const, and become a pointer constant. The pointing of the pointer cannot be changed, and the value stored in the pointer can be changed; The pointing can be changed, but the value of the pointer cannot be changed; or both are specified as const at the same time, neither the pointing nor the value of the pointer can be modified.
(3) The member variables of the const modified class represent member constants and cannot be modified, and they can only be assigned in the initialization list.
(4) If const modifies a member function of a class, then the member function cannot modify any non-const member function in the class. Generally written at the end of the function to modify.
(5) In a function declaration, const can modify the formal parameter, indicating that it is an input parameter, and its value cannot be changed inside the function.

Seven, the role of the static keyword?

(1) Hidden. When we compile multiple files at the same time, all global variables and functions not prefixed with static have global visibility. Using this feature, you can define functions and variables with the same name in different files without worrying about naming conflicts.
(2) Keep the variable content persistent. The variables stored in the static data area will be initialized when the program starts running, and it is the only initialization. There are two kinds of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible range of variables.
(3) The third role of static is to initialize it to 0 by default.
(4) The static member variables in the class belong to the entire class, and there is only one copy of the object of the class.
(5) The static member function in the class belongs to the entire class. This function does not receive the this pointer, so it can only access the static member variable.

The difference between static global variables and ordinary global variables The
 difference between the two is that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are in each source file. are all valid. The static global variable limits its scope, that is, it is only valid in the source file where the variable is defined, and cannot be used in other source files of the same source program. Because the scope of the static global variable is limited to one source file, it can only be shared by the functions in the source file, so it can avoid causing errors in other source files.
 
The difference between static local variables and ordinary local variables
After changing a local variable to a static variable, its storage method is changed, that is, its lifetime is changed. Changing a global variable to a static variable changes its scope and limits its scope of use. Static local variables are only initialized once, and the next time is based on the last result value.

Differences between static functions and ordinary functions
   The scope of static functions and ordinary functions is different, only in this document. Functions used only in the current source file should be described as internal functions (static modified functions), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be stated in a header file, and the source file that uses these functions must include this header file.

8. Can a constructor be a virtual function? Can the destructor be a virtual function?

For the constructor:
(1) When we define a virtual function in the class, there will be a virtual function table vtable, which is stored in the memory space of the object, and which function to call is confirmed through the virtual pointer vptr. If the constructor is virtual, it needs to be called through the vtable, but at this time the object has not been instantiated, that is, there is no memory space, and the vtable cannot be found.
(2) The virtual function is mainly used to make the overloaded function get the corresponding call when the information is incomplete. The constructor itself is to initialize the instance, so there is no practical significance in using virtual functions.
(3) The real type of the object cannot be determined when the constructor is called (because the subclass will call the constructor of the parent class), and the function of the constructor is to provide initialization, which is only executed once during the life of the object, not the dynamic behavior of the object , there is no need to be a virtual function;
in summary: the constructor cannot be a virtual function, because when creating an object, we always have to explicitly specify the type of the object.

For the destructor:
the destructor can be a virtual function, and when using the parent class pointer or reference to call the subclass, it is best to declare the parent class's destructor as a virtual function, otherwise there may be a memory leak problem .

9. Which functions cannot be defined as virtual functions

1. A friend function, which is not a member function of a class.
2. Global functions.
3. Static member function, it does not have this pointer.
4. Constructors, copy constructors, and assignment operator overloading (can be used but generally not recommended as virtual functions).

10. What is the content stored in the virtual function table? Where is the virtual table pointer stored?

1. The content stored in the virtual function table: the address of the virtual function of the class.
2. The time when the virtual function table is established: the compilation stage, that is, the address of the virtual function will be placed in the virtual function table during the compilation process of the program.
3. The storage position of the virtual table pointer: the virtual table pointer is stored at the frontmost position in the memory space of the object, which is to ensure that the offset of the virtual function is correctly obtained.

Guess you like

Origin blog.csdn.net/qq_46901210/article/details/124190346