Inheritance -II.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
OBJECT ORIENTED FEATURES AND IMPLEMENTATION OF UNIVERSAL QUANTIFICATION IN C++ Karoly Bosa RISC SS2000.
I NHERITANCE Chapter 7 Department of CSE, BUET 1.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
Chapter -6 Polymorphism
Inheritance and Composition Reusing the code and functionality Unit - 04.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
Overview of C++ Polymorphism
1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
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 2 Objects and Classes
Hank Childs, University of Oregon
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Templates.
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Inheritance: hiding methods
Inheritance & Polymorphism
Constructors & Destructors.
Inheritance II CMSC 202.
UML Class Diagram: class Rectangle
PRG 218 Week 5 Individual Assignment Coding: Derived Classes Please click here to buy ( (
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Ambiguity Resolution in Inheritance
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Inheritance, Polymorphism, and Virtual Functions
Constructors and destructors
Constructors and Destructors
By Muhammad Waris Zargar
Inheritance: Polymorphism and Virtual Functions
Multiple Base Classes Inheritance
Inheritance:Concept of Re-usability
Overview of C++ Polymorphism
Inheritance -I.
Inheritance: Polymorphism and Virtual Functions
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
C++ Polymorphism Reference and pointer implicit type casting
Inheritance in C++ Inheritance Protected Section
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

Inheritance -II

Contents Multiple Inheritance 1 Virtual Inheritance 2 Case Study 3

Multiple Inheritance(多继承) Bed +Weigh: int +sleep():void Sofa +watchTV():void SleeperSofa +foldOut():void A class can have more than one direct base class. The use of more than immediate base class is usually called multiple inheritance.(有多个基类)

Multiple Inheritance Bed Sofa SleeperSofa +Weigh: int +sleep():void Sofa +watchTV():void SleeperSofa + foldOut():void class SleeperSofa: public Sofa, public Bed{ //… };

Multiple Inheritance class Bed{ public: Bed(){} void sleep() { cout << "Sleep..\n"; } int weight; }; class Sofa{ public: Sofa(){} void watchTV() { cout << "Watching TV..\n"; } int weight; }; class SleeperSofa : public Bed, public Sofa{ public: SleeperSofa(){} void foldOut() { cout << "Fold out..\n"; } }; int main() { SleeperSofa ss; ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } Derived constructor without parameters

Multiple Inheritance Constructors and destructor of multiple inheritance Constructor definition Derived class’s constructor : base_class1’s constructor, base_class2’s constructor, base_class3’s constructor {} Constructing objects’ sequence: base_class1 object ->base_class2 object-> base_class3 object -> Derived class object Destructor definition Derived class’s destructor {} Destroying objects’ sequence: Derived class object -> base_class3 object ->base_class2 object -> base_class1 object

Multiple Inheritance class Bed{ public: Bed(int w):weight(w){} void sleep() { cout << "Sleep..\n"; } int weight; }; class Sofa{ public: Sofa(int w):weight(w){} void watchTV() { cout << "Watching TV..\n"; } int weight; }; class SleeperSofa : public Bed, public Sofa{ public: SleeperSofa(int w):Bed(w),Sofa(w){} void foldOut() { cout << "Fold out..\n"; } }; int main() { SleeperSofa ss(5); ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } Derived constructor with parameters

Ambiguous Inheritance(模糊继承) Multiple inheritance ambiguities(多继承的模糊性) int main() { SleeperSofa ss(5); ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } Bed’s weight? cout<<ss.weight; //ambiguity cout<<ss.Bed::weight; Sofa’s weight? When the base classes may have member functions with the same name, the function call of derived class object leads to ambiguity. (当两个基类成员名字相同时,派生类对象访问成员或函数时产生模糊)

Replicated Inheritance(重复继承) Bed +sleep():void Sofa +watchTV():void SleeperSofa + foldOut():void Furniture -weigh: int Single inheritance Problem: Replicated Base Classes(重复基类) multiple inheritance

Replicated Inheritance Bed +Sleep():void Sofa +watchTV():void SleeperSofa + foldOut():void Furniture -weigh: int Lattice structure (网格结构) Avoid replicated Base Classes(重复基类)?

Replicated Inheritance class Furniture{ int weight; public: Furniture(int w) :weight(w){} int getWeight(){return weight;} }; class Sofa : public Furniture{ public: Sofa(int w) :Furniture(w){} void watchTV() {cout << "Watching TV..\n";} }; class Bed:public Furniture{ public: Bed(int w) :Furniture(w){} void sleep() { cout << "Sleep..\n";} }; class SleeperSofa : public Bed, public Sofa{ public: SleeperSofa(int w) :Bed(w), Sofa(w){} void foldOut() {cout << "Fold out..\n";} }; int main() { SleeperSofa ss(5); ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } Result: Constructing furniture... Constructing Bed ... Constructing Sofa ... Constructing SleeperSofa ...

Replicated Inheritance int main() { SleeperSofa ss(5); ss.sleep(); ss.watchTV(); ss.foldOut(); return 0; } cout << ss.getWeight() << endl; //ambiguity Result: Constructing furniture... Constructing Bed ... Constructing Sofa ... Constructing SleeperSofa ... Problem: Replicated Base Classes solving

Virtual Inheritance(虚继承) int main() { SleeperSofa ss(5); ss.sleep();ss.watchTV(); ss.foldOut(); cout << ss.getWeight() << endl; return 0; } class Furniture{ int weight; public: Furniture(int w) :weight(w){} int getWeight(){return weight;} }; class Bed:virtual public Furniture{ public: Bed(int w) :Furniture(w){} void sleep() { cout << "Sleep..\n";} }; virtual inheritance class SleeperSofa : public Bed, public Sofa{ public: SleeperSofa(int w) : Furniture(w), Bed(w), Sofa(w){} void foldOut() {cout << "Fold out..\n";} }; class Sofa :virtual public Furniture{ public: Sofa(int w) :Furniture(w){} void watchTV() {cout << "Watching TV..\n";} };

Virtual Inheritance Virtual Inheritance(虚继承) The mechanism offers a way to save space and avoid ambiguity in multiply inheritance. (虚继承为多继承提供一种节省空间并避免模糊继承的方法) Virtual base class(虚基类) The common base class in this mechanism is called virtual base class. For example, Furniture is a virtual base class of Bed and Sofa. Constructing sequence: Furniture Bed Sofa SleeperSofa

Virtual Inheritance The constructing sequence of the virtual base classes In the same level, the virtual base classes are constructed according to the sequence of their declaration.(同级别虚基类按照声明顺序构造) class C : virtual public A, virtual public B{ .... }; constructing sequence: A -> B -> C When virtual base classes and non-virtual base classes exist the same level, virtual base classes are first constructed and non-virtual base classes are then constructed.(虚基类先于其它基类构造) class C : public A, virtual public B{ .... }; constructing sequence: B -> A -> C

Summary Be able to understand multiple inheritance Be able to know why ambiguous inheritance happens Be able to define a virtual base class