Beruflich Dokumente
Kultur Dokumente
Agenda
Permissions and Inheritance Multiple Inheritance Common Ancestor
There are obviously cases where we wish to deny access from all other classes (and the main), but to allow the derived classes to access this member (part of the family) The protected permission does just that !!!
Base private protected public V V V Derived X V V Other X X V
We can limit these permissions though, by using private and protected inheritance Those limitation will affect only the entities using the derived class (derived class from this class, main, etc.) and not the class itself.
copyrights Elhanan Borenstein
Multiple Inheritance
Multiple Inheritance
What have we already learned?
Simple Inheritance
Base
Polymorphism
Base
Derived
Derived1
Derived2
Multiple Inheritance
Introduction
There are cases when we wish to inherit the characteristics of more than one class. That is, we wish to have more than one class as the base class. Inheriting more than one class is permitted in C++. This mechanisms is called: Multiple Inheritance !
(In General, multiple inheritance should be used wisely. There are cases in which we will use other techniques to avoid multiple inheritance.)
Derived
Additional
copyrights Elhanan Borenstein
Multiple Inheritance
Guidelines
The same principles of single inheritance, still apply in multiple inheritance:
In case the base class (classes) ctors requires parameters, the derived class should pass these parameters through the initialization line. The derived class can use the methods inherited from the base class, even if they were hidden, using their full names (base::func()). This is particularly useful if there are members with identical names in both base classes (otherwise ambiguity). The derived class can use private members of the base classes through appropriate public methods (get(), set()). While inheriting, the inheritance type (public/protected/private) should be specified for each base class independently.
Example: multiple
copyrights Elhanan Borenstein
Multiple Inheritance
Common Ancestor
As we saw, when members with the same name are inherited by the derived class, we can (and must) access them using their full names. There is, however, an interesting situation where such a scenario is bound to happen. (WHEN?) Ancestor Base1 Ancestor Base2
Base1 a note about drawings Ancestor Base2 Derived
Derived
When both base classes are themselves inherited from a common base (ancestor) class: The members of the common ancestor will appear twice in the Derived class once through Base1 and once through Base2.
copyrights Elhanan Borenstein
Common Ancestor
Why do we actually ends up with duplicated copies? Does it make sense? Ancestor A; A: Ancestor Base1 Ancestor Base2 D: B1:
Ancestor (members)
B2:
Derived D; Derived
Ancestor (members) Base1 (additional) Ancestor (members) Base2 (additional) Derived (additional) Base1 Base2
Common Ancestor
What is it good for?
A common ancestor inheritance of that sort, is relevant only when the derived entity should, by definition, get two copies of the ancestors members. (Logical argument) Using members which were inherited twice takes place using their full names:
base2::member or base1::member
Members inherited from int m_speed Vehicle (twice): bool m_wings Additional members inherited from Car: x2 bool m_automatic
Example: bat
Additional members int m_missiles inherited from Airplane: Additional members in Batmobile: NONE
This is not what we want !!! Here we want the diamond case.
Ancestor Base1 Base2 Derived
Triangle Rectangle
Circle
Meshuribul
copyrights Elhanan Borenstein
Common Ancestor
Simple Inheritance
Ancestor A; A: B1:
Ancestor (members)
Virtual Inheritance
Ancestor A; A:
Ancestor (members) near ptr Base1 (additional) near ptr Base2 (additional) Ancestor (members)
B2:
Derived D; D:
Ancestor (members) Base1 (additional) Ancestor (members) Base2 (additional) Derived (additional) Base1 Base2
Ancestor (members)
Derived D; D:
near ptr Base1 (additional) near ptr Base2 (additional) Derived (additional) Ancestor (members)
Questions?