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

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Lecture 5: Interfaces.
Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Object Oriented Programming Elhanan Borenstein Lecture #8 copyrights © Elhanan Borenstein.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Inheritance and Polymorphism CS351 – Programming Paradigms.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Object Oriented Programming Elhanan Borenstein Lecture #6.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Object Oriented Programming Elhanan Borenstein Lecture #4.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Object Oriented Programming Elhanan Borenstein Lecture #5.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Fall 2013CISC2200 Yanjun Li1 Review. Fall 2013CISC2200 Yanjun Li2 Outline Array Pointer Object-Oriented Programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Overview of C++ Polymorphism
Week 12 Methods for passing actual parameters to formal parameters.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming Elhanan Borenstein Lecture #7.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Learners Support Publications Constructors and Destructors.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Constructors and Destructors
Classes (Part 1) Lecture 3
Object-Oriented Programming: Inheritance
Inheritance: hiding methods
Object Oriented Programming
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
6 Chapter Functions.
Constructors and Destructors
Overview of C++ Polymorphism
Computer Science II for Majors
Presentation transcript:

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

Agenda Inheritance Permissions Multiple Inheritance Common Ancestor copyrights © Elhanan Borenstein

Inheritance Permissions copyrights © Elhanan Borenstein

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

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

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

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

Multiple Inheritance copyrights © Elhanan Borenstein

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

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

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

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

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

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

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

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;

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(); } };

Questions? copyrights © Elhanan Borenstein