Example of Destructors


/***********************************************************************
*

The following example shows the use of destructors:

                                                                        *
************************************************************************/

#include <string.h>
class Y
{
private:
      char * string;
      int number;
public:
      Y(const char* n,int a);   // constructor
      ~Y() { delete[] string; } // destructor
};
Y::Y(const char* n, int a)      // define class Y constructor
{
      string = strcpy(new char[strlen(n) + 1 ], n);
      number = a;
}
void main ()
{
      Y yobj = Y("somestring", 10); // create and initialize
                                    // object of class Y
// ...
// destructor ~Y is called before control returns from main()
}