 2003 Prentice Hall, Inc. All rights reserved. 1 22.9 Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 21 - Standard C++ Language Additions Outline 21.1Introduction 21.2 bool Data Type 21.3 static_cast.
Slide 1 Inheritance: Part I. Slide 2 Structuring ‘classes’ class Employee { string firstName; string familyName; … } class Manager { Employee e; list.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 - Inheritance Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes 19.3Protected.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
Inheritance, Polymorphism, and Virtual Functions
 2006 Pearson Education, Inc. All rights reserved Other Topics.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 12: Adding Functionality to Your Classes.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Object-Oriented Programming: Polymorphism Zhen Jiang West Chester University
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 19 - C++ Inheritance Outline 19.1Introduction.
Inheritance One of the most powerful features of C++
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
CSCI 383 Object-Oriented Programming & Design Lecture 19 Martin van Bommel.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
 2006 Pearson Education, Inc. All rights reserved Templates.
1 Inheritance inheritance –a mechanism that build a new class by deriving from an existing class –derived class (or subclass) inherit from based class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
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.
Chapter 22 - C++ Templates
Chapter 19 - C++ Inheritance
Object-Oriented Design (OOD) and C++
Inheritance, Polymorphism, and Virtual Functions
Inheritance: Part I.
Chapter 22 - Other Topics Outline 22.1 Introduction
Inheritance, Polymorphism, and Virtual Functions
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 20- Virtual Functions and Polymorphism
Chapter 19 - Inheritance Outline 19.1 Introduction
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Capitolo 9 - Inheritance
Chapter 19 - Inheritance Outline 19.1 Introduction
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but can cause ambiguity problems If both base classes have functions of the same name Solution: specify exact function using :: –myObject.BaseClass1::function() –Format Use comma-separated list class Derived : public Base1, public Base2{ contents }

 2003 Prentice Hall, Inc. All rights reserved. Outline 2 base1.h (1 of 1) 1 // Fig : base1.h 2 // Definition of class Base1 3 #ifndef BASE1_H 4 #define BASE1_H 5 6 // class Base1 definition 7 class Base1 { 8 public: 9 Base1( int parameterValue ) { value = parameterValue; } 10 int getData() const { return value; } protected: // accessible to derived classes 13 int value; // inherited by derived class }; // end class Base #endif // BASE1_H There are two base classes in this example, each has its own getData function. This base class contains an int.

 2003 Prentice Hall, Inc. All rights reserved. Outline 3 base2.h (1 of 1) 1 // Fig : base2.h 2 // Definition of class Base2 3 #ifndef BASE2_H 4 #define BASE2_H 5 6 // class Base2 definition 7 class Base2 { 8 public: 9 Base2( char characterData ) { letter = characterData; } 10 char getData() const { return letter; } protected: // accessible to derived classes 13 char letter; // inherited by derived class }; // end class Base #endif // BASE2_H

 2003 Prentice Hall, Inc. All rights reserved. Outline 4 derived.h (1 of 1) 1 // Fig : derived.h 2 // Definition of class Derived which inherits 3 // multiple base classes (Base1 and Base2). 4 #ifndef DERIVED_H 5 #define DERIVED_H 6 7 #include 8 9 using std::ostream; #include "base1.h" 12 #include "base2.h" // class Derived definition 15 class Derived : public Base1, public Base2 { 16 friend ostream &operator<<( ostream &, const Derived & ); public: 19 Derived( int, char, double ); 20 double getReal() const; private: 23 double real; // derived class's private data }; // end class Derived #endif // DERIVED_H Use comma-separated list.

 2003 Prentice Hall, Inc. All rights reserved. Outline 5 derived.cpp (1 of 1) 1 // Fig : derived.cpp 2 // Member function definitions for class Derived 3 #include "derived.h" 4 5 // constructor for Derived calls constructors for 6 // class Base1 and class Base2. 7 // use member initializers to call base-class constructors 8 Derived::Derived( int integer, char character, double double1 ) 9 : Base1( integer ), Base2( character ), real( double1 ) { } // return real 12 double Derived::getReal() const { return real; } // display all data members of Derived 15 ostream &operator<<( ostream &output, const Derived &derived ) 16 { 17 output << " Integer: " << derived.value 18 << "\n Character: " << derived.letter 19 << "\nReal number: " << derived.real; return output; // enables cascaded calls } // end operator<< Note use of base-class constructors in derived class constructor.

 2003 Prentice Hall, Inc. All rights reserved. Outline 6 fig22_18.cpp (1 of 2) 1 // Fig : fig22_18.cpp 2 // Driver for multiple inheritance example. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include "base1.h" 9 #include "base2.h" 10 #include "derived.h" int main() 13 { 14 Base1 base1( 10 ), *base1Ptr = 0; // create Base1 object 15 Base2 base2( 'Z' ), *base2Ptr = 0; // create Base2 object 16 Derived derived( 7, 'A', 3.5 ); // create Derived object // print data members of base-class objects 19 cout << "Object base1 contains integer " 20 << base1.getData() 21 << "\nObject base2 contains character " 22 << base2.getData() 23 << "\nObject derived contains:\n" << derived << "\n\n"; 24

 2003 Prentice Hall, Inc. All rights reserved. Outline 7 fig22_18.cpp (2 of 2) 25 // print data members of derived-class object 26 // scope resolution operator resolves getData ambiguity 27 cout << "Data members of Derived can be" 28 << " accessed individually:" 29 << "\n Integer: " << derived.Base1::getData() 30 << "\n Character: " << derived.Base2::getData() 31 << "\nReal number: " << derived.getReal() << "\n\n"; cout << "Derived can be treated as an " 34 << "object of either base class:\n"; // treat Derived as a Base1 object 37 base1Ptr = &derived; 38 cout getData() yields " 39 getData() << '\n'; // treat Derived as a Base2 object 42 base2Ptr = &derived; 43 cout getData() yields " 44 getData() << endl; return 0; } // end main Note calls to specific base class functions. Can treat derived-class pointer as either base-class pointer.

 2003 Prentice Hall, Inc. All rights reserved. Outline 8 fig22_18.cpp output (1 of 1) Object base1 contains integer 10 Object base2 contains character Z Object derived contains: Integer: 7 Character: A Real number: 3.5 Data members of Derived can be accessed individually: Integer: 7 Character: A Real number: 3.5 Derived can be treated as an object of either base class: base1Ptr->getData() yields 7 base2Ptr->getData() yields A

 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance and virtual Base Classes Ambiguities from multiple inheritance iostream could have duplicate subobjects –Data from ios inherited into ostream and istream –Upcasting iostream pointer to ios object is a problem Two ios subobjects could exist, which is used? –Ambiguous, results in syntax error iostream does not actually have this problem ios ostream istream iostream

 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance and virtual Base Classes Solution: use virtual base class inheritance –Only one subobject inherited into multiply derived class Second Derived Class Base Class First Derived Class Multiply-Derived Class virtual inheritance

 2003 Prentice Hall, Inc. All rights reserved. Outline 11 fig22_20.cpp (1 of 3) 1 // Fig : fig22_20.cpp 2 // Attempting to polymorphically call a function that is 3 // multiply inherited from two base classes. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 // class Base definition 10 class Base { 11 public: 12 virtual void print() const = 0; // pure virtual }; // end class Base // class DerivedOne definition 17 class DerivedOne : public Base { 18 public: // override print function 21 void print() const { cout << "DerivedOne\n"; } }; // end class DerivedOne 24 This example will demonstrate the ambiguity of multiple inheritance.

 2003 Prentice Hall, Inc. All rights reserved. Outline 12 fig22_20.cpp (2 of 3) 25 // class DerivedTwo definition 26 class DerivedTwo : public Base { 27 public: // override print function 30 void print() const { cout << "DerivedTwo\n"; } }; // end class DerivedTwo // class Multiple definition 35 class Multiple : public DerivedOne, public DerivedTwo { 36 public: // qualify which version of function print 39 void print() const { DerivedTwo::print(); } }; // end class Multiple 42

 2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig22_20.cpp (3 of 3) 43 int main() 44 { 45 Multiple both; // instantiate Multiple object 46 DerivedOne one; // instantiate DerivedOne object 47 DerivedTwo two; // instantiate DerivedTwo object // create array of base-class pointers 50 Base *array[ 3 ]; array[ 0 ] = &both; // ERROR--ambiguous 53 array[ 1 ] = &one; 54 array[ 2 ] = &two; // polymorphically invoke print 57 for ( int i = 0; i < 3; i++ ) 58 array[ i ] -> print(); return 0; } // end main Which base subobject will be used?

 2003 Prentice Hall, Inc. All rights reserved. Outline 14 fig22_20.cpp output (1 of 1) c:\cpp4e\ch22\fig22_20_21\fig22_20.cpp(52) : error C2594: '=' : ambiguous conversions from 'class Multiple *' to 'class Base *' Error executing cl.exe. test.exe - 1 error(s), 0 warning(s)

 2003 Prentice Hall, Inc. All rights reserved. Outline 15 fig22_21.cpp (1 of 3) 1 // Fig : fig22_21.cpp 2 // Using virtual base classes. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 // class Base definition 9 class Base { 10 public: // implicit default constructor virtual void print() const = 0; // pure virtual }; // end Base class // class DerivedOne definition 19 class DerivedOne : virtual public Base { 20 public: // implicit default constructor calls 23 // Base default constructor // override print function 26 void print() const { cout << "DerivedOne\n"; } }; // end DerivedOne class Use virtual inheritance to solve the ambiguity problem. The compiler generates default constructors, which greatly simplifies the hierarchy.

 2003 Prentice Hall, Inc. All rights reserved. Outline 16 fig22_21.cpp (2 of 3) // class DerivedTwo definition 31 class DerivedTwo : virtual public Base { 32 public: // implicit default constructor calls 35 // Base default constructor // override print function 38 void print() const { cout << "DerivedTwo\n"; } }; // end DerivedTwo class // class Multiple definition 43 class Multiple : public DerivedOne, public DerivedTwo { 44 public: // implicit default constructor calls 47 // DerivedOne and DerivedTwo default constructors // qualify which version of function print 50 void print() const { DerivedTwo::print(); } }; // end Multiple class Use virtual inheritance, as before.

 2003 Prentice Hall, Inc. All rights reserved. Outline 17 fig22_21.cpp (3 of 3) int main() 55 { 56 Multiple both; // instantiate Multiple object 57 DerivedOne one; // instantiate DerivedOne object 58 DerivedTwo two; // instantiate DerivedTwo object // declare array of base-class pointers and initialize 61 // each element to a derived-class type 62 Base *array[ 3 ]; array[ 0 ] = &both; 65 array[ 1 ] = &one; 66 array[ 2 ] = &two; // polymorphically invoke function print 69 for ( int i = 0; i < 3; i++ ) 70 array[ i ]->print(); return 0; } // end main

 2003 Prentice Hall, Inc. All rights reserved. Outline 18 fig22_21.cpp output (1 of 1) DerivedTwo DerivedOne DerivedTwo