Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Advertisements

Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Object–Orientated Design. OOP Design Describe the following: a computer a university Usually the most natural way to describe a task is to list the entities.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Multiple Inheritance Fall 2005 OOPD John Anthony.
©Silberschatz, Korth and Sudarshan8.1Database System Concepts Chapter 8: Object-Oriented Databases Need for Complex Data Types The Object-Oriented Data.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Pointers Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Inheritance and Polymorphism CS351 – Programming Paradigms.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 9 Polymorphism Richard Gesick.
Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1.
Lecture 10 Inheritance “Absolute C++” Chapter 14.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object Oriented Software Development
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
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.
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
COMP Inheritance Basics Yi Hong June 09, 2015.
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.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Functions Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Object-Oriented Programming: Inheritance
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
CS1201: Programming Language 2
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
CISC/CMPE320 - Prof. McLeod
CPS120: Introduction to Computer Science
Fundaments of Game Design
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
CS 144 Advanced C++ Programming March 28 Class Meeting
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.
SPL – PS4 C++ Advanced OOP.
Presentation transcript:

Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006

class Student { private: std::string id; public: std::string get_id(); }; class Employee { private: std::string id; public: std::string get_id(); };

Example:Syntax class TeachingAssistant:public Student, public Employee { … } Teaching Assistant maintains the attributes and the methods associated with both base classes Student, and Employee

Polymorphic Assignment TeachingAssistant * Mark = new TeachingAssistant; Employee * E = Mark; //Legal as a Teaching Assistant is-an Employee Student * S = Mark; //Legal as a Teaching Assistant is-a Student

Common Misuse of Multiple Inheritance A common error is to use Multiple Inheritance as composition rather than specialisation (is-a): E.G. The following is incorrect: class car: public Engine, public Transmission, public Wheels { … }

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 C++ compiler cannot determine which version to use in the TeachingAssistant class: the get_id() in the Employee class or the get_id () in the Student class.

class TeachingAssistant { }; TeachingAssistant * Mark; Mark->get_id(); /* Which class? */

Problems with Multiple Inheritance: Name Ambiguity Solution 1: Use a fully qualified function name: TeachingAssistant * Mark = new TeachingAssistant; cout << “ The TeachingAssistant is” << Mark -> Employee::get_id() << “\n”;

Problems with Multiple Inheritance: Name Ambiguity Solution 2: Redefine the ambiguous function in the new class and hide the qualified name in a method body: class TeachingAssistant:public Student, public Employee { public: string get_id(); public: string student_id(); }

string TeachingAssistant::get_id() { return Employee::get_id(); } string TeachingAssistant::student_id() { return Student::get_id(); }

Problems with Multiple Inheritance:  Replicated Base Classes: Base classes might be inherited more than once (indirectly) due to a class inheritance hierarchy causing duplicated attributes.  In our example suppose the following holds: class Employee: public Person {..} class Student: public Person {..} class TeachingAssistant:public Student, public Employee {..}

Problems with Multiple Inheritance: Replicated Base Classes: Attributes in the Person class get inherited twice! Eg. A teaching Assistant will have two names. 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 {..}

TeachingAssistant * Mark; = new TeachingAssistant; Student * s = Mark; // Legal due to is-a relationship Person * p = s; // Legal due to is-a relationship Employee * e = Mark; // Legal due to is-a relationship Person * p1 = e; // Legal due to is-a relationship Person * p2 = Mark; // Legal only if Virtual Inheritance is used so that the compiler knows which version of person to use, error otherwise