C ++ inheritance and derivation (Principle induction)

Source: Internet
Author: User

C ++ inheritance and derivation (Principle induction)
1. C ++ inheritance is different from java. java Follows single inheritance, but java interfaces make up for its shortcomings. C ++ is flexible and multi-inheritance. That is, a C ++ class can inherit the attributes of N classes at the same time. 2. There are three inheritance Methods: public, private, and protect. For Classes inherited by public, the public members are still public members, and the private Members are still private members. For protect, private is restricted, just like a water pipe. The public water pipe is the largest and there is no limit on the water flow. The protection pipe is of medium numbers. The traffic of the large pipe turns it into medium traffic, which is not limited to medium or lower. The private pipe is the minimum number. The private pipe standard should be changed to the private pipe standard if the private pipe exceeds the limit. Private inheritance is actually a sterilization measure. That is, future inheritance does not make much sense. 3. for the generalization of inheritance about the constructor and the structure sequence, see the code: 1 # include <iostream> 2 using namespace std; 3 4 class Base1 {5 6 public: 7 Base1 () {8 cout <"Default Base1" <endl; 9} 10 Base1 (int I) {11 cout <"Base1" <I <endl; 12} 13 ~ Base1 () {14 cout <"Base1 destructor" <endl; 15} 16}; 17 18 class Base2 {19 20 public: 21 Base2 () {22 cout <"Default Base2" <endl; 23} 24 ~ Base2 () {25 cout <"Base2 destructor" <endl; 26} 27 Base2 (int I) {28 cout <"Base2" <I <endl; 29} 30}; 31 class Base3 {32 33 public: 34 Base3 () {35 cout <"Default Base3" <endl; 36} 37 ~ Base3 () {38 cout <"Base3 destructor" <endl; 39} 40 Base3 (int I) {41 cout <"Base3" <I <endl; 42} 43 44}; 45 46 47 class Derived: public Base1, public Base2, public Base3 // (1) start construction here from left to right 48 // The structure is from right to left 49 {50 51 public: 52 Derived () {53 cout <"Default Derived" <endl; 54} 55 ~ Derived () {56 cout <"Derived destructor" <endl; 57} 58 Derived (int a, int B, int c, int d) 59: men2 (B), Base1 (a), Base3 (c), Base2 (d), men1 (B) {60 cout <"Derived" <endl; 61 }; 62 63 private: 64 // construct 65 Base3 men3 from left to right; 66 Base2 men2; 67 Base1 men1; 68 69 // The structure is upgraded from the bottom, base 370,}; 71 72 int main (void) {73 74 Derived obj (,); 75 return 0; 76 77} Base11Base24Base33Default Base3Base22Base12Derive DDerived: Base1: Base2: Base3: Base3: Base2: Base1: press any key to continue... 4. the above is the description of single inheritance. If it is multi-inheritance, we can easily understand the execution sequence of the machine. But what if it is virtual inheritance? 1. now let's look at the figure: How is the running result ....... 1 # include <iostream> 2 using namespace std; 3 4 class Boss {5 6 public: 7 Boss () {8 cout <"this is Boss's constructor! "<Endl; 9}; 10 Boss (int I) {11 cout <" this is Boss's constructor! "\ 12 <" moneny = "<I <endl; 13} 14 15 void show () {16 cout <" Sword sharpening, soul demon, time matte, magic Blade "<endl; 17} 18 virtual ~ Boss () {19 cout <"this is Boss's xigou function! "<Endl; 20}; // virtual destructor 21}; 22 23 // shop 2's 24 class xiao_er: virtual public Boss 25 {26 public: 27 xiao_er () {28 cout <"this is xiao_er's constructor! "<Endl; 29} 30 xiao_er (int I): Boss (I) {31 cout <" this is xiao_er's constructor! "\ 32 <" moneny = "<I <endl; 33} 34 virtual ~ Xiao_er () {35 cout <"this is xiao_er's xigou function! "<Endl; 36} 37 void show () {38 cout <" I Am a store administrator, guest! "<Endl; 39} 40}; 41 42 // Wang erxiao 43 class er_xiao: virtual public Boss \ 44, virtual public xiao_er/* This can be omitted here, but this is to write code */45 {46 public: 47 er_xiao () {48 cout <"this is er_xiao's constructor! "<Endl; 49} 50 er_xiao (int I): \ 51 Boss (I), xiao_er (I + 1) 52 {53 cout <"this is er_xiao's constructor! "\ 54 <" moneny = "<I <endl; 55} 56 virtual ~ Er_xiao () {57 cout <"this is er_xiao's xigou function! "<Endl; 58} 59 void show () {60 cout <" I'm Wang erxiao, the hero of the bad guy! "<Endl; 61} 62}; 63 64 // tianchao VIP staff 65 class VIP_em: virtual public Boss 66 {67 68 public: 69 VIP_em () {70 cout <"this is VIP_em's constructor! "<Endl; 71} 72 73 VIP_em (int I): \ 74 Boss (I) 75 {76 cout <" this is VIP_em's constructor! "\ 77 <" moneny = "<I <endl; 78} 79 virtual ~ VIP_em () {80 cout <"this is VIP_em's xigou function! "<Endl; 81} 82 void show () {83 cout <" I Am a VIP, and I have the privilege! "<Endl; 84} 85}; 86 87 // bear child 88 class stupid_kid: virtual public V, virtual public xiao_er, \ 90 virtual public er_xiao 91 {92 public: 93 stupid_kid () {94 cout <"this is stupid_kid's constructor! "<Endl; 95} 96 97 stupid_kid (int I): \ 98 VIP_em (I), xiao_er (12), er_xiao (13), xe (I) 99 {100 cout <"this is stupid_kid's constructor! "\ 101 <" moneny = "<I <endl; 102} 103 ~ Stupid_kid () {104 cout <"this is stupid_kid's xigou function! "<Endl; 105} 106 107 void show () {108 cout <" I'm a bear child, Shu, Shu, hug! "<Endl; 109} 110 private: 111 VIP_em vi; 112 xiao_er xe; 113 er_xiao ex; 114}; 115 116 117 int main () {118 100 stupid_kid st ); 119 // The parent class function is overwritten with 120 st. show (); 121 // how to call the parent class is mandatory. 122 (Boss) st ). show (); 123 124 // stupid_kid * pt = & st; 125 // stupid_kid & sb = st; 126 // pt-> show (); 127 // (Boss) sb ). show (); 128 return 0; 129}: this is Boss's constructor! This is VIP_em's constructor! Moneny = 100 this is xiao_er's constructor! Moneny = 12 this is er_xiao's constructor! Moneny = 13 ------------- this part is the inheritance part of the child bear constructor. below is the constructor of the private variable. this is the Boss's constructor! This is VIP_em's constructor! ------ Private variable Vip_em call the non-parameter constructor this is Boss's constructor! Moneny = 100 this is xiao_er's constructor! Moneny = 100 ------ private variable xiao_er calls the constructor with parameters this is Boss's constructor! This is xiao_er's constructor! ------ The private variable xiao_er calls the non-parameter constructor. this is er_xiao's constructor! This is stupid_kid's constructor! Moneny = 100 I'm a bear child, Shu, Shu, hug! The sword is honed, the demon is the soul of the soul, the time is frosted, And the magic blade is out of this is the Boss's xigou function! This is stupid_kid's xigou function! This is er_xiao's xigou function! This is xiao_er's xigou function! This is the Boss's xigou function! This is xiao_er's xigou function! This is the Boss's xigou function! This is VIP_em's xigou function! This is the Boss's xigou function! This is er_xiao's xigou function! This is xiao_er's xigou function! This is VIP_em's xigou function! This is the Boss's xigou function! Press any key to continue... 6. From the above Code, it is not difficult to see that the virtual inheritance avoids the ambiguity and only compresses the public your virtual class inheritance class. To understand virtual inheritance, you must first know virtual table (vtbl)-the virtual function table we are talking about is in the memory block, and a continuous memory block will be left, the JMP address is used for vtble storage, and the virtual function address is stored in vtble. Each time the base class inherits, A vptr pointer is generated, pointing to the geology of the derived class, when the vptr Pointer Points to the same address, it is not constructed repeatedly. Other constructor and destructor are based on the first code column. Based on your understanding of the source code and the test summary, please correct me if there are any errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.