Virtual base, constructor & destructor

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

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.
I NHERITANCE Chapter 7 Department of CSE, BUET 1.
1 ָ נן oop C++ Addendum: Multiple and Virtual Inheritance in C++ uInitialization of Virtual Bases uFrozen Classes in C++ uConstruction Order with MI.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
C++ data types. Structs vs. Classes C++ Classes.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
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.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
C++ Classes C++ Interlude 1 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
1 Introduction to Object Oriented Programming Chapter 10.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
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,
Procedural and Object-Oriented Programming
OBJECT ORIENTED PROGRAMMING
2.7 Inheritance Types of inheritance
Programming with ANSI C ++
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Object-Oriented Programming
Introduction to Classes
Chapter 5 Classes.
Java Unit 11: Inheritance I
Class Operations Pointer and References with class types
This pointer, Dynamic memory allocation, Constructors and Destructor
C++ Classes C++ Interlude 1
Interfaces.
Inheritance, Polymorphism, and Interfaces. Oh My
Understanding Inheritance
Introduction to Classes
Lecture 22 Inheritance Richard Gesick.
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Packages and Interfaces
Polymorphism Polymorphism
Constructors and destructors
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Fundaments of Game Design
COP 3330 Object-oriented Programming in C++
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
NAME 436.
Templates Generic Programming.
Tag at C++ Compiler Kei Hasegawa.
ENERGY 211 / CME 211 Lecture 22 November 10, 2008.
C++ data types.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Polymorphism Reference and pointer implicit type casting
Constructors & Destructors
Templates Generic Programming.
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.
Presentation transcript:

Virtual base, constructor & destructor 2019.07.12 Kei Hasegawa

Example V A B C struct C : A, B { }; struct V { ... C(int c) : A(c) {} V(int); ~V(); }; struct A : virtual V { A(int a) : V(a) { ... } struct B : virtual V { ~B(); V A B C

Constructor of C Call default constructor of V that is common part Nothing to be done if there doesn’t exist constructor of V It’s error if V has non-default construct and doesn’t have default constructor Call special version constructor of A and B which are base class of C: not call constructor of V

Constructor of A struct A : virtual V { }; ... A(int a) : V(a) { ... } Special version constructor Called from constructor of C Generated by compiler Same with A(int) in spite of not calling constructor of V } };

Consturctor of B Basically same with A struct B : virtual V { }; ... Generated by compiler B has virtual base V or V constructor is declared } B’() { Special version Called from construct of C Same with B() in spite of not calling constructor of V };

Destructor of C Call special version destructor of B and A Not calling destructor of V Call destructor of V which is common part Nothing to be done if V has no destructor

Destructor of B struct B : virtual V { }; ... ~B(); ~B’() { } Generated by compiler Same with ~B() in spite of not calling ~V() Called from constructor of C } };

Destructor of A Basically same with B struct A : virtual V { }; ... Generated by compiler ~V() is declared } ~A’() { Called from destructor of C Same with ~A() in spite of not calling ~V() If there exists nothing to be done, calling from destructor of C can be omitted. };

Implementation note Number of special versions constructor & destructor is 2^n – 1, where n is number of virtual base struct A : virtual V1, virtual V2, ..., virtual Vn { ... }; All these special versions shouldn’t be generated at compiling of A When struct C is declared like example, special versions constructor & destructor of A and B are required. So, it’s reasonable to generate them when they are require.

Implementation abstract (1) Enable to specify exclude list when generating base class constructor & destructor struct V at example Suppress inline substitution for exchanging special version constructor & destructor C::C(int c) : A(c) { ... } A(c) will be exchanged to special version

Implementation abstract (2) Constructor & destructor of class which has virtual base Generate inline function for body part struct A : virtual V { ... A(int a) : V(a) { /* Code here saved */ } A.body(int a) { /* at this inline function */ } }; For special version of A::A(int)