I. The nature of inheritance
1, in the C + + compiler's internal class can be understood as the structure body
2. Subclasses are the new members of the parent class that are superimposed on the child class.
Ii. The nature of polymorphism
1. When a virtual function is declared in a class, the compiler generates a virtual function table in the class
2. A virtual function table is a data structure that stores the address of a member function
3, virtual function table is automatically generated and maintained by the compiler
4.the virtual member function will be put into the virtual function table by the compiler
5. When a virtual function is stored, each object has a pointer to a virtual function table
#include <iostream>using namespacestd;classdemo{protected: intmi; intMJ; Public: Virtual voidprint () {cout<<"mi ="<< mi <<","<<"MJ ="<< MJ <<Endl; }};classDerived: Publicdemo{Private: intMk; Public: Derived (intIintJintk) {mi=i; MJ=J; Mk=K; } voidprint () {cout<<"mi ="<< mi <<","<<"MJ ="<< MJ <<","<<"mk ="<< Mk <<Endl; }};structtest{void*p; intmi; intMJ; intmk;};intMain () {cout<<"sizeof (Demo) ="<<sizeof(Demo) <<Endl; //12 instead of 8, cause: A pointer to a virtual function table is plugged into the class cout<<"sizeof (Derived) ="<<sizeof(Derived) <<Endl; //16 rather than Derived D (1,2,3); structTest *p = reinterpret_cast<test*> (&d); //indicates that the structure and object have the same memory distribution, the first pointer to a virtual function table, followed by MI, MJ, Mk cout<<"before chinging ..."<<Endl; D.print (); //mi = 1, MJ = 2, mk = 3 p->mi =Ten; P->MJ = -; P->MK = -; cout<<"After chinging ..."<<Endl; D.print (); //mi = ten, MJ =, mk = return 0;}
third, implement object-oriented with C language
1, must realize three major characteristics: encapsulation, inheritance, polymorphism
2. Use the void* pointer to ensure that members of the structure are not accessed by the outside world to achieve encapsulation
3, through the parent class structure as the first member of the subclass structure to implement inheritance
4, using the structure to simulate the virtual function table to achieve polymorphism
(1), defining a pointer to a virtual function table in a class
(2), define the virtual function table structure of the corresponding type according to the pointer type and populate the corresponding virtual function pointers
(3), generates a virtual function table of subclasses and parent classes , used by the compiler by the C + + virtual function table, and adds the static keyword to the simulation
(4), associating concrete objects with virtual function tables
(5), the realization of the real virtual function, is also static
(6), transform virtual function interface (Find concrete implementation function in virtual function table)
(7), the use of similar methods to improve the sub-category
Add.h:
#ifndef _add_h_#define_add_h_typedefvoidDemo;typedefvoidDerived;demo* Demo_create (intIintj);intDemo_geti (demo*pThis);intDEMO_GETJ (demo*pThis);intDemo_add (demo* pThis,intvalue);voidDemo_free (demo*pThis);D erived* Derived_create (intIintJintk);intDERIVED_GETK (derived*pThis);intDerived_add (derived* pThis,intvalue); //Rewrite the Add function #endif
ADD.C:
#include"add.h"#include<malloc.h>Static intDemo_virtual_add (demo* pThis,intvalue);Static intDerived_virtual_add (demo* pThis,intvalue);structvtable{int(*padd) (void*,int);//2. Define the virtual function table structure and populate the corresponding virtual function pointers};structclassdemo{structVTable *ptr;//1. Define a pointer to a virtual function table in a class intmi; intMJ;};structclassderived{structClassdemo D; intmk;};Static structVTable demo_virtual_table = {Demo_virtual_add};//3. Create a virtual function table of subclasses and parent classes, used by the compiler by the C + + virtual function table, and add the static keyword to the simulationStatic structVTable derived_virtual_table = {Derived_virtual_add};//3//Demo Parent Classdemo* Demo_create (intIintj) { structclassdemo* ret =malloc(sizeof(structClassdemo)); if(Ret! =NULL) {ret->ptr = &Demo_virtual_table;//4. Associating specific objects with virtual function tablesRet->mi =i; RET->MJ =J; } returnret;}intDemo_geti (demo*pThis) { structclassdemo* ret = (structClassdemo *) (pThis); returnRet->mi;}intDEMO_GETJ (demo*pThis) { structclassdemo* ret = (structClassdemo *) (pThis); returnRet->MJ;}Static intDemo_virtual_add (demo* pThis,intValue//5. Implement a true virtual function{ structclassdemo* ret = (structClassdemo *) (pThis); return(Ret->mi + RET->MJ +value);}intDemo_add (demo* pThis,intValue{ structclassdemo* ret = (structClassdemo *) (pThis); returnRet->ptr->Padd (pThis, value); //6. Transforming the virtual function interface }voidDemo_free (demo*pThis) { Free(pThis);}//derived sub-classderived* Derived_create (intIintJintk) { structclassderived* ret =malloc(sizeof(structclassderived)); if(Ret! =NULL) {ret->d.ptr = &derived_virtual_table; RET->D.MI =i; RET->D.MJ =J; RET->MK =K; } returnret;}intDERIVED_GETK (derived*pThis) { structclassderived* ret = (structclassderived*) (pThis); returnRet->Mk;}Static intDerived_virtual_add (demo* pThis,intvalue) { structclassderived* ret = (structclassderived*) (pThis); returnRET->MK +value;}intDerived_add (derived* pThis,intvalue) { structclassderived* ret = (structclassderived*) (pThis); returnRet->d.ptr->Padd (pThis, value);}
MAIN.C:
#include <stdio.h>#include<malloc.h>#include"add.h"voidRun (demo* p,intv) { intR =Demo_add (P, v); //Imitate C + +, the member function of the parent class is called by default printf ("Run (p,v) =%d\n", R);}intMain () {Demo* PB = Demo_create (1,2); Derived* PD = Derived_create (4,5,6); printf ("Demo_add (pb,3) =%d\n", Demo_add (Pb,3));//1+2+3=6printf"Derived_add (pd,3) =%d\n", Derived_add (PD,3));//6+3=9Run (Pb,3); //1+2+3=6
Run (PD,3); //6+3=9 ===>demo_add (pd,3) ===> return Pd->ptr->padd (PD, 3) ====> and pd->ptr = &demo_virtual_table
Demo_free (PB);
Demo_free (PD);
return0;
}
Iv. Summary
1. The essence of inheritance is the superposition of member variables between father and son
2. The polymorphism of C + + is realized by the virtual function table .
3, virtual function table is automatically generated and maintained by the compiler
4, the call efficiency of virtual function is lower than the ordinary member function
51st Lesson, C + + object Model analysis (bottom)