What is the importance of constructors and destructors and what do they do? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the importance of constructors and destructors and what do they do?

31st Jul 2016, 10:24 PM
jigoku_tenshi
7 Answers
+ 5
When you wrote the souce code for the class A you created the method ~A() class A{ public: A();// constructor ~A();// destructor } void f(){ A var; // i've created an object of type A /* some code */ }// the compiler is calling the destructor for the object var. The difference between static and dynamic variables is the lifespan; a static variable exists only within the curly brackets it was declared but a dynamic variable exist until you delete it. In both cases the destructor is doing the same thing and as i mentioned before the destructor is called by the compiler when the object is destroyed.
1st Aug 2016, 12:04 AM
bogdan
bogdan - avatar
+ 1
A constructor is the first method that runs when an object is created. It can be used to initialize the values of the attributes and prepare the object to be used. A destructor is the method that runs when an object is destroyed. It is useful for example if you allocated some data on heap and after you destroy the object you don't need that information anymore so when you destroy the object containing that data you deallocate the variable.
31st Jul 2016, 11:05 PM
bogdan
bogdan - avatar
0
And how does one destroy an object?
31st Jul 2016, 11:11 PM
jigoku_tenshi
0
well lets say i have a class A { A var; // i've createa an object of type A /* some code */ }// at this point every variable within the curly brackets that are declared static are destroyed if you declare a variable dynamic like this: A *var = new A; the only way you can destroy this variable is by using the keyword delete: delete var; if you don't delete a variable you no longer need you are making a memory leak that means you are using memory for no reason and if your program is running a specific period of time the program might crash.
31st Jul 2016, 11:29 PM
bogdan
bogdan - avatar
0
I apologize for the excessive questions, but I don't get how all the variables that were declared static within the curly brackets were destroyed. Isn't there supposed to be a ~A() somewhere?
31st Jul 2016, 11:44 PM
jigoku_tenshi
0
Now I got it. I'll make better sense of it when I use it in an actual program, but it makes sense now. Thanks!
1st Aug 2016, 12:11 AM
jigoku_tenshi
0
If you have defined an object using 'new', you can delete it with, well, 'delete'. That will free up the memory being used by the object. Deleting a 'new' object within the scope (function, for instance) it's created in will avoid the memory leak. If it isn't deleted and the function/class it was scoped to is destroyed, you've lost access to the variable, hence, a memory leak. If an object is created, but without the 'new' specifier, it will be destroyed automatically (via a call to its destructor) once the function/class it is part of is destroyed.
6th Aug 2016, 10:09 PM
Jonathan Price
Jonathan Price - avatar