Single inheritance and multiple inheritance and boss diamond inheritance

single inheritance

  A subclass can have only one direct parent class called single inheritance. Simple one-to-one relationship.
  write picture description here

multiple inheritance

 1. A subclass with multiple direct parent classes is called multiple inheritance. One-to-many relationship.
  write picture description here

class AA
{
public:
    int _a;
};
class BB 
{
public:
    int _a;
    int _b;
};
class CC :public BB,public AA
{
public:
    int _c;
};
void Test1()
{
    CC c;
}

  Multiple inheritance enables a subclass to inherit the characteristics of multiple parent classes. It can be regarded as an extension of single inheritance. It has multiple base classes. There is a one-to-one single inheritance relationship between the derived class and each base class. The memory of the subclass is stored in the order of inheritance. But just because it can inherit multiple parent classes, it causes two problems: data redundancy and ambiguity are
  like the above example: parent classes AA and BB both have member variables _a, then after the subclass CC inherits, CC As it should be, it also has two non-private members of the parent class, then two member variables _a are stored in the CC, so when the object c accesses _a, is it the parent class AA or the parent class BB?
  Solution:
  First: use the scope character ::
  to directly tell you which parent class the object c accesses, it is definitely not wrong.

CC c;
c.AA::_a = 1;

  Second: virtual inheritance

 2. Constructors with multiple inheritance The
   first thing to remember: constructors are not inherited!  
   The execution order of the constructors of the derived class is to execute the constructor of the base class, and then execute the constructor of the derived class itself. The execution order of the constructors of the base classes at the same level depends on the order of the base classes specified when the derived class is defined. The order of the items in the member initialization list defined in the constructor of the derived class is irrelevant. That is, the order in which the base class constructors are executed depends on the order in which the base classes were defined when the derived class was defined. It can be seen that the order of items in the member initialization list of the derived class constructor can be arranged arbitrarily. Just like the above example: call the constructor of BB first, followed by AA's, and finally the subclass itself.

diamond inheritance

  With multiple inheritance, there must be diamond-shaped inheritance. In a multiple-inheritance relationship, multiple parent classes inherit from another parent class at the same time, which is diamond-shaped inheritance. Like multiple inheritance, diamond inheritance can allow subclasses to have more characteristics of parent classes. At the same time, diamond inheritance is bound to cause fatal problems: data redundancy and ambiguity.
  write picture description here
 Post on the code:
 

class Person
{
public :
    string _name ; // 姓名
};
class Student : public Person
{
protected :
    int _num ; //学号
};
class Teacher : public Person
{
protected :
    int _id ; // 职工编号
};
class Assistant : public Student, public Teacher
{
protected :
    string _majorCourse ; // 主修课程
};
void Test ()
{
// 解决二义性方法1:(作用域符)
Assistant a ;
a.Student ::_name = "xxx";
a.Teacher ::_name = "yyy";
}

  The subclass Assistant inherits two parent classes, and the two parent classes inherit from another parent class. For the subclass Assistant, there is an ambiguity problem.
  The ambiguity problem is solved by the scope character above, and the method of virtual inheritance is used here.
  So what is virtual inheritance? Adding the keyword virtual in front of the inheritance method is virtual inheritance

class AA 
{
public:
    int _a;
};
class BB : virtual public AA//虚继承
{
public:
    int _b;
};
class CC : virtual public AA//虚继承
{
public:
    int _c;
};
class DD : public BB, public CC
{
public:
    int _d;
};

  So, how does virtual inheritance resolve ambiguity?
  Virtual inheritance allows only one copy of conflicting member variables to be saved, that is, one share is shared, so that there will be no ambiguity problem.
  It's like three children are watching a TV, then one child changes the channel, then the other two children see the program after the change.
  write picture description here

  Through virtual inheritance, we save only one copy of the member variable with ambiguity, and other objects find this member variable through the virtual base table pointer, thus solving the ambiguity problem.
  At the same time, virtual inheritance also solves its data redundancy problem, although two virtual base table pointers are added to the memory in the above example, which makes the memory size larger, which is 4 bytes more than without virtual inheritance. However, when the cardinality is large, the advantages of virtual inheritance are manifested. I just added two virtual base table pointers to point to the shared member variable location. I no longer have to inherit the data of the parent class as before, which greatly reduces the Reduced space overhead.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324696631&siteId=291194637