I am new to c++ and and our teachers have started Object oriented programming.. I am facing real trouble in understanding the concepts of constructor and destructor. Why are they used and how are they used..
They are called at the time the object is created. They can be used to initialize member variables and/or allocate memory for them.
MyClass(int xin,int yin)
x = xin;
y = yin; //there's a shortcut to doing this, look up initialization lists
MyClass() //default constructor (no parameter)
x = 0;
y = 0;
//nothing brewing here
No Destructor only Deletes the memory allocation.
if i would've known the answer, i wouldn't had started the thread......
Not quite right. A "destructor", like a "constructor", is just another function (except they don't return anything). It's important that you understand the functions themselves don't actually do anything unless you tell them to in your implementation.
The significance of them is WHEN THEY EXECUTE, not really what they do. They execute automatically at object creation (constructor) and when an object goes out of scope (destructor). This, rather than what they do, is really what makes a constructor/destructor what it is. What they need to do is largely determined by this timing.
Try to approach the situation from the perspective of "What needs to be done when I create/destroy an object?" and that should help you understand better.
Oops, this may be out of date due to overlap...