© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.

Slides:



Advertisements
Similar presentations
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
© Copyright Eliyahu Brutman Programming Techniques Course.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
OOP Languages: Java vs C++
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Chapter 12: Adding Functionality to Your Classes.
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.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Lecture 10 Inheritance “Absolute C++” Chapter 14.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CS  C++ allows multiple implementation inheritance  Handy for multiple “is-a” situations  Handy for reusing implementation without “is-a”  Leads.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
1 Lecture OOP Course Inheritance Basics Shape.
Domain Modeling Part2: Domain Class Diagram Chapter 4 pp part 2 1.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object Oriented Software Development
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Programming in C++ Michal Brabec Petr Malý. Inheritance class derived-class: access-specifier base-class {} Base & Derived class Implementation inheritance.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Object-Oriented Programming: Inheritance
Object-Oriented Programming (OOP) Lecture No. 45
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Programming with ANSI C ++
CISC/CMPE320 - Prof. McLeod
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
COP 3330 Object-oriented Programming in C++
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0

© Copyright Eliyahu Brutman Chapter 9 – Multiple Inheritance Version 1.0

© Copyright Eliyahu Brutman Multiple Inheritance - 3 Lets recall What we have seen so far Polymorphic inheritance  For defining generic interface  Accessing the concrete implementations via this interface  The compiler implements code, which extracts during Run-Time the needed calling address  Through a virtual pointer Which usually point at the actual object  And through a virtual table Which holds the addresses of the virtual functions

© Copyright Eliyahu Brutman Multiple Inheritance - 4 Construction and Destruction Lets recall the construction order and functionality What is the order of “ things ” Lets see what happens during destruction What is the order of “ things ” Can a destructor be virtual? Does it need to be virtual? When? Class Employee Class SalesPersonClass ExecutiveClass Administrative m_salary v. CalcSalary() m_salesm_performance

© Copyright Eliyahu Brutman Multiple Inheritance - 5 Multiple Inheritance Sometimes it is desirable for a derived class to inherit features from more than one base class. How is this represented in memory? What is the layout of the instance? Can you think of problems using this? Class Employee Class SalesPersonClass ExecutiveClass Administrative m_salary v. CalcSalary() m_salesm_performance Class Student v. M() Class Metargel v. CalcSalary() v. M() Class Person

© Copyright Eliyahu Brutman Multiple Inheritance - 6 Syntax class Metargel : public Student, public Employee { … } Teaching Assistant maintains the attributes and the methods associated with both base classes Student, and Employee

© Copyright Eliyahu Brutman Multiple Inheritance - 7 Polymorphic Assignment Metargel * myMetargel = new Metargel; Employee * E = myMetargel; //Legal as a Teaching Assistant is-an Employee Student * S = myMetargel; //Legal as a Teaching Assistant is-a Student

© Copyright Eliyahu Brutman Multiple Inheritance - 8 Problems with Multiple Inheritance Name Ambiguity: Similar names can be used for different operations in the base classes  e.g. an employee might have an id_number and a student might also have an id_number field. Both base classes might have a similar get_id() method. The compiler cannot determine which version to use in the Metargel class: the get_id() in the Employee class or the get_id () in the Student class. A common misuse of multiple inheritance is using it as composition rather than specialization (is-a): The following is incorrect: class car: public Engine, public Transmission, public Wheels

© Copyright Eliyahu Brutman Multiple Inheritance - 9 Name Ambiguity Solution 1: Use a fully qualified function name: Metargel * myMetargel = new Metargel; cout << “ The TeachingAssistant is” << myMetargel -> Employee::get_id() << “\n”;

© Copyright Eliyahu Brutman Multiple Inheritance - 10 Name Ambiguity Solution 2: Redefine the ambiguous function in the new class and hide the qualified name in a method body: class Metargel : public Student, public Employee { public: string get_id(); public: string student_id(); } string Metargel ::get_id() { return Employee::get_id(); } string Metargel ::student_id() { return Student::get_id(); }

© Copyright Eliyahu Brutman Multiple Inheritance - 11 Virtual Inheritance - Motivation What if Class student and class Employee both inherit from Person? How can this be dealt with ? Class Employee Class SalesPersonClass ExecutiveClass Administrative m_salary v. CalcSalary() m_salesm_performance Class Student v. M() Class Metargel v. CalcSalary() v. M() Class Person

© Copyright Eliyahu Brutman Multiple Inheritance - 12 Replicated Base Classes The same class can not be directly inherited more than once Base classes might be inherited indirectly more than once due to a class inheritance hierarchy. In our example suppose the following: class Employee : public Person {..} class Student : public Person {..} class Metargel : public Student, public Employee {..} Attributes from the Person class get inherited twice! myMetargel will have two names

© Copyright Eliyahu Brutman Multiple Inheritance - 13 Replicated Inheritance Recall that replicated inheritance does not share the same common grandparent even if they both derive from the same grandparent! If we have code like this in C++: class A { … }; class B : public A { … }; class C : public A { … };/* Derives from A but it is not shared! */ class D : public B, public C { … }; This gives us the following situation: A BC A D A* a; B* b; C* c; D* d; b = d; // ok c = d; // ok a = b; // ok a = c; // ok a = d; //error ambiguous

© Copyright Eliyahu Brutman Multiple Inheritance - 14 Virtual Inheritance Introducing Virtual Inheritance Single instance within layout. Class Employee Class SalesPersonClass ExecutiveClass Administrative m_salary v. CalcSalary() m_salesm_performance Class Student v. M() Class Metargel v. CalcSalary() v. M() Class Person

© Copyright Eliyahu Brutman Multiple Inheritance - 15 Virtual Base Classes To merge any common base classes into one single copy the inheritance should be written as virtual: class Employee: virtual public Person {..} class Student: virtual public Person {..} class TeachingAssistant : public Student, public Employee {..}

© Copyright Eliyahu Brutman Multiple Inheritance - 16 Virtual Inheritance Standard base classes D members appear twice in C Virtual base classes class A : public virtual D { … } Avoid duplication of base class members Require additional pointers so that D part of A, B parts of object can be shared C AB D A part D part C part B part

© Copyright Eliyahu Brutman Multiple Inheritance - 17 Virtual Base Classes Metargel * myMetargel = new Metargel; Student * s = myMetargel; // Legal due to is-a relationship Person * p = s; // Legal due to is-a relationship Employee * e = myMetargel;// Legal due to is-a relationship Person * p1 = e; // Legal due to is-a relationship Person * p2 = myMetargel; // Legal only if Virtual Inheritance is used so // that the compiler knows which version of //person to use, error otherwise

© Copyright Eliyahu Brutman Multiple Inheritance - 18 Multiple Inheritance - General It is recommended to keep multiple inheritance to minimal usage Due to complexity, which may arise Due to misusage You may use it, but please use it with care Recommended to use as many as possible abstract classes, and not multiple inheritance Make sure this really models your problem domain well