Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.

Slides:



Advertisements
Similar presentations
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Advertisements

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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
C++ Classes & Data Abstraction
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 222 Object Oriented Programming Using C++ Inheritance.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
Rhapsody in C++ Tool Training "Essential" © I-Logix v3.0 1/29/2001 Int-1 Section 3 Intermediate Inherited Sensor.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
OOP, Virtual Functions and Inheritance
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
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.
CS212: Object Oriented Analysis and Design Lecture 14: Reusing classes in C++
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive.
 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.
Constructor in Inheritance. 2 Constructors are used to initialized object. In inheritance the base class contains default constructor then, the base class.
Constructors & Garbage Collection Ch. 9 – Head First Java.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
1.Which of the following statements is correct?
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
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.
1 CSC241: Object Oriented Programming Lecture No 03.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
EEL 3801 Part VII Fundamentals of C and C++ Programming Inheritance.
Learners Support Publications Constructors and Destructors.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
1 Chapter 9 Introduction to Classes and Objects Program Development and Design Using C++, Third Edition.
1.  The following class, called road_vehicle, very broadly defines vehicles, that travel on the road. It stores the number of wheels a vehicle has and.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Constructors and Destructors
GC211 Data structure Lecture 3 Sara Alhajjam.
Static data members Constructors and Destructors
Programming with ANSI C ++
Class A { public : Int x; A()
Constructors & Destructors.
Constructor & Destructor
Constructor & Destructor
Contents Introduction to Constructor Characteristics of Constructor
Inheritance.
CLASS DEFINITION (FIELDS)
Unit-2 Objects and Classes
Constructors and Destructors
Multiple Base Classes Inheritance
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Object-Oriented Programming (OOP) Lecture No. 27
Anatomy of Inheritance
Constructors & Destructors
Inheritance in C++ Inheritance Protected Section
Chapter 5 Classes.
Presentation transcript:

Order of Constructor Call

Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first the base class default constructor is executed and then the derived class’s constructor finishes execution.

Points to Remember Whether derived class's default constructor is called or parameterized is called, base class's default constructor is always called inside them. To call base class's parameterized constructor inside derived class's parameterized constructor, we must mention it explicitly while declaring derived class's parameterized constructor.

Base class Default Constructor in Derived class Constructors class Base { int x; public: Base() { cout << "Base default constructor“<<endl; } };

Base class Default Constructor in Derived class Constructors class Derived : public Base { int y; public: Derived() { cout << "Derived default constructor“<<endl; } Derived(int i) { cout << "Derived parameterized constructor“<<endl; } };

Base class Default Constructor in Derived class Constructors int main() { Base b; Derived d1; Derived d2(10); }

Base class Default Constructor in Derived class Constructors You will see in the above example that with both the object creation of the Derived class, Base class's default constructor is called.

Output of the program Base default constructor Derived default constructor Base default constructor Derived parameterized constructor

Base class Parameterized Constructor in Derived class Constructor We can explicitly mention to call the Base class's parameterized constructor when Derived class's parameterized constructor is called.

class Base { int x; public: Base(int i) { x = i; cout << "Base Parameterized Constructor“<<endl; } };

class Derived : public Base { int y; public: Derived(int j) : Base(j) { y = j; cout << "Derived Parameterized Constructor“<<endl; } };

int main() { Derived d(10) ; }

Output Base Parameterized Constructor Derived Parameterized Constructor

Why is Base class Constructor called inside Derived class ? Constructors have a special job of initializing the object properly. A Derived class constructor has access only to its own class members, but a Derived class object also have inherited property of Base class, and only base class constructor can properly initialize base class members. Hence all the constructors are called, else object wouldn't be constructed properly.

Constructor call in Multiple Inheritance Its almost the same, all the Base class's constructors are called inside derived class's constructor, in the same order in which they are inherited. class A : public B, public C ; // figure out the order in which the contructors get called.

In this case, first class B constructor will be executed, then class C constructor and then class A constructor.