In inheritance first base class constructer is call so why destructer of drive class is first call? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

In inheritance first base class constructer is call so why destructer of drive class is first call?

15th Sep 2020, 5:26 AM
vaibhav patil
9 Answers
+ 2
Ummm, DRIVED CLASS or DERIVED CLASS, I do not know what's drived class is but if you mean derived class, then, The derived class must be constructed after the base class so that the derived class constructor can refer to base class data. For the same reason, the derived class destructor must run before the base class destructor. It's very logical: we construct from the inside out, and destroy from the outside in. If the base class destructor throws an exception, it cannot be caught by the derived class destructor. Nor can an exception in a base class constructor be handled by a derived class constructor. In general, exceptions should not be thrown from destructors. If still not clear...
19th Sep 2020, 8:16 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
+ 1
Constructer concept is clear but I am asking about destructer
16th Sep 2020, 12:33 PM
vaibhav patil
+ 1
WHAT IS DERIVED CLASS ? A class that is created from an existing class. The derived class inherits all members and member functions of a base class. The derived class can have more functionality with respect to the Base class and can easily access the Base class. A Derived class is also called a child class or subclass --------------------------------------------------------------- BASE CLASS is a subclass of a class, whereas DERIVED CLASS is a class referencing to a base class (or a subclass of a base class) Clearly, without a base class whom will the derived class inherit to... Therefore, to destroy the parent you need to first destroy the child class. Which means, Derived class inherit from Base class, so to destroy Base class, we need to destroy the Derived class (child) first and then the Base class (parent). Hope it's pretty clear now...
20th Sep 2020, 8:15 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
+ 1
Thanks it's understand
20th Sep 2020, 9:17 AM
vaibhav patil
0
To understand this you will have to recall your knowledge on inheritance. What happens when a class is inherited from other? The data members and member functions of base class comes automatically in derived class based on the access specifier but the definition of these members exists in base class only. So when we create an object of derived class, all of the members of derived class must be initialized but the inherited members in derived class can only be initialized by the base class’s constructor as the definition of these members exists in base class only. This is why the constructor of base class is called first to initialize all the inherited members. See the practical application for better understanding...
16th Sep 2020, 7:59 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
0
See this practical, // C++ program to show the order of constructor call // in single inheritance #include <iostream> using namespace std; // base class class Parent { public: // base class constructor Parent() { cout << "Inside base class" << endl; } }; // sub class class Child : public Parent { public: //sub class constructor Child() { cout << "Inside sub class" << endl; } }; // main function int main() { // creating object of sub class Child obj; return 0; } OUTPUT Inside base class Inside sub class Hope it's clear with this...
16th Sep 2020, 8:02 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
0
Probably you are trying to ask, WHY DESTRUCTER OF BASE CLASS IS CALLED FIRST ? If this is the question, here's why, -> You never need to explicitly call a destructor. -> A derived class's destructor (whether or not you explicitly define one) automatically invokes the destructors for base class - subobjects. -> Base classes are destructed after member objects. If I got the question wrong again, please mention...
18th Sep 2020, 3:26 PM
Arun Bhattacharya
Arun Bhattacharya - avatar
0
Sorry, butYou not got question question is WHY DISTRUCTER OF DRIVED CLASS IS CALLED FIRST?
18th Sep 2020, 3:35 PM
vaibhav patil
0
Ok my mistake , I mean derived class you can explain neatly?
19th Sep 2020, 1:51 PM
vaibhav patil