Teen Programmers Unite  
 

 

Return to forum top

destructor constractor !! " i do NOT understand "

Posted by rollic2002 [send private reply] at May 23, 2002, 11:28:27 AM

why do we need a constructor and a destructor please help me !!!!!!!!!!!!!!!!!!!!!!!!!

Posted by RedX [send private reply] at May 23, 2002, 11:40:58 AM

Constructor alow you to be sure certain things (allocating memory for example) are done when a object is created.
Destructor alow you to clean up the mess without having to trust the programmer on this.

For example an object that holds a string. The constructor would allocate the memory for the string. Its member functions could do all sort of stuff with the string and the destructor would deallocated the memory of the string.

RedX

Posted by CodeRed [send private reply] at May 23, 2002, 01:02:44 PM

The most common use of constructors is to initialize an objects instance variables. Here are two examples, one with a constructor and one without:

Without Constructor:

class Box
{
  private:
    int width;
    int heigth;
  public:
    void setWidth(int w)
    {
      width = w;
    }
    void setHeigth(int h)
    {
      heigth = h;
    }
};

void main()
{
  Box box1;            //Creates a box
  box1.setWidth(5);    //Sets the width to 5
  box1.setHeigth(10);  //Sets the heigth to 10
}


Now with a constructor:
class Box
{
  private:
    int width;
    int heigth;
  public:
    Box(){}           //Default constructor
    Box(int w, int h) //Overloaded constructor
    {
      width = w;
      heigth = h;
    }
};

void main()
{
  Box box1(5,10); //Creates a box with width=5 and heigth=10
}
Posted by RedX [send private reply] at May 23, 2002, 03:20:59 PM

Consider this incomplete class (lacks accessor functions, because they aren't needed to explain the constructor & destructor)

#define MAX_SCORES 500

class scoreTable
{
   public:
      scoreTable();        // constructor
      ~scoreTable();       // destructor
 
   private:
      int count; 
      int *scores;
};
        
scoreTable::scoreTable()
{
   int i;
   scores = new int[MAX_SCORES];
   for (i = 0; i < MAX_SCORES; i++)
   {
     scores[i] = 0;
   }
   count = 0;
}   

scoreTable::~scoreTable()
{
   delete [] scores;
}


This demonstrates the most basic use of a constructor/destructor.
The constructor allocates memory for the array scores and initialises them to zero.
(It does everything needed to build the object)
The destructor frees the memory used by the object when it's destroyed.
(It does everything needed to remove the object)

Because the constructor and the destructor are called automaticly, the actions performed in them can't be forgotten by the programmer using this class and prevents memory leaks (in this particular class) and much more.

The constructor is quite a powerful part of the class. It can be overloaded (See CodeReds example) to procide several sets of parameter lists. However it can't return values.
The destructor is less powerfull, there can be only one (in the class) and it can't return any values or use parameters.

RedX
(I had to add an example because CodeReds post was bigger than mine. :-) )
Posted by CodeRed [send private reply] at May 23, 2002, 04:56:55 PM

Thats okay, I didn't bother doing a destructor anyways, I rarely use them.

Posted by RedX [send private reply] at May 24, 2002, 01:13:48 PM

I use them for cleanup-code and everything that feels right to be put in there. I find it easier to prevent memory leaks this way.

RedX
(After re-reading my post I must admit that a spelling-checker would be a good thing to implent on these forums. Else I'm going to get arrest one day for severe harrasment of the English language)

Posted by CViper [send private reply] at May 24, 2002, 02:50:35 PM

You can also do some nice things with constructors & destructors with static classes, like initializing stuff pre-main/cleaning it up post-main (WinMain for windows apps)

Btw: who cares about spelling? As long as it's readable that is. Wasn't there something about "dynamic spelling"? :)

Posted by CodeRed [send private reply] at May 24, 2002, 06:24:31 PM

If you want people to take you seriously you have to know how to spell. Good grammar doesn't hurt either

Posted by rollic2002 [send private reply] at May 25, 2002, 12:33:27 PM

really thanks guys you helped me alote all of you

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.