CSCI 383 Object-Oriented Programming & Design Lecture 19 Martin van Bommel.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Multiple Inheritance CMPS Inheritance Heart of concept of inheritance is the is-a relationship But in the real world, objects classified in multiple,
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Inheritance and Composition.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 12: Adding Functionality to Your Classes.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of CS, Nipissing University.
CSCI-383 Object-Oriented Programming & Design Lecture 19.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 12 Support for Object oriented Programming.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CSSE501 Object-Oriented Development. Chapter 13: Multiple Inheritance  In this chapter we will investigate some of the problems that can arise when a.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Overview of C++ Polymorphism
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
CSCI-383 Object-Oriented Programming & Design Lecture 21.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Week 6 Object-Oriented Programming (2): Polymorphism
Overview of C++ Overloading
Java Inheritance.
Fundaments of Game Design
Chapter 11: Inheritance and Composition
Overview of C++ Polymorphism
Java Programming Language
Final and Abstract Classes
Presentation transcript:

CSCI 383 Object-Oriented Programming & Design Lecture 19 Martin van Bommel

Fall 2010CSCI 383 Lecture 19 M. van Bommel 2 Inheritance as Classification In one way, the is-a relationship is a form of classification E.g., A TextFile is a type of File so class TextFile inherits from class File File TextFile

Fall 2010CSCI 383 Lecture 19 M. van Bommel 3 Inheritance as Classification But in the real world, most objects can be categorized in a variety of ways. A person can be a Male Professor Parent None of these are proper subsets of the other, Cannot make a single rooted inheritance hierarchy out of them

Fall 2010CSCI 383 Lecture 19 M. van Bommel 4 Inheritance as Combination Instead, real world objects are combinations of many nonoverlapping categories, each category contributing something to the result Note that we have not lost the is-a relationship; it still applies in each case MaleProfessor Parent

Fall 2010CSCI 383 Lecture 19 M. van Bommel 5 Multiple Inheritance Modeling this behavior in programs seems to call for the concept of multiple inheritance An object can have two or more different parent classes and inherit both data and behavior from each MaleProfessorParent

Fall 2010CSCI 383 Lecture 19 M. van Bommel 6 Multiple Inheritance It wouldn't be an exaggeration to say that multiple inheritance has stirred more controversy and heated debates than has any other C++ feature Yet the truth is that multiple inheritance is a powerful and effective feature - when used properly What is a good model?

Fall 2010CSCI 383 Lecture 19 M. van Bommel 7 Incomparable Complex Numbers A portion of the Smalltalk class hierarchy Where do complex numbers fit? Object BooleanMagnitudeCollection CharNumberPointSetKeyedCollectionTrueFalse IntegerFloatFraction things that can be compared to each other things that can perform arithmetic

Fall 2010CSCI 383 Lecture 19 M. van Bommel 8 Possible Solutions Make Number subclass of Magnitude, but redefine comparison operators in class Complex to give error message if used (e.g., subclassing for limitation) Don't use inheritance at all - redefine all operators in all classes (e.g., flattening the inheritance tree) Use part inheritance, but simulate others (e.g., use Number, but have each number implement all relational operators) Make Number and Magnitude independent, and have Integer inherit from both (i.e., multiple inheritance)

Fall 2010CSCI 383 Lecture 19 M. van Bommel 9 Possible Solutions MagnitudeNumber CharIntegerComplex

Fall 2010CSCI 383 Lecture 19 M. van Bommel 10 Another Example – Walking Menus A Menu is a structure charged with displaying itself when selected by the user A Menu maintains a collection of MenuItems Each MenuItem knows how to respond when selected A cascading menu is both a MenuItem and a Menu

Fall 2010CSCI 383 Lecture 19 M. van Bommel 11 Multiple Inheritance in C++ C++ supports multiple inheritance (i.e., a class may be derived from more than one base class) C++ syntax: class Derived: public Base1, public Base2, public Base3, protected Base4 {... }; Example: class passengerVehicle {... }; class trainCar {... }; class passengerCar: public passengerVehicle, public trainCar {... }; Handout #5

Fall 2010CSCI 383 Lecture 19 M. van Bommel 12 The Ambiguity Problem Circumstances: Let Derived be inherited from Base1 and Base2 All feature names inside Base1 are distinct All feature names inside Base2 are distinct All feature names inside Derived should be distinct Ambiguity problem: the same feature name X occurs both in Base1 and in Base2 The problem does not occur in single inheritance If the same feature name occurs both in Derived and in Base, then no ambiguity occurs. Why?

Fall 2010CSCI 383 Lecture 19 M. van Bommel 13 The Ambiguity Problem In handout #5, what would happen if we attempted to print the data members of derived by accessing them individually instead of using the overloaded operator<<handout #5 cout << “Object derived contains:\n” << “Integer: “ << derived.getData() << “\nCharacter: “ << derived.getData() << “\nReal number: “ << derived.getReal() << “\n\n“;

Fall 2010CSCI 383 Lecture 19 M. van Bommel 14 Coincidental vs. Inherent Ambiguity Coincidental ambiguity occurs when the duplicated names Base1:: X and Base2:: X are unrelated Ambiguity is a coincidence Same name, distinct entities Inherent ambiguity occurs when Base1 and Base2 derive from a common Base, in which X occurs Ambiguity is inherent Same name, same entity

Fall 2010CSCI 383 Lecture 19 M. van Bommel 15 Ambiguity Resolution Approaches Forbid the inheritance as given. Force the designer of Base1 and/or Base2 to resolve the ambiguity Unreasonable: Good names are rare. In many cases, multiple inheritance marries together two distinct inheritance hierarchies, sometimes supplied by different vendors Impossible: In cases of repeated inheritance Force the designer of Derived to resolve all ambiguities In Eiffel, the compiler does not allow a class in which an ambiguity exists. The designer must rename all ambiguous features In C++, a good designer will override all ambiguous features. This is not always possible since one cannot override data members

Fall 2010CSCI 383 Lecture 19 M. van Bommel 16 Ambiguity Resolution in C++ Scenario An inherited function/data member is used (inside or outside the class) The compiler checks that the name is defined in exactly one (direct/indirect) base class Error message produced if name is defined in more than one class Ambiguity should be resolved before compilation can proceed Use qualified names ( :: ) to specify exactly one derived member

Fall 2010CSCI 383 Lecture 19 M. van Bommel 17 Ambiguity Resolution in C++ In handout #5, if we wanted to print the data members of derived by accessing them individually instead of using the overloaded operator<<, we could use the scope resolution operator ( :: ) to resolve the ambiguityhandout #5 cout << “Object derived contains:\n” << “Integer: “ << derived.Base1::getData() << “\nCharacter: “ << derived.Base2::getData() << “\nReal number: “ << derived.getReal() << “\n\n“; Drawbacks Client should be aware of implementation details Late detection of errors

One Solution: Redefinition/Renaming By redefinition we mean a change in the operation of a command such as what happens when a virtual method is overridden in a subclass By renaming we simply mean changing the name by which a method is invoked without altering the command’s functionality class Derived:public Base1, public Base2 { public:... int getData() const {return Base1::getData();} char getCharData() const {return Base2::getData();} double getReal() const {return real;}... };

Problem with Redefinition Solution While the redefinition of the function getData solves the problem when a Derived object is used in isolation, further problems may arise when we consider the implications of the principle of substitution Suppose getData is defined as a virtual function in Base1, Base2, and Derived Base2* b = new Derived(7,’A’,3.5); cout getData(); // this may not be what was intended

Diamond of Death Another common problem occurs when parent classes have a common root ancestor Occurs in every large system Must occur if the inheritance hierarchy has a common root Does the new object have one or two instances of the ancestor? Think of an example in which we would like to keep two instances A BC D

Diamond of Death Classical example: In C++ standard library, multiple inheritance is used to form the class iostream Handout #6 ios istream ostream iostreamifstreamofstream fstream

Approaches to Repeated Inheritance Forbid the situation A diamond occurs naturally in many cases Demand complicates the design Single copy of common base class Not always what we want Multiple copies of common base class Not always what we want

Approaches to Repeated Inheritance Either single or multiple copies of common base class Eiffel’s approach: decision made by designer of D C++’s approach: decision made by designer B and C A BC D

Approaches to Repeated Inheritance In C++ this problem is overcome through the use of the virtual modifier in the parent class list The virtual keyword indicates that the superclass may appear more than once in descendant classes of the current class but that only one copy of the superclass should be included class ios {... }; class istream: virtual public ios {... }; class ostream: virtual public ios {... }; class iostream: public istream, public ostream {... };

Planning for the Diamond Problem class Person {... void print(ostream& out){ // Print person data (e.g., name, gender, etc) } }; class Student: virtual public Person {... void print(ostream& out){ Person::print(out); // Print student data (e.g., courses, year, etc) } };

Planning for the Diamond Problem class Teacher: virtual public Person {... void print(ostream& out) { Person::print(out) // Print teacher data (e.g., salary, courses, etc) }; class TeachingAssistant: public Student, public Teacher {... void print(ostream& out) { Student::print(out); Teacher::print(out); // Print assistant data (e.g., supervisor, etc) }; Do you see any problem? Fix this problem (DONE IN CLASS)

Cross Delegation Handout #7 Notice how a class that DerivedOne knows nothing about will supply the override of a virtual function invoked by DerivedOne::foo() This “cross delegation” can be a powerful technique for customizing the behavior of polymorphic classes

Constructors and Virtual Base Class? class V { public: V(const char* s){ cout << s;} }; class B1: virtual public V{ public: B1(const char* s):V(“B1”){ cout << s;} }; class B2: virtual public V{ public: B2(const char* s):V(“B2”){ cout << s;} }; class D: public B1, public B2 { public: D(void):B1(“DB1”),B2(“DB2”) {} }; What will be printed when instantiating object of class D ?

Construction of a Virtual Base Class Work around 1 Define a default constructor in V Work around 2 Call V ’s constructor directly from the constructor of D Virtual bases are initialized by the initializer of the most derived class, other initializers are ignored Virtual bases may be initialized in a constructor of a derived class even if they are not immediate base classes Practically speaking, this means that when you create a class that has a virtual base class, you must be prepared to pass whatever parameters are required to call the virtual base class's constructor This might mean that the most-derived class's constructor needs more parameters than you might otherwise think