일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- kubernetes
- virtual function
- 회귀학습
- Class
- 회귀
- call by reference
- regression
- 맵리듀스
- MapReduce
- member function
- call by value
- 지도학습
- inheritance
- overriding
- struct
- 선형모델
- Nonlinear Model
- 딥러닝
- Linear Regression
- 최소제곱법
- Machine learning
- least square
- local minimum
- c++
- 머신러닝
- 비선형 함수
- Nonlinear Function
- Effective C++
- 비선형모델
- Overloading
- Today
- Total
J's Study log
[C++] Multiple Inheritance 본문
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.
LastDerived class inherits Base class two times.
And with 'virtual' declare the structure is like this.
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++ 프로그래밍
'Computer Language > C++' 카테고리의 다른 글
[C++] Operator Overloading 1 (0) | 2020.05.02 |
---|---|
[C++] Default Value of the Parameter (0) | 2020.05.02 |
[C++] Operation principle of Member Function & Virtual Function (0) | 2020.04.25 |
[C++] Virtual Function (0) | 2020.04.01 |
[C++] Reference Relation of Object Pointer (0) | 2020.03.27 |