Results 1 to 4 of 4

Thread: Basic use of destructor

  1. #1
    Join Date
    Jan 2009
    Posts
    53

    Basic use of destructor

    Hi friends,

    I am aware about the basic use of constructors. But I don't know about the destructror.

    What is the use of destructor ?Is use of destructor is similar to the constructor?

    Please give me solution over this confusion....

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Basic use of destructor

    Below are some information which I know regarding destructor

    1. A the use of destructor is exactly opposite to constructor.

    2. The destructor is used to release the constructors.

    3. A destructor is a method of a class which executed when the class is deleted.

    4. In other word It performs cleanup of the members of the class which need cleanup process ,

    e.g Deallocation of objects referred to pointer within the class.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Basic use of destructor

    Please go through the following example which shows the use of the destructor :

    Program code:

    #include <iostream>
    using namespace std;

    class myclass {
    int a;
    public:
    myclass(); // constructor
    ~myclass(); // destructor
    void show();
    };

    myclass::myclass()
    {
    cout << "In constructor\n";
    a = 10;
    }
    myclass::~myclass()
    {
    cout << "Destructing...\n";
    }

    void myclass::show()
    {
    cout << a << endl;
    }

    int main()
    {
    myclass ob;

    ob.show();

    return 0;
    }

    Hope, it will help you

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Basic use of destructor

    Destructors is basically used to deallocate memory allocated by constructor. And also to do other cleanup for a class object and members when the object is deleted.
    Destructor is called when the object of class passes out of scope OR deleted.

    Destructor is a member function which has similar name as its class prefixed by a '~' .

    e.g:
    class A {
    public:

    A();
    // Constructor for class A

    ~A();
    // Destructor for class A

    };

Similar Threads

  1. Replies: 3
    Last Post: 28-08-2012, 12:05 PM
  2. Problem in LinkedList destructor.
    By KAIRU26 in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 07:14 PM
  3. What is the difference between a destructor and a finalizer?
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 10:47 AM
  4. Destructor in PHP5
    By Duck in forum Software Development
    Replies: 3
    Last Post: 18-03-2009, 12:01 AM
  5. Destructor in Java
    By Anti_Trend in forum Software Development
    Replies: 7
    Last Post: 25-10-2008, 03:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,700,932.13270 seconds with 17 queries