Class inheritance in C + + single Inheritance & Multiple Inheritance & Diamond inheritance

Source: Internet
Author: User

Single inheritance is a general single inheritance, when a subclass has only one direct parent class that is called the inheritance relationship as a single inheritance. This kind of relationship is relatively simple and one-on relationship:

Multiple inheritance refers to a subclass that has two or more direct parent classes that are called this inheritance relationship for multiple inheritance. This inheritance allows a subclass to inherit the attributes of more than one parent class. Multiple inheritance can be seen as an extension of single inheritance. A derived class has more than one base class, and the relationship between the derived class and each base class can still be considered a single inheritance. Multiple inheritance The constructor of a derived class is similar to a derived class constructor under single inheritance, and it must also be responsible for the invocation of all base class constructors for that derived class. Also, the number of arguments for a derived class must contain the number of arguments required to complete initialization of all base classes. In the memory of the subclasses they are stored in the order defined by the Declaration, which is clearly seen below.
But there is a problem with multiple inheritance, to study this problem, we start from the single inheritance. Look at the memory space:

Class Base
{
Public
Base () {
cout << "B ()" << Endl;
}
int B1;
};
Class Derive:public Base
{
Public
Derive () {
cout << "D ()" << Endl;
}
int D1;
};
int main ()
{
Test ();
GetChar ();
return 0;
}

Multiple inherited memory space:

Class Base
{
Public
Base () {
cout << "B ()" << Endl;
}
int B1;
};
Class C
{
Public
C () {
cout << "C ()" << Endl;
}
int C;
};
Class Derive:public Base, public C
{
Public
Derive () {
cout << "D ()" << Endl;
}
int D1;
};

Diamonds inherit the distribution of data in memory:

Class A
{
Public
A () {
cout << "A ()" << Endl;
}
int A;
};
Class Base:public A
{
Public
Base () {
cout << "B ()" << Endl;
}
int B1;
};
Class C:public A
{
Public
C () {
cout << "C ()" << Endl;
}
int C;
};
Class Derive:public Base, public C
{
Public
Derive () {
cout << "D ()" << Endl;
}
int D1;
};
initializing int a=4 in Class A can clearly see the memory distribution in the diamond inheritance so there are two data members in Class A in subclass derive, which creates the problem of access ambiguity and data redundancy. This is the problem of multiple inheritance that I said earlier. You can access this

1 tmp. C::A=4;2 tmp. base::a=5;
What is an object model

There are two concepts that can explain the C + + object model:

1, the language directly supports the object-oriented programming part.
2, for a variety of support for the underlying implementation mechanism.

There is another way to solve this problem, we need to use a new method of inheritance: Virtual inheritance-to solve the two semantics of diamond inheritance and data redundancy problem. Look at the following code:

1 class Base 2 {3 Public:4     Base () {5         cout << "B ()" << Endl; 6     } 7     int b1; 8}; 9 Class Deri Ve:virtual public Base10 {public:12     Derive () {cout         << D () << endl;14     }15     int d1; 16 };17 void Test () {     Derive tmp;20     tmp.d1 = 1;21     tmp.b1 = 2;23}24 int main () {+     test ();     ge TCHAR ();     return 0;29}

Virtual Inheritance of keywords---vsan

1 Class A 2 {3 public:4     A () {5         cout << "A ()" << Endl; 6     } 7     int A; 8}; 9 Class Base:vir Tual public A10 {public:12     Base () {         cout << B () << endl;14     }15     int b1;16};17 class C:v Irtual public A18 {public:20     C () {         cout << "C ()" << endl;22     }23     int c;24};25 class De Rive:virtual public Base, virtual public C26 {public:28     Derive () {         cout << D () << endl;30
   }31     int d1;};33 void Test ()     Derive tmp;36 tmp.d1     = 1;37     tmp.b1 = 2;38     tmp.c = 3;39< C20/>TMP.A = 4;40}41 int main () () () ()     ;     GetChar ();     return 0;46}

The object model of Diamond virtual inheritance solves two the semantic problem is offset in the VS environment, instead of pointing directly at the graph, it's just for a more intuitive display.

Class inheritance in C + + single Inheritance & Multiple Inheritance & Diamond inheritance

Related Article

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.