J's Study log

[C++] Multiple Inheritance 본문

Computer Language/C++

[C++] Multiple Inheritance

정우섭 2020. 4. 30. 20:18

Multiple Inheritance literally means that two or more classes' inheritance to a derived class.

 

Views on Multiple Inheritance

 

Mutiple Inheritance has sparked controversy.

 

Programmers view on multiple inheritance is not actually good.

 

But to understand the libraries which are using mutiple inheritance,

 

we should study how this works.

 

 

 

How to Use

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
 
using namespace std;
 
 
class BaseOne
{
public:
    void SimpleFunOne()
    {
        cout<<"BaseOne"<<endl;
    }
};
class BaseTwo
{
public:
    void SimpleFunTwo()
    {
        cout<<"BaseTwo"<<endl;
    }
};
class MultiDerived : public BaseOne, protected BaseTwo
{
public :
    void ComplexFunc()
    {
        SimpleFunOne();
        SimpleFunTwo();
    }
};
int main()
{
    MultiDerived mdr;
    mdr.ComplexFunc();
    mdr.SimpleFunOne(); //can access because BaseOne is inherited in public
/*
    mdr.SimpleFunTwo(); //cannot access because BaseTwo is inherited in protected
*/
    return 0;
}
 

 

Result

 
 
 
BaseOne
BaseTwo
BaseOne
 

This simple example looks nothing special.

 

But, look up the following code and find the problem of multiple inheritance and it's solution.

 

 

Ambiguous of Multiple Inheritance

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class BaseOne
{
public:
    void SimpleFun()
    {
        cout<<"BaseOne"<<endl;
    }
};
class BaseTwo
{
public:
    void SimpleFun()
    {
        cout<<"BaseTwo"<<endl;
    }
};
class MultiDerived :public BaseOne,protected BaseTwo
{
public:
    void ComplexFunc()
    {
        BaseOne::SimpleFun();
        BaseTwo::SimpleFun();
    }
};
int main()
{
    MultiDerived mdr;
    mdr.ComplexFunc();
 
    return 0;
}
 

 

Result

 
 
BaseOne
BaseTwo
 

 

As you can see in the code, two base classes BaseOne and BaseTwo both have declared void SimpleFunc( ) function.

 

So the drived class MultiDerived is not able to call only by the name of member function.

 

The simple solution, which is already in the code, is using "::" scope resolution operator.

 

 

 

Virtual Inheritance

 

The following code is another case of Ambiguous.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
using namespace std;
 
class Base
{
public:
    Base() {
        cout<<"Base Constructor"<<endl;
    }
    void SimpleFunc() {
        cout<<"BaseOne"<<endl;
    }
};
 
class MiddleDerivedOne : virtual public Base
{
public:
    MiddleDerivedOne() : Base()
    {
        cout<<"MiddleDerivedOne Constructor"<<endl;
    }
    void MiddleFuncOne()
    {
        SimpleFunc();
        cout<<"MiddleDerivedOne"<<endl;
    }
};
 
class MiddleDerivedTwo : virtual public Base
{
public:
    MiddleDerivedTwo() : Base()
    {
        cout<<"MiddleDerivedTwo Constructor"<<endl;
    }
    void MiddleFuncTwo()
    {
        SimpleFunc();
        cout<<"MiddleDerivedTwo"<<endl;
    }
};
 
class LastDerived : public MiddleDerivedOne, public MiddleDerivedTwo
{
public:
    LastDerived() : MiddleDerivedOne(), MiddleDerivedTwo()
    {
        cout<<"LastDerived Constructor"<<endl;
    }
    void ComplexFunc()
    {
        MiddleFuncOne();
        MiddleFuncTwo();
        SimpleFunc();
    }
};
 
int main(void)
{
    LastDerived ldr;
    cout<<"---after constructing objects---"<<endl;
    ldr.ComplexFunc();
    return 0;
}
 

 

Result

 
 
 
 
 
 
 
 
 
 
Base Constructor
MiddleDerivedOne Constructor
MiddleDerivedTwo Constructor
LastDerived Constructor
---after construction objects---
BaseOne
MiddleDerivedOne
BaseOne
MiddleDerivedTwo
BaseOne
 

 

Without 'virtual' declare in line15 and line29 the structure of the objects will be like this.

 

The structure of the objects without 'virtual inheritance'

LastDerived class inherits Base class two times. 

 

And with 'virtual' declare the structure is like this.

 

The structure of the code

Base class is virtual inherited and the structure became as how we intended.

 

So the Base class's constructor is called only one time in this code.

 

 

 

 

 

 

 

 

[참고자료]

- 윤성우의 열혈 C++ 프로그래밍

Comments