Can anyone plz elaborate the calling of constructor and destructor by giving some program as example? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Can anyone plz elaborate the calling of constructor and destructor by giving some program as example?

23rd Jul 2016, 7:21 PM
Kaivalya Dalvi
Kaivalya Dalvi - avatar
1 ответ
0
whats there to elaborate? :-) it's a portion of code that's called everytime an object of its class is created (constructor) or destroyed (destructor). So for example you have a 'car' class and one of its properties is color. Lets say that you want to have every car you create a different color. You just put a nice line that randomizes the color in the constructor and its done :-) same thing with destructor. If you want EVERY car to explode on destroying you put it in destructor and just watch the fireworks (cause it will be called everytime object of that class is unloaded from program). utilizing constructors and destructors can be very handy but its all dependant on situation. not every class you create will have custom constructor/destructor.
23rd Jul 2016, 10:41 PM
PaulC
PaulC - avatar