CS 3370.  C++ allows multiple implementation inheritance  Handy for multiple “is-a” situations  Handy for reusing implementation without “is-a”  Leads.

Slides:



Advertisements
Similar presentations
How C++ Compilers Implement Object Orientation Eric Powders (ejp2127)
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
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.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Computer Science I Inheritance Professor Evan Korth New York University.
1 CSE 303 Lecture 24 Inheritance in C++, continued slides created by Marty Stepp
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
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.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
JAVA Introduction ● One of the main JAVA design goal is reducing complexity for programmer – Development time is half or less comparing to equivalent C++
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.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
Inheritance in the Java programming language J. W. Rider.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object Oriented Software Development
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
Object Oriented Programming
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
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.
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
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.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
The Slicing Problem CS240 Computer Science II. Some relationship between base and derived classes: data members Derived class inherits data members of.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Classes and Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
“Under the Hood” of Polymorphism
Advanced Programming in Java
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Programming with ANSI C ++
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Polymorphism Reference and pointer implicit type casting
C++ Object Oriented 1.
Presentation transcript:

CS 3370

 C++ allows multiple implementation inheritance  Handy for multiple “is-a” situations  Handy for reusing implementation without “is-a”  Leads to C++’s Darkest Corner (IMHO)  Inheritance “hierarchy” becomes a DAG  Ambiguities galore!  Complicated rules

 Invented with Common Lisp (“Flavors”)  Clients inherit implementation to gain a capability  Printable, Storable, etc.  Mixin classes are often abstract  Provides most but not all of feature  You override virtual function(s) to complete the feature  A Mixin that itself happens to derive from another class should do so via virtual inheritance (described later)  See AbleTest.cpp

 Suppose we have the proverbial Shape hierarchy  Circle, Triangle, etc.  If there is common code to all shapes, you can put it in Shape  If it is common to only some, keep it a secret  Don’t have it derive from Shape  See next slide…

class Shape {…}; class Common {…}; class Logo : public Shape {…}; class Circle : public Shape, protected Common {…}; class Triangle: public Shape, protected Common {…};

 A derived object has a subobject for each base class  data duplication!  Name clashes possible  Possible duplicate data via “diamond” inheritance  Examples: upcast.cpp, delta.cpp, ambiguous.cpp

 The derived class holds a pointer to the base subobject  It is not inherited by value  Only one subobject exists in the complete object  Therefore, there is no ambiguity in casting to the top-level class

 There must be no ambiguity in name lookup  If multiple base classes contain the same member names, you have a problem  If a member function, you can override it and do the Right Thing  If a data member, you must use “::”  And they must still be accessible  Examples: ambiguous2.cpp, disambiguate.cpp

 A fancy name for the way virtual functions work  The “most-derived” binding applies  B::f dominates A::f if A is a (direct or indirect) base class of B  Non-virtual functions work the same way for consistency

What if a D object calls f( ) via a pointer?

 How do you initialize a virtual base?  With diamond inheritance, there are multiple paths to the shared base  Which intermediate class is responsible?

 None!  There’s no criterion for choosing  It is always initialized, therefore, by the most derived class  Other initializations are ignored, but must be supplied by every derived concrete class  This makes for weird, but necessary code  See next slides

 All virtual bases are initialized before non- virtual bases  No matter where they are  In top-down-left-to-right order  Subobjects are never initialized twice  So the compiler has a lot of work to do to keep track of things

What is the order of initialization for G g; ? (See virtinit2.cpp)

 Affects copy constructors and assignment operators  Copy constructors work okay  Most-derived class must still take care of virtual base, similar to initialization example  Assignment operators are different!  They are not inherited  You must control all sub-assignments explicitly!  Examples: assignment.cpp, virtassign*.cpp