Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming Elhanan Borenstein Lecture #9 copyrights © Elhanan Borenstein.

Similar presentations


Presentation on theme: "Object Oriented Programming Elhanan Borenstein Lecture #9 copyrights © Elhanan Borenstein."— Presentation transcript:

1 Object Oriented Programming Elhanan Borenstein borens@tau.ac.il Lecture #9 copyrights © Elhanan Borenstein

2 Agenda Inheritance Permissions Multiple Inheritance Common Ancestor copyrights © Elhanan Borenstein

3 Inheritance Permissions copyrights © Elhanan Borenstein

4 Inheritance Permission As we recall  A public member in the base class can be accessed by both the derived class and all other (main)  A private member in the base class cannot be accessed by neither the derived class or all other (main) There are obviously cases where we wish to deny access from all other classes (and main), but to allow the derived classes to access this member (“part of the family”) The protected permission does just that !!! The Protect Permission (for class members) copyrights © Elhanan Borenstein BaseDerivedOther privateVXX protectedVVX publicVVV

5 Inheritance Permission So far all we learned applies to public inheritance: class derived1: public base Using public inheritance means we do not wish to change the permission of the inherited members:  public members that were inherited are still accessible to everybody  protected members that were inherited are still only accessible to derived classes 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. Using Permissions in Inheritance copyrights © Elhanan Borenstein

6 Inheritance Permission Using Permissions in Inheritance - Summary copyrights © Elhanan Borenstein BaseDerived1 : ____ BaseDerived2 : public Derived1Derived1 in Main PubPrtPrvPubPrtPrvPubPrtPrv PrivateXXXXXXXXXX ProtectedVPrt PrvPrt XXXX PublicVPubPrtPrvPubPrtXVXX

7 Inheritance Permission Permissions in Inheritance – Con’t copyrights © Elhanan Borenstein The final permission will always be the more restricting permission (WHY?). Can be used for “hiding”. An important use for private/protected inheritance is for scenarios where the base class does not implemented enough protection for its members and we wish to fix it. We can restore the original (or an intermediate) permission level (or further restrict the permission) for specific data members by declaring them again (using their full name) under the appropriate permission. Example: prvprt.cpp

8 Multiple Inheritance copyrights © Elhanan Borenstein

9 Multiple Inheritance 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.) Introduction copyrights © Elhanan Borenstein Base1 Base2 Derived class Derived : public Base1, public base2

10 Multiple Inheritance The same principles of single inheritance, still apply in multiple inheritance:  In case the base class (classes) c’tors 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 Guidelines copyrights © Elhanan Borenstein

11 Multiple Inheritance 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?) Common Ancestor copyrights © Elhanan Borenstein Base1 Base2 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. Ancestor Base1 Base2 Derived Ancestor a note about drawings

12 Common Ancestor Why do we actually ends up with duplicated copies? Does it make sense? copyrights © Elhanan Borenstein Base1 Base2 Derived Ancestor Ancestor A; A: Base1 B1; Base2 B2; B1:B2: Derived D; D: Ancestor (members) Ancestor (members) Base1 (additional) Ancestor (members) Base2 (additional) Ancestor (members) Base1 (additional) Ancestor (members) Base2 (additional) Derived (additional) Base1 Base2

13 Common Ancestor A common ancestor inheritance of that sort, is relevant only when the derived entity should, by definition, get duplicated copies of the ancestors members.  (Logical argument) Using members which were inherited twice takes place using their full name:  base2::member  or … base1::member Example: bat What is it good for? copyrights © Elhanan Borenstein NONEAdditional members in Batmobile: int m_missilesAdditional members inherited from Airplane: bool m_automaticAdditional members inherited from Car: int m_speed bool m_wings Members inherited from Vehicle (twice): x2

14 Common Ancestor – Virtual Inheritance In the previous example, all members of the common ancestors appeared twice in the derived class. In most cases, however, this is not the desired behavior (again – think of the logical argument). Examples !!! If we wish that only one copy of the ancestor members will be inherited we should use: Virtual Inheritance!!! Virtual Inheritance instructs the compiler to use the same location in memory for both copies of ancestor’s members.  Each base class will include a pointer to that location.  The location (and access) of these members is transparent to the programmer.  Only one copy of these member exists. The last assignment (regardless of which base class was used) will determine its value. Virtual Inheritance copyrights © Elhanan Borenstein

15 Common Ancestor copyrights © Elhanan Borenstein Ancestor A; A: Base1 B1; Base2 B2; B1: B2: Derived D; D: Ancestor (members) Ancestor (members) Base1 (additional) Ancestor (members) Base2 (additional) Ancestor (members) Base1 (additional) Ancestor (members) Base2 (additional) Derived (additional) Base1 Base2 Ancestor A; A: Base1 B1; Base2 B2; B1: B2: Derived D; D: Ancestor (members) Ancestor (members) Ancestor (members) Base2 (additional) near ptr Base1 (additional) near ptr Base2 (additional) Derived (additional) near ptr Base1 (additional) Ancestor (members) Simple Inheritance Virtual Inheritance

16 Common Ancestor – Virtual Inheritance Example: vir_inh.cpp Using Virtual common ancestor is relevant when the ancestor’s member actually represents the same entity (and should thus appear only once.  (Logical argument) Example: crisis Note:  We use the initialization line of ManagerSecretary to send the c’tor parameters of Employee. (WHY?)  When using virtual inheritance, the common base class must implement a virtual d’tor (even if it is empty). Otherwise, we may crash (WHY?) Virtual Inheritance copyrights © Elhanan Borenstein Employee* pe = new Manager() delete pe;

17 Common Ancestor – Virtual Inheritance One more thing to think about… copyrights © Elhanan Borenstein class Employee {... void Print() { cout<<"Name:"<<Name<<endl; cout<<"Salary:"<<Salary<<endl; } }; class Manager : virtual public Employee {... void Print() { Employee::Print(); cout<<"Level:"<<Level<<endl; } }; … class Secretary : virtual public Employee {... void Print() { Employee::Print(); cout<<"Words per min:"<<WordsPerMin<<endl; } }; class ManagerSecretary : public Manager, public Secretary {... void Print() { Manager::Print(); Secretary::Print(); } };

18 Questions? copyrights © Elhanan Borenstein


Download ppt "Object Oriented Programming Elhanan Borenstein Lecture #9 copyrights © Elhanan Borenstein."

Similar presentations


Ads by Google