+ 9

I want to know the reason behind the unusual output of this constructor-destructor problem?

#include <iostream> using namespace std; class A{ public: A() {cout<<"a";} ~A() {cout<<"b";} }; class B {public: B() {cout<<"c";} ~B() {cout<<"d";} }; int main() { B b; A a; return 0; } The above program gives the output- ( cabd) please explain why?

15th Aug 2016, 2:33 PM
Atul Anand Jha
Atul Anand Jha - avatar
10 Respuestas
+ 15
The destructors are called when the object are explicitly destroyed or when the program ends... and so far as it seems, the objects are deleted from memory in a LIFO order (Last in, First out)
15th Aug 2016, 6:48 PM
Nelson Urbina
Nelson Urbina - avatar
+ 4
The destructor is ran when an object is deleted from memory. Your main doesn't say to get rid of anything before it gets to the end. Your main is: Create a B, create an A, exit the program with return code 0 (exiting thus deletes the objects). Destructors aren't ran immediately after constructors, destructors are only there to do stuff when the object is deleted. I think you're treating the class like a function, as in it goes through everything line by line. Classes are their own little blueprint for an entity, constructors and destructors being defined functions relating to the class they're assigned to. They are separate from one another.
15th Aug 2016, 2:58 PM
Ahkrin
+ 2
As if was written above, these objects are built in the stack, so naturally they are released in reverse order. https://isocpp.org/wiki/faq/dtors#order-dtors-for-locals
15th Dec 2016, 12:08 PM
Udi Finkelstein
Udi Finkelstein - avatar
+ 1
You create a B so it calls B's constructor to print a c. You make an A so it calls A's constructor to print an a. Same story for the return, it destructs the objects in the order declared (I believe, or the order in memory?) so A's destructor to print b is invoked and d from B is invoked.
15th Aug 2016, 2:41 PM
Ahkrin
+ 1
but brother ahkrin, like you said....at first the B class is called.thus it operates the constructor part.but it must print for the destructor part too before leaving class B and moving onto class A. you can cross check this with class A's example.the constructor part is printed.afterwards the destructor part is printed together. this is my opinion....think on it. may be I am wrong.then please correct me.and make me understand ur point of view..I would be so much thankful to u.
15th Aug 2016, 2:48 PM
Atul Anand Jha
Atul Anand Jha - avatar
+ 1
oh...that sounds pretty reasonable.I think this has cleared my doubt.you explained it so well.thanks brother.
15th Aug 2016, 3:03 PM
Atul Anand Jha
Atul Anand Jha - avatar
+ 1
this seems to be like variables are stored in. a stack. am I right bro.?
15th Aug 2016, 6:52 PM
Atul Anand Jha
Atul Anand Jha - avatar
0
Yes... that's what it seems...I'm not sure if that behavior is compiler dependant... but at least in here it behaves like a stack..
15th Aug 2016, 8:54 PM
Nelson Urbina
Nelson Urbina - avatar
0
voce
15th Dec 2016, 6:49 PM
pedrogabriel
0
it's just like opening and closing braces... {(.....)}. so first when u open the curl brace that is according to your program object 'b' so that constructor is called.. and then the A class constructor so the brace has to be closed ... so first the normal bracket that is A destructor followed by B destructor!!
9th Feb 2017, 5:19 PM
swetha sri sridhara
swetha sri sridhara - avatar