C++ Interview Guide - Summary of Common Knowledge Points and Concepts (with C++ Advanced Video Tutorial)

Constructor

  1. The constructor can throw exceptions and can be overloaded. If you add parentheses after the class name when instantiating, you just create an anonymous object. The constructor cannot be a virtual function, because the virtual function table has not been initialized at this time. The new object will mediate the constructor.
  2. When no copy constructor is defined, the IDE will automatically generate a default copy constructor. When calling a function as a class by value, if the actual parameter is an object, the copy constructor will be called. When a function returns an object by value, vs will call the copy construction, but g++ will not, and the default copy is a shallow copy
  3. Deep copy: copy all member variables and memory held by the original object to the new object, and allocate a piece of memory for the new object. The dynamic memory held by the original object and the new object are independent of each other. STL containers all use deep copy
  4. Shallow copy: When dynamically allocated memory, pointers to other data, etc., the default copy constructor cannot copy these resources

 Note: When a class has member variables of pointer type, deep copy is required

static member variable

  1. Memory for static member variables is allocated neither when the class is declared, nor when the object is created, but at initialization (outside the class). Static member variables that are not initialized outside the class cannot be used.
  2. Static member variables do not occupy the memory of the object, but open up memory outside all objects, and can be accessed even if the object is not created. All objects share these static member variables.

static member function

  1. Each object has its own copy of ordinary member variables, but there is only one copy of static member variables, which are shared by all objects. Static member functions do not specifically act on an object. Static members of a class can be accessed even if the object does not exist. Static member functions cannot access non-static member variables, nor can they call non-static member functions.
  2. The this pointer cannot appear inside a static member function

this pointer

  • Actually in C++. When defining a member function of a class, a parameter is hidden. The definition of this parameter is const Name1* this, and the C++ compiler performs operations on different objects according to different this pointers.

destructor

  1. No parameters, no overloading, no exception throwing, inline. When not defined, the system will generate a destructor by default. If the default destructor is used, after the default destructor is executed, the destructor of the member variable will be called, and then the destructor of the derived class will be called, and the amount of code will increase. It will be called automatically, or it can be called manually.
  2. At the time of destructuring, the virtual function table has been initialized, and the destructor can be called in the virtual function table.
  3. When the subclass pointer points to the subclass, both the base class and the subclass will be released during delete. (SubClass* pObj = new SubClass(); delete pObj;)
  4. When the base class pointer points to a subclass, if the destructor is a virtual function (plus the virtual keyword), both the base class and the subclass will be released during delete. If the destructor is not a virtual function delete, only the base class is released, and the subclass is not released, which will cause memory leaks. (BaseClass* pObj = new SubClass(); delete pObj;)

virtual function

  1. The only use of virtual functions is to form polymorphism: the object can be called explicitly at runtime, and the function is called according to the type of the object passed in. For example, when the base class pointer is used to point to the derived class object, the member function of the derived class can be called. If not A virtual function calls a member function of the base class itself.
  2. The constructor cannot be a virtual function: the call of the virtual function is looked up through the virtual function table, and the virtual function is generated after the object is constructed.
  3. Inline functions cannot be virtual functions: inline is statically compiled. The virtual function is called dynamically, and the compiler does not know whether the virtual function of the parent class or the subclass needs to be called, so the inline declaration cannot be expanded.
  4. Static member functions cannot be virtual functions: because there is no this pointer. Virtual functions rely on virtual function function pointers and virtual function tables to handle. The virtual function pointer is created in the constructor of the class and can only be accessed with the this pointer. For static member functions, it does not have a this pointer, so virtual functions cannot be accessed. 
  5.  final: Modifies a virtual function, indicating that the virtual function can no longer be rewritten; modifies a class, and the class cannot be inherited.
  6. override: override is written in the subclass, and it is required to strictly check whether the override is completed. If the override is not completed, an error will be reported.

pure virtual function 

  •     A pure virtual function is an abstract interface with no default implementation, assigned with =0.

polymorphism

  1. Polymorphism: It means that things with the same name can perform different functions. In C++, it can be divided into compile-time polymorphism (function overloading, operator overloading, calling overloaded functions by actual parameters) and runtime polymorphism.
  2. Runtime polymorphism: When the virtual function makes the base class pointer point to the base class object, it uses the members of the base class, and when it points to the derived class object, it uses the members of the derived class.
  3. The purpose of polymorphism: through the base class pointer, "all-round" access to member variables and member functions of all derived classes (including direct and indirect derivation), especially member functions. Without polymorphism, only member variables can be accessed.
  4. Constitute polymorphism: there must be an inheritance relationship, there must be a virtual function with the same name in the inheritance relationship, and they are an overriding relationship (the function prototype is the same), there is a pointer to the base class, and the virtual function is called through the pointer.
  5. Covariance: When a subclass rewrites a virtual function of the parent class, the return value type of the virtual function of the parent class is different.

single inheritance

  1.  Inheritance method: public inheritance is still public, and protected is protected. Protection inheritance public is still protected, and protected is protected. Private inheritance public is still private, and protected is private. All inheritance methods private cannot be used. The private members of the base class can be inherited, and (member variables) will occupy the memory of the derived class object, it is just not visible in the derived class. Class constructors cannot be inherited. (The base class hides the implementation of the base class from the derived class to reflect object-oriented encapsulation.)
  2. Members in the derived class (including member variables and member functions) have the same name as members in the base class, then the members inherited from the base class will be shadowed. So base class member functions and derived class member functions do not constitute overloading.
  3. Derived classes need constructors to complete inherited member variables, but most base classes have member variables with private attributes, which cannot be accessed in derived classes, let alone initialized with derived class constructors. So the constructor of the base class is called in the constructor of the derived class.
  4. When an object is created, the constructor of the base class is called first, and the constructor of the derived class is called second. The derived class constructor can only call the constructor of the direct base class, not the indirect base class. (C->B->A, the construction of A is implicitly called twice, which wastes cpu time, so it is forbidden.)
  5. Destructors cannot be inherited. There is no need to explicitly call the destructor of the base class in the destructor of the derived class, because there is only one destructor for each class.
  6. When creating a derived class object, the execution order of the constructor is the same as the inheritance order, that is, the base class constructor is executed first, and then the derived class constructor is executed. When destroying a derived class object, the execution order of the destructor is opposite to the inheritance order, that is, the derived class destructor is executed first, and then the base class destructor is executed.

multiple inheritance

  1. In multiple inheritance, the constructor of the derived class calls the constructors of multiple base classes. The order in which the base class constructors are called has nothing to do with the order in which they appear in the derived class constructors, but is the same as the order in which the base classes appear when the derived class is declared.
  2. In multiple inheritance, when there are members with the same name in two or more base classes, a naming conflict will occur. The class name and domain resolver:: should be added in front of the member name to explicitly indicate which class member to use , to eliminate ambiguity.

virtual inheritance

  1.  In order to solve the problem of multiple inheritance naming conflicts and redundant data, only one member of the indirect base class is reserved in the derived class. The purpose of virtual inheritance is to let a certain class (virtual base class) make a statement and promise to share its base class kind. No matter how many times the virtual base class appears in the inheritance system, only one member of the virtual base class is included in the derived class.
  2. Virtual derivation only affects classes that are further derived from the derived class that specifies the virtual base class, it does not affect the derived class itself. Because only one member of the virtual base class is reserved in the final derived class of virtual inheritance, this member can be directly accessed without ambiguity. Furthermore, if a member of a virtual base class is overridden by only one derivation path, then the overridden member can still be accessed directly. However, if the member is covered by two or more paths, it cannot be accessed directly. At this time, it must be indicated which class the member belongs to.
  3. The virtual base class is initialized by the final derived class, and the constructor of the final derived class must call the constructor of the virtual base class. For the ultimate derived class, the virtual base class is an indirect base class, not a direct base class. This is different from ordinary inheritance. In ordinary inheritance, only the constructor of the direct base class can be called in the derived class constructor, and the indirect base class cannot be called.

class memory

member function

  • Member functions do not occupy the memory space of the object, only the member variables occupy the memory space of the object. When instantiating, the memory is applied for according to the total number of bytes occupied by the member variables of the class (satisfying memory alignment)

virtual function

  1. When a class includes a virtual function, the class automatically contains a pointer to the virtual function vptr, and the virtual function table stores pointers (function pointers) to all virtual functions defined in the class, and can quickly access the virtual function through vptr.
  2. The derived class does not rewrite the virtual function of the base class, and does not inherit the vptr pointer of the base class. When performing polymorphic operations, if the virtual function of the derived class (virtual function with the same name as the base class) is called, polymorphic behavior will not occur , the virtual function of the base class is still called.
  3. The derived class rewrites the virtual function of the base class, and the compiler reallocates the memory space for the rewritten virtual function. When performing polymorphic operations, if the virtual function of the derived class is called, polymorphic behavior will occur. It will be a virtual function of the derived class!
  4. The derived class rewrites some virtual functions of the base class, and the rewritten part is the derived class.

Memory

  1.  The size of the storage space occupied by the object is equal to the sum of the storage space occupied by each member variable (memory alignment)
  2. When the memory is used up, virtual memory will be used, that is, written to the hard disk, and then the program will be killed.

video tutorial

Link: https://pan.baidu.com/s/1W0cfUPxnf9c8egzMm1rBVA  Extraction code: jpy3 
 

Guess you like

Origin blog.csdn.net/matt45m/article/details/130179795