Objective functions of C ++
1. Why solve what polymorphism means that the same entity has multiple forms at the same time. It is an important feature of Object-Oriented Programming (OOP. If a language only supports classes but does not support polymorphism, it can only be indicated that it is object-based, rather than object-oriented. The polymorphism in C ++ is embodied in running and compiling. The Runtime polymorphism is a dynamic polymorphism, And the referenced objects can be determined only at runtime. During compilation, polymorphism is static polymorphism, and the object format can be determined during compilation. (Static binding and dynamic binding) polymorphism: the same operation acts on different objects and can have different interpretations to produce different execution results. At runtime, you can call methods in the derived class by pointing to the base class pointer. In C ++, there are the following methods to realize polymorphism: virtual functions, abstract classes, overwrites, templates (overload and polymorphism are irrelevant ). Polymorphism means that the parameter or return value of the method can be input or returned by the parent type. Assign a pointer of the subclass type to a pointer of the parent class.
In fact, for this purpose, function pointers can also achieve polymorphism. Or, in turn, the virtual function searches for its virtual table pointer * _ vptr (void **) through the class name **).
2. In simple terms, member functions modified by virtual keywords are virtual functions. Its general implementation model is: each class has a virtual table, which contains the addresses of the virtual functions in this class, each object then has a vptr pointing to the virtual table. This class has four more bytes. These four bytes are the VTABLE address. Each virtual function in this class is a member of the VTABLE array.
3. Implementation principle working mechanism as long as we have a virtual function class, the compiler will create a VTABLE for this class and use it to save all the virtual function addresses of this class. We call it a function pointer array. Each element of this array is the address of the virtual function. The compiler adds a pointer vptr to the vtable. VPTR is the entry address of VTABLE. The value of VPTR depends on the address of the object memory. The VTABLE of each class is independent and unique. That is to say, when an instance (object) of the derived class is constructed, this class has a virtual table, and each instance (object) there will be a virtual pointer (vptr) pointing to the virtual function tabl of this class.
4. Example: # include // Www.realtoptv.com by zhangyj // ==================================== ========================================/// namespace declaration // ========================================================== ====== using std:: cout; using std: endl; // Y = 0.30R + 0.59G + 0.11B, U = 0.493 (B-Y), V = 0.877 (R-Y) // R = Y + 1.4075 * (V-128) // G = Y-0.3455 * (U-128)-0.7169 * (V-128) // B = Y + 1.779 * (u- 128) // ================================================ =============================== ========================================== class RGB2YUV {public: // default constructor RGB2YUV (int r = 1, int g = 1, int B = 1): m_r (r), m_g (g), m_ B (B) {cout <RGB2YUV constructor called <endl;} // defined as virtual int Y () {return 0.30 * m_r + 0.59 * m_g + 0.11 * m_ B ;} virtual int U () {return 0.493 * (m_ B-Y ();} virtual int V () {return 0.877 * (m_r-Y ());} // display void ShowY () {cout <Y volume is <Y () <endl;} void ShowU () {cout <U volume is <U () <endl ;}void ShowV () {cout <V volume is <V () <endl ;}protected: int m_r; int m_g; int m_ B ;}; // ================================================ =============================== ========================================== class PS_RGB2YUV: public RGB2YUV {public: PS_RGB2YUV (int r, int g, int B): RGB2YUV (r, g, B) {}// virtual int Y () {return 0.30 * m_r + 0.59 * m_g + 0.11 * m_ B + 1;} virtual int U () {return 0.493 * (m_ B-Y () + 1 ;} virtual int V () {return 0.877 * (m_r-Y () + 1 ;}}; // ================================================ ==============================/// Function Name: main // function description implementation mechanism of virtual functions // function parameters: NONE // return value: 0 // =================================================== ============================ int main () {RGB2YUV Wyuv (32, 32, 32); // The base class object defines PS_RGB2YUV Wyuv_offset (32, 32, 32); // The derived class object definition // displays Wyuv. showY (); Wyuv. showU (); Wyuv. showV (); // display the Wyuv_offset.ShowY (); Wyuv_offset.ShowU (); Wyuv_offset.ShowV (); // display the size of the class cout <Wyuv's size is <sizeof (Wyuv) <endl; cout <Wyuv_offset's size is <sizeof (Wyuv_offset) <endl; system (pause );}
8. disassembly Analysis
// Defined as virtual function 29: virtual int Y () 30: {013C4AA0 55 push ebp 013C4AA1 8B EC mov ebp, esp 013C4AA3 81 ec cc 00 00 00 sub esp, 0CCh 013C4AA9 53 push ebx 013C4AAA 56 push esi 013C4AAB 57 push edi 013C4AAC 51 push ecx 013C4AAD 8D BD 34 FF lea ediebp-0CCh] 013C4AB3 B9 33 00 00 00 mov ecx, 33 h 013C4AB8 B8 CC mov eax, 0 CCCCCCCCh 013C4ABD F3 AB rep stos dword ptr es: [edi] 013C4ABF 59 pop ecx 013C4AC0 89 4D F8 mov dword ptr [this], ecx // each instance has this, and the value in the register ECX serves as the this pointer address 31: return 0.30 * m_r + 0.59 * m_g + 0.11 * m_ B; 013C4AC3 8B 45 F8 mov eax, dword ptr [this] 013C4AC6 F2 0F 2A 40 04 cvtsi2sd xmm0, dword ptr [eax + 4] 013C4ACB F2 0F 59 05 60 DC 3C 01 mulsd xmm0, mmword ptr ds: [13cfd 60h] 013C4AD3 8B 4D F8 mov ecx, dword ptr [this] 013C4AD6 F2 0F 2A 49 08 cvtsi2sd xmm1, dword ptr [ecx + 8] 013C4ADB F2 0F 59 0D C8 DC 3C 01 mulsd xmm1, mmword ptr ds: [13CDCC8h] 013C4AE3 F2 0F 58 C1 addsd xmm0, xmm1 013C4AE7 8B 55 F8 mov edx, dword ptr [this] 013C4AEA F2 0F 2A 4A 0C cvtsi2sd xmm1, dword ptr [edx + 0Ch] 013C4AEF F2 0F 59 0D B0 DB 3C 01 mulsd xmm1, mmword ptr ds: [13CDBB0h] 013C4AF7 F2 0F 58 C1 addsd xmm0, xmm1 013C4AFB F2 0F 2C C0 cvttsd2si eax, xmm0 32 :}
Virtual int Y () // adjusted twice value 74: {013C4A30 55 push ebp 013C4A31 8B EC mov ebp, esp 013C4A33 81 ec cc 00 00 00 sub esp, 0CCh 013C4A39 53 push ebx 013C4A3A 56 push esi 013C4A3B 57 push edi 013C4A3C 51 push ecx 013C4A3D 8D BD 34 FF lea edi, [ebp-0CCh] 013C4A43 B9 33 00 00 mov ecx, 33 h 013C4A48 B8 CC mov eax, 0 CCCCCCCCh 013C4A4D F3 AB rep stos dword ptr es: [edi] 013C4A4F 59 pop ecx 013C4A50 89 4D F8 mov dword ptr [this], ecx // each instance has this, and the value in the register ECX serves as the this pointer address.
75: return 0.30 * m_r + 0.59 * m_g + 0.11 * m_ B + 1; 013C4A53 8B 45 F8 mov eax, dword ptr [this] // tune its instance 013C4A56 F2 0F 2A 40 04 cvtsi2sd xmm0, dword ptr [eax + 4] 013C4A5B F2 0F 59 05 60 DC 3C 01 mulsd xmm0, mmword ptr ds: [13cfd 60h] 013C4A63 8B 4D F8 mov ecx, dword ptr [this] 013C4A66 F2 0F 2A 49 08 cvtsi2sd xmm1, dword ptr [ecx + 8] 013C4A6B F2 0F 59 0D C8 DC 3C 01 mulsd xmm1, mmword ptr ds: [13CDCC8h] 013C4A73 F2 0F 58 C1 addsd xmm0, xmm1 75: return 0.30 * m_r + 0.59 * m_g + 0.11 * m_ B + 1; 013C4A77 8B 55 F8 mov edx, dword ptr [this] // tune its instance 013C4A7A F2 0F 2A 4A 0C cvtsi2sd xmm1, dword ptr [edx + 0Ch] 013C4A7F F2 0F 59 0D B0 DB 3C 01 mulsd xmm1, mmword ptr ds: [13CDBB0h] 013C4A87 F2 0F 58 C1 addsd xmm0, xmm1 013C4A8B F2 0F 58 05 98 DD 3C 01 addsd xmm0, mmword ptr ds: [13CDD98h] 013C4A93 F2 0F 2C C0 cvttsd2si eax, xmm0 76 :}
9. Summarize the advantages and disadvantages of A, dynamic (late) binding and static (early) binding.
B. Advantages and Disadvantages of memory layout C. Multiple inheritance view advantages and disadvantages D. How many implementation methods can achieve the same function