Multiple and Virtual Inheritance in C++

Similar documents
C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Important Questions with Answers

VIRTUAL FUNCTIONS Chapter 10

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS3157: Advanced Programming. Outline

G52CPP C++ Programming Lecture 13

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Ch. 11: References & the Copy-Constructor. - continued -

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Object Oriented Programming. Solved MCQs - Part 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Inheritance and Overloading. Week 11

And Even More and More C++ Fundamentals of Computer Science

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Midterm Exam 5 April 20, 2015

Comp151. Inheritance: Initialization & Substitution Principle

Comp151. Inheritance: Initialization & Substitution Principle

Fast Introduction to Object Oriented Programming and C++

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

Simplest version of DayOfYear

A <Basic> C++ Course

COEN244: Class & function templates

CPSC 427a: Object-Oriented Programming

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

A <Basic> C++ Course

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Special Member Functions

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

OOP THROUGH C++(R16) int *x; float *f; char *c;

Short Notes of CS201

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Inheritance. Object-Oriented Designs. Inheritance. C++ Object Oriented Programming Pei-yih Ting NTOUCS

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

CS201 - Introduction to Programming Glossary By

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 4, 2016

COMP 2355 Introduction to Systems Programming

American National Standards Institute Reply to: Josee Lajoie

7 TEMPLATES AND STL. 7.1 Function Templates

Interview Questions of C++

CS 376b Computer Vision

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Chapter 5 Object-Oriented Programming

Object Oriented Programming in C++ Basics of OOP

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Instantiation of Template class

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Supporting Class / C++ Lecture Notes

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

IS 0020 Program Design and Software Tools

Extending Classes (contd.) (Chapter 15) Questions:

CPSC 427: Object-Oriented Programming

Recharge (int, int, int); //constructor declared void disply();

6.096 Introduction to C++ January (IAP) 2009

Chapter 16. Templates. Copyright 2010 Pearson Addison-Wesley. All rights reserved

C++ Programming: Polymorphism

Template Issue Resolutions from the Stockholm Meeting

AIMS Embedded Systems Programming MT 2017


INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

ECE Fall 2014, Final Exam

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Review Questions for Final Exam

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Object Oriented Programming. Java-Lecture 11 Polymorphism

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Major Differences between Java and C++ Programming in C++ Multiple Inheritance

Implementing Subprograms

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

class for simulating a die (object "rolled" to generate a random number) Dice(int sides) -- constructor, sides specifies number of "sides" for the die

Java Object Oriented Design. CSC207 Fall 2014

OOP. Unit:3.3 Inheritance

CS250 Final Review Questions

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

Comp 249 Programming Methodology

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

More C++ : Vectors, Classes, Inheritance, Templates

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Programming, numerics and optimization

Object Oriented Design

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++: Assignment Week 8

Polymorphism. Zimmer CSCI 330

CS105 C++ Lecture 7. More on Classes, Inheritance

LECTURE 11 TREE TRAVERSALS

Exam Duration: 2hrs and 30min Software Design

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

Transcription:

Multiple and Virtual Inheritance in C++ Initialization of virtual bases Final classes in C++ Multiple inheritance, virtual members and virtual inheritance. Construction order with multiple inheritance 1

Constructors and virtual base classes? struct V { V(const char * s) { cout << s; } struct B1 : virtual V { B1(const char * s) : V("B1") { cout << s; } struct B2 : virtual V { B2(const char * s) : V("B2") { cout << s; } 2 struct D : B1, B2 { D() : B1("DB1"), B2("DB2") {} } d; First let s draw a D object, What will be printed?

Construction of a virtual base class Answer: nothing will be printed. The compiler will issue an error message. Work-around 1: define a parameter-less constructor in V. Work-around 2: call V s constructor directly from the constructor of D. Virtual bases are always initialized by most derived class other initializations are ignored. This also applies if the most derived class is not an immediate derived class of the virtual base. Work-around 1 is actually the same solution by giving V a default constructor, it will always be implicitly called by the most derived class. Comments: All virtual inheritances of the same object are unified. All non-virtual inheritances of the same object are distinct. 3

Virtual base initialization example struct V { V(); V(int);... struct B1 : virtual V { B1(); B1(int i): V(i) { /* */ }... struct B2 : virtual V { B2(); B2(int i) { /* */ }... struct D : B1, B2 { D(int i): V(i) { /* */ }... V v(1); B1 b1(2); B2 b2(3); D d(4); // use V(int) // use V(int) // use V() // use V(int) 4

Frozen classes struct Ice{ Ice() { // Constructor ice.h #include "ice.h" The trick may be easily worked around by virtually deriving the Violation class from Ice class Frozen: private virtual Ice { //... class Violation: public Frozen { //... 5 Error:Ice::Ice() is not accessible in function Violation::Violation() (Though it works in some compilers)

Frozen classes take 2 template <typename T> class Ice{ Ice() { // Private constructor friend class T; #include "ice.h" ice.h class Frozen: private virtual Ice<Frozen> { //... class Violation: public Frozen { // ERROR! //... Some compilers overlook privacy of virtual inheritance They can t ignore private constructors! 6

C++11 Explicit final struct Base1 final { // // ill-formed because the class Base1 has been // marked final struct Derived1 : Base1 { // struct Base2 { virtual void f() final; struct Derived2 : Base2 { // ill-formed because the virtual function // Base2::f has been marked final void f(); 7

Construction and multiple inheritance Elements to initialize: sub-objects and fields. Where to initialize? Best is in the initialization list (after the constructor signature). Sub-objects can only be initialized in the initialization list. Order of elements in the initialization list is unrelated to the order in which they will actually be invoked. This makes it possible to guarantee that the construction order is the exact opposite of destruction order. Construction order is a recursive algorithm: 1. Virtual base classes, in the order they occur in depth-first, left-to-right (by definition order) traversal of the hierarchy graph. o If a virtual base class is derived from a non-virtual base, then this non-virtual base will be constructed before the virtual base. 2. Remaining base classes, in the order they occur in the hierarchy graph. 3. Fields (data members). 4. Constructor body. Order of destruction is the same in reverse. 8

Initialization order algorithm example Apply topological sort ranking inheritance DAG in a depth-first, left-right scan Virtual and non-virtual inheritance are treated alike. Construct all virtual base classes (immediate and non-immediate) Use ranking order. Do not construct twice. Apply recursively to construct their non-virtual bases. Construct non-virtual immediate base classes: Use ranking order (same as definition order). Apply recursively to construct their non-virtual bases. Construction order in example: U1 U2 Y X V2 V1 V3 V4 B1 B2 D Destruction order in example: 9 D B2 B1 V4 V3 V1 V2 X Y U2 U1 B1 U1 1 2 2 1 X 1 2 1 U2 2 34 V1 6 V2 5 V3 7 V4 8 1 2 3 4 4 3 2 1 1 4 9 10 2 3 D 11 Y 1 34 B2 2 non virtual inheritance virtual inheritance