+ 1
Why do we need Constructors and Destructors?
I am doing the lessons on classes I dont know why I need to know them in code? Maybe give an example where you need to use them in code
3 Answers
+ 8
A constructor does some kind of task every time an object is made. It might give some default values to the variables, or write to a log file, or some other needed task.
Destructors do something once an object is disposed of to free memory. If the object uses a database or file, it can disconnect from it, which saves more memory.
For where you probably are right now, you might want it to try experimenting with them, but it might not beâ necessary for anything you could write.
0
damn almost 2 years later, i never chose best answer woops
and i understand this perfectly:)