Hello all.
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..
Please post...

Recommended Answers

All 10 Replies

What are some of your thoughts, then someone can either confirm them or make corrections.

I guess that constructor initializes the class when program is started and destructor destroys the memory allocation..

Not when the program is started, but when the object is created.

How about the destructor for a class that doesn't do any memory allocation?

What happens when you don't specify a constructor for the class?

No Destructor only Deletes the memory allocation.
if i would've known the answer, i wouldn't had started the thread......

if i would've known the answer, i wouldn't had started the thread......

Me telling you the answer doesn't help you learn anything. I'm just asking you some follow up questions, I'm not expecting that you're going to know everything

If you include a destructor with nothing in the body of it (or when the compiler provides a default constructor), it doesn't do anything at all.

ok... Please help me with the syntax and also tell that what are they used for

They are called at the time the object is created. They can be used to initialize member variables and/or allocate memory for them.

class MyClass
{
   private:
       int x,y;
   public:
       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;
       }

      ~MyClass() //destructor
       {
             //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.

EDIT:
Oops, this may be out of date due to overlap...

Oops, this may be out of date due to overlap...

No worries, I'm bound to have overlooked something.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.