1. what is the difference between struct and struct in C language? in C language, struct is only used to define complex data structures. In other words, it can only define data and its members can only be data types, method functions cannot be defined directly. In C ++, in addition to the usage in C, struct can also define Member method functions like class. 2. What is the difference between struct and class: they can all define classes in the object-oriented model: defining member data and member method functions. Difference: the default access attribute of struct members is public, while the default access attribute of class members is private. In addition, struct can only be used to define classes and cannot have other functions. The class can also have inheritance, polymorphism, virtual base class and template functions. 3. The function of extern "C" {} is C ++ compatible with C, but after all, the language implementation is different, so C and C ++ cannot be used together directly. C is a process-oriented language. The functions in the compiled symbol table are identified only by the names used for definition. C ++ is object-oriented and has many new features, such as function overloading, templates, and polymorphism. To support these object-oriented features, in addition to the function name, the compiled symbol table must have parameter-related information (similar to the method signature in Java) so that the correct function can be found at runtime. Therefore, Calling C functions directly in C ++ or Calling C ++ functions directly in C will report errors such as undefined reference at the link. To solve the preceding problem, the macro extern "C" is used to tell the compiler to compile and process the code in {} using C, in this way, C ++ and C can call each other. It is usually used in C ++ code. More specifically, it is used in the C ++ Compiler:
#ifdef __cplusplusextern "C"{#endif// C++ codes or C codes goes here#ifdef __cplusplus}#endif
For C code, if it is a C ++ compiler, you also need to use extern "C". If it is a C compiler, it doesn't matter. In short, it is introduced to solve the compatibility between C ++ and C. 4. how polymorphism is implemented depends on declaring virtual functions. Only when the base class function is virtual, only when the pointer of the base class actually points to the subclass that overwrites the virtual method will polymorphism occur. When the compiler discovers a virtual function, it immediately generates a virtual function table vtable for this class. The vtable items are pointers to virtual functions. A vptr pointer is also used to point to a specific vtable. When constructing polymorphism, the pointer of the base class is used, but the constructor of the subclass is used. Therefore, the vptr is directed to the vtable of the subclass to realize the polymorphism of dynamic binding. 5. Role of virtual 1: the condition for implementing polymorphism is: The method declaration of the base class is virtual; the pointer is of the base class, but points to the subclass, and the virtual method can be called to realize polymorphism. Purpose 2: If a time-out occurs when the common base class inherits multiple inheritance by reference, the number of objects in the common base class exceeds one, which may cause errors if you do not waste space, for example, you cannot tell which base class is referenced. During inheritance, the virtual object is inherited by reference, that is, the objects in the subclass are a reference of the base class. In this way, only one copy of the actual object exists, and all the other objects are referenced (remember, references are aliases. Class A: virtual public B6. how to design a class so that it cannot be inherited? similar to final in Java, class constructor and destructor can be declared as private, in this way, compilation errors occur because the parent class cannot be called to construct and analyze the parent class, thus preventing the class from being inherited during compilation.