SlideShare a Scribd company logo
1 of 23
INHERITANCE
Order of execution of constructor and
destructor during inheritance
Basic points[Order of execution of constructor and destructor during inheritance ]
• If a constructor is defined in the base class and derived class, then on
creating the object of derived class, base class constructor will be executed
first, then derived class constructor will be called.
• Order of execution of destructor will be in reverse order as that of the
order of execution of constructor
• If a base class is having default constructor, then it is optional for derived
class to have a default constructor
• If a base class is having parameterized constructor, then it is mandatory for
derived class to have a parameterized constructor which will pass the
parameters to the base class constructor through initializer list.
Order of execution of constructor and destructor
during single inheritance[Default constructor in both
classes]
#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"nCalling default base class
constructor";
}
~A()
{
cout<<"nCalling base class destructor";
}
};
class B:public A
{
public:
B()
{
cout<<"n Calling default derived constructor";
}
~B()
{
cout<<"nCalling derived class destructor";
}
};
int main()
{
B obj1;
return 0;
}
Output:
Calling default base class constructor
Calling default derived constructor
Calling derived class destructor
Calling base class destructor
Order of execution of constructor and destructor during single
inheritance[Parameterized constructor in both classes]
#include<iostream>
using namespace std;
class A
{
int x;
public:
A(int a)
{
x=a;
cout<<"nCalling base class parameterized
"<<x;
}
~A()
{
cout<<"nCalling base class destructor";
}
};
class B:public A
{
int l;
public:
B(int p):A(p)
{
l=p;
cout<<"nCalling derived class
parameterized:"<<l;
}
~B()
{
cout<<"nCalling derived class destructor";
}
};
int main()
{
B obj2(1);
return 0;
}
Output:
Calling base class parameterized 1
Calling derived class parameterized: 1
Order of execution of constructor and destructor during single
inheritance[Parameterized and default constructor in both classes]
#include<iostream>
using namespace std;
class A
{
int x;
public:
A()
{
cout<<"nCalling base class default";
}
A(int a)
{
x=a;
cout<<"nCalling base class
parameterized "<<x;
}
~A()
{
cout<<"nCalling base class destructor";
}
};
class B:public A
{
int l;
public:
B()
{
cout<<"nCalling derived class default";
}
B(int p):A(p)
{
l=p;
cout<<"nCalling derived class parameterized:"<<l;
}
~B()
{
cout<<"nCalling derived class destructor";
}
};
int main()
{
//B obj1; //Either default/ or parameterized constructor can be called
B obj2(1);
return 0;
}
Polling Questions(Q1)
In case of inheritance where both base and derived class are having
constructors, when an object of derived class is created then___________ .
A. constructor of derived class will be invoked first
B. constructor of base class will be invoked first
C. constructor of derived class will be executed first followed by base class
D. constructor of base class will be executed first followed by derived class
Q2
If base class has constructor with arguments, then it will be ________________ for the
derived class to have constructor and pass the arguments to base class constructor.
A. Optional
B. Mandatory
C. Compiler dependent
D. Compiler Error Situation
Q3
In case of inheritance where both base and derived class are having constructor and
destructor, then which if the following are true ?
1. Constructors are executed in their order of derivation
2. Constructors are executed in reverse order of derivation
3. Destructors are executed in their order of derivation
4. Destructors are executed in reverse order of derivation
A. Only 2 ,4
B. Only 1 , 3
C. Only 1 , 4
D. Only 2, 3
Q4
#include <iostream>
using namespace std;
class Base
{
public:
Base() { cout << "Base"; }
};
class Derived : public Base
{
public:
Derived(int i) { cout << i; }
};
int main()
{
Derived d2(10);
return 0;
}
A. Base10
B. 10Base
C. Base
D. 10
Order of execution of constructor and destructor during Multilevel inheritance
Program-Order of execution of constructor and destructor during Multilevel
inheritance
#include<iostream>
using namespace std;
class A
{
int x,y;
public:
A(int r,int s)
{
x=r;
y=s;
cout<<"nCalling base class constructor:"<<x<<" "<<y;
}
~A()
{
cout<<"nCalling base class destructor";
}
};
class B:public A
{
int l,m;
public:
B(int p,int q,int r,int s):A(r,s)
{
l=p;
m=q;
cout<<"nCalling derived class B constructor:"<<l<<"
"<<m;
}
~B()
{
cout<<"nCalling derived B class destructor";
}
};
Program-Order of execution of constructor and destructor during Multilevel
inheritance….Continued
class C:public B
{
int n,m;
public:
C(int u,int v,int p,int q,int r,int s):B(p,q,r,s)
{
n=u;
m=v;
cout<<"nCalling derived class C constructor with
values:"<<n<<" "<<m;
}
~C()
{
cout<<"nCalling derived class C destructor";
}
};
int main()
{
C obj1(1,2,3,4,5,6);
return 0;
}
Order of execution of constructor and destructor during Multiple
inheritance
• Order of execution of constructor during multiple inheritance is dependent upon
the order of derivation(or order of inheritance).
• Order of execution of destructor will be reverse as that of order of execution of
constructor
Order of execution of constructor and destructor during
Multiple inheritance…continued…
Class A:public B, virtual C
{
};
Order of execution of constructor
//C() virtual base
//B() ordinary base
//A() derived
If one of the class is inherited in virtual mode, then constructor
of that will be executed first and then order of inheritance will be
followed and if both are derived in virtual mode, then order of
inheritance will be followed(as usual)
Polling Questions
//Output??
#include<iostream>
using namespace std;
class Base1 {
public:
Base1()
{ cout << " Base1's constructor called" <<
endl; }
};
class Base2 {
public:
Base2()
{ cout << "Base2's constructor called" <<
endl; }
};
class Derived: public Base1, public Base2 {
public:
Derived()
{ cout << "Derived's constructor called" <<
endl; }
};
int main()
{
Derived d;
return 0;
}
A. Compiler Dependent
B. Base1′s constructor called
Base2′s constructor called
Derived’s constructor called
C. Base2′s constructor called
Base1′s constructor called
Derived’s constructor called
D. Compiler Error
Output??
//Output?
#include <iostream>
using namespace std;
class Base1 {
public:
~Base1() { cout << " Base1's destructor" << endl; }
};
class Base2 {
public:
~Base2() { cout << " Base2's destructor" << endl; }
};
class Derived: public Base1, public Base2 {
public:
~Derived() { cout << " Derived's destructor" << endl;
}
};
int main()
{
Derived d;
return 0;
}
A. Base1's destructor
Base2's destructor
Derived's destructor
B. Derived's destructor
Base2's destructor
Base1's destructor
C. Derived's destructor
D. Compiler Dependent
Output??
What will be the order of execution of base class constructors in the following method
of inheritance?
class A: public B, virtual public C {….};
A) B(); C(); A();
B) C(); B(); A();
C) A(); B(); C();
D) B(); A(); C();
Program-Order of execution of constructor and destructor during Multiple
inheritance
#include<iostream>
using namespace std;
class M
{
protected:
int m;
public:
M(int x)
{
m=x;
cout<<"nIn M";
}
};
class N
{
protected:
int n;
public:
N(int y)
{
n=y;
cout<<"nIn N";
}
};
class P:public M,public N
{
int l;
public:
P(int p,int q,int r):M(r),N(q)
{
l=p;
cout<<"nIn P";
}
void display()
{
cout<<“n”<<"m="<<m<<" "<<"n="<<n<<"
"<<"l="<<l;
}
};
int main()
{
P obj1(3,2,1);
obj1.display();
return 0;
}
Output:
In M
In N
In P
Order of execution of constructor and destructor during hierarchical inheritance
#include<iostream>
using namespace std;
class M
{
protected:
int m;
public:
M(int x)
{
m=x;
cout<<"nIn M";
}
};
class N:public M
{
protected:
int n;
public:
N(int y):M(y)
{
n=y;
cout<<"nIn N";
}
};
class P:public M
{
int l;
public:
P(int p):M(p)
{
l=p;
cout<<"nIn P";
}
};
int main()
{
N obj1(1);//Output In M,In N
P obj2(2);//Output In M,In P
return 0;
}

More Related Content

Similar to Inheritance_PART2.pptx

Constructors.16
Constructors.16Constructors.16
Constructors.16myrajendra
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classesDocent Education
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructorsAkshaya Parida
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Abu Saleh
 
Complet vector.dep.inc# This code depends on make tool being .docx
Complet vector.dep.inc# This code depends on make tool being .docxComplet vector.dep.inc# This code depends on make tool being .docx
Complet vector.dep.inc# This code depends on make tool being .docxardhowp
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++sandeep54552
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Abu Saleh
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfLadallaRajKumar
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++RAJ KUMAR
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initializationDocent Education
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initializationDocent Education
 
Padroes Projeto
Padroes ProjetoPadroes Projeto
Padroes Projetolcbj
 
Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Julien Truffaut
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptishan743441
 

Similar to Inheritance_PART2.pptx (20)

Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Lab3
Lab3Lab3
Lab3
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
 
Complet vector.dep.inc# This code depends on make tool being .docx
Complet vector.dep.inc# This code depends on make tool being .docxComplet vector.dep.inc# This code depends on make tool being .docx
Complet vector.dep.inc# This code depends on make tool being .docx
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructor
ConstructorConstructor
Constructor
 
Constructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdfConstructors & Destructors [Compatibility Mode].pdf
Constructors & Destructors [Compatibility Mode].pdf
 
Constructors and destructors in C++
Constructors and destructors in  C++Constructors and destructors in  C++
Constructors and destructors in C++
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
12 constructors invocation and data members initialization
12 constructors invocation and data members initialization12 constructors invocation and data members initialization
12 constructors invocation and data members initialization
 
Padroes Projeto
Padroes ProjetoPadroes Projeto
Padroes Projeto
 
Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Inheritance_PART2.pptx

  • 1. INHERITANCE Order of execution of constructor and destructor during inheritance
  • 2. Basic points[Order of execution of constructor and destructor during inheritance ] • If a constructor is defined in the base class and derived class, then on creating the object of derived class, base class constructor will be executed first, then derived class constructor will be called. • Order of execution of destructor will be in reverse order as that of the order of execution of constructor • If a base class is having default constructor, then it is optional for derived class to have a default constructor • If a base class is having parameterized constructor, then it is mandatory for derived class to have a parameterized constructor which will pass the parameters to the base class constructor through initializer list.
  • 3. Order of execution of constructor and destructor during single inheritance[Default constructor in both classes] #include<iostream> using namespace std; class A { public: A() { cout<<"nCalling default base class constructor"; } ~A() { cout<<"nCalling base class destructor"; } }; class B:public A { public: B() { cout<<"n Calling default derived constructor"; } ~B() { cout<<"nCalling derived class destructor"; } }; int main() { B obj1; return 0; } Output: Calling default base class constructor Calling default derived constructor Calling derived class destructor Calling base class destructor
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. Order of execution of constructor and destructor during single inheritance[Parameterized constructor in both classes] #include<iostream> using namespace std; class A { int x; public: A(int a) { x=a; cout<<"nCalling base class parameterized "<<x; } ~A() { cout<<"nCalling base class destructor"; } }; class B:public A { int l; public: B(int p):A(p) { l=p; cout<<"nCalling derived class parameterized:"<<l; } ~B() { cout<<"nCalling derived class destructor"; } }; int main() { B obj2(1); return 0; } Output: Calling base class parameterized 1 Calling derived class parameterized: 1
  • 9. Order of execution of constructor and destructor during single inheritance[Parameterized and default constructor in both classes] #include<iostream> using namespace std; class A { int x; public: A() { cout<<"nCalling base class default"; } A(int a) { x=a; cout<<"nCalling base class parameterized "<<x; } ~A() { cout<<"nCalling base class destructor"; } }; class B:public A { int l; public: B() { cout<<"nCalling derived class default"; } B(int p):A(p) { l=p; cout<<"nCalling derived class parameterized:"<<l; } ~B() { cout<<"nCalling derived class destructor"; } }; int main() { //B obj1; //Either default/ or parameterized constructor can be called B obj2(1); return 0; }
  • 10. Polling Questions(Q1) In case of inheritance where both base and derived class are having constructors, when an object of derived class is created then___________ . A. constructor of derived class will be invoked first B. constructor of base class will be invoked first C. constructor of derived class will be executed first followed by base class D. constructor of base class will be executed first followed by derived class
  • 11. Q2 If base class has constructor with arguments, then it will be ________________ for the derived class to have constructor and pass the arguments to base class constructor. A. Optional B. Mandatory C. Compiler dependent D. Compiler Error Situation
  • 12. Q3 In case of inheritance where both base and derived class are having constructor and destructor, then which if the following are true ? 1. Constructors are executed in their order of derivation 2. Constructors are executed in reverse order of derivation 3. Destructors are executed in their order of derivation 4. Destructors are executed in reverse order of derivation A. Only 2 ,4 B. Only 1 , 3 C. Only 1 , 4 D. Only 2, 3
  • 13. Q4 #include <iostream> using namespace std; class Base { public: Base() { cout << "Base"; } }; class Derived : public Base { public: Derived(int i) { cout << i; } }; int main() { Derived d2(10); return 0; } A. Base10 B. 10Base C. Base D. 10
  • 14. Order of execution of constructor and destructor during Multilevel inheritance
  • 15. Program-Order of execution of constructor and destructor during Multilevel inheritance #include<iostream> using namespace std; class A { int x,y; public: A(int r,int s) { x=r; y=s; cout<<"nCalling base class constructor:"<<x<<" "<<y; } ~A() { cout<<"nCalling base class destructor"; } }; class B:public A { int l,m; public: B(int p,int q,int r,int s):A(r,s) { l=p; m=q; cout<<"nCalling derived class B constructor:"<<l<<" "<<m; } ~B() { cout<<"nCalling derived B class destructor"; } };
  • 16. Program-Order of execution of constructor and destructor during Multilevel inheritance….Continued class C:public B { int n,m; public: C(int u,int v,int p,int q,int r,int s):B(p,q,r,s) { n=u; m=v; cout<<"nCalling derived class C constructor with values:"<<n<<" "<<m; } ~C() { cout<<"nCalling derived class C destructor"; } }; int main() { C obj1(1,2,3,4,5,6); return 0; }
  • 17. Order of execution of constructor and destructor during Multiple inheritance • Order of execution of constructor during multiple inheritance is dependent upon the order of derivation(or order of inheritance). • Order of execution of destructor will be reverse as that of order of execution of constructor
  • 18. Order of execution of constructor and destructor during Multiple inheritance…continued… Class A:public B, virtual C { }; Order of execution of constructor //C() virtual base //B() ordinary base //A() derived If one of the class is inherited in virtual mode, then constructor of that will be executed first and then order of inheritance will be followed and if both are derived in virtual mode, then order of inheritance will be followed(as usual)
  • 19. Polling Questions //Output?? #include<iostream> using namespace std; class Base1 { public: Base1() { cout << " Base1's constructor called" << endl; } }; class Base2 { public: Base2() { cout << "Base2's constructor called" << endl; } }; class Derived: public Base1, public Base2 { public: Derived() { cout << "Derived's constructor called" << endl; } }; int main() { Derived d; return 0; } A. Compiler Dependent B. Base1′s constructor called Base2′s constructor called Derived’s constructor called C. Base2′s constructor called Base1′s constructor called Derived’s constructor called D. Compiler Error
  • 20. Output?? //Output? #include <iostream> using namespace std; class Base1 { public: ~Base1() { cout << " Base1's destructor" << endl; } }; class Base2 { public: ~Base2() { cout << " Base2's destructor" << endl; } }; class Derived: public Base1, public Base2 { public: ~Derived() { cout << " Derived's destructor" << endl; } }; int main() { Derived d; return 0; } A. Base1's destructor Base2's destructor Derived's destructor B. Derived's destructor Base2's destructor Base1's destructor C. Derived's destructor D. Compiler Dependent
  • 21. Output?? What will be the order of execution of base class constructors in the following method of inheritance? class A: public B, virtual public C {….}; A) B(); C(); A(); B) C(); B(); A(); C) A(); B(); C(); D) B(); A(); C();
  • 22. Program-Order of execution of constructor and destructor during Multiple inheritance #include<iostream> using namespace std; class M { protected: int m; public: M(int x) { m=x; cout<<"nIn M"; } }; class N { protected: int n; public: N(int y) { n=y; cout<<"nIn N"; } }; class P:public M,public N { int l; public: P(int p,int q,int r):M(r),N(q) { l=p; cout<<"nIn P"; } void display() { cout<<“n”<<"m="<<m<<" "<<"n="<<n<<" "<<"l="<<l; } }; int main() { P obj1(3,2,1); obj1.display(); return 0; } Output: In M In N In P
  • 23. Order of execution of constructor and destructor during hierarchical inheritance #include<iostream> using namespace std; class M { protected: int m; public: M(int x) { m=x; cout<<"nIn M"; } }; class N:public M { protected: int n; public: N(int y):M(y) { n=y; cout<<"nIn N"; } }; class P:public M { int l; public: P(int p):M(p) { l=p; cout<<"nIn P"; } }; int main() { N obj1(1);//Output In M,In N P obj2(2);//Output In M,In P return 0; }

Editor's Notes

  1. D
  2. B
  3. C
  4. A
  5. B
  6. B
  7. B