0
Can anyone plz elaborate the calling of constructor and destructor by giving some program as example?
1 Odpowiedź
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.