PDA

View Full Version : Constructors and Deconstructor



Alisa
November 5th, 2012, 07:52 PM
Hello Friends,


Now, I am learning the C++ Language. At time this, I have learn only the basics of this Language.
But Here, I want to ask you,
What is use of constructors and Deconstructors in C++ Language ...

commodorejohn
November 5th, 2012, 08:01 PM
Constructors are used for initial set-up of things a given instance of a class needs - say, if you're creating a generic spaceship class in a video game, you might give it a constructor that takes things like shape/model, mass, and engine power as arguments, so that any given instance knows what kind of spaceship it's trying to be.

Destructors are used for any clean-up that needs to get done before an instance of a class exits. If an instance of a class has memory allocated outside of its basic member storage, or if it's gotten exclusive access to the display, or something, then it needs to relinquish all that, otherwise you'll wind up with errors like memory leaks and device conflicts.

barythrin
November 6th, 2012, 07:51 AM
Have you programmed in any other languages before? In simpler languages like Basic, Basic takes care of allocating memory for variable names and releasing that memory after it's done running for you. C is a bit simpler and when you allocate a variable's memory it's up to you to be a good programmer and make sure you use a destruct to free it up after your program is done. I think the newest version or flavor of C does this for you now but I could be wrong. I lost track of all the C variants.