Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-11-2009
Member
 
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....
Reply With Quote
  #2  
Old 19-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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.
Reply With Quote
  #3  
Old 19-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Basic use of destructor

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

Program code:

Quote:
#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
Reply With Quote
  #4  
Old 19-11-2009
Member
 
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:
Quote:
class A {
public:

A();
// Constructor for class A

~A();
// Destructor for class A

};
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Basic use of destructor"
Thread Thread Starter Forum Replies Last Post
Dinobot Destructor Pack and Massive Fury Pack DLC for Transformers: Fall of Cybertron cech Video Games 3 28-08-2012 12:05 PM
Problem in LinkedList destructor. KAIRU26 Software Development 5 15-02-2010 07:14 PM
What is the difference between a destructor and a finalizer? Taylor D Software Development 5 27-01-2010 10:47 AM
Destructor in PHP5 Duck Software Development 3 18-03-2009 12:01 AM
Destructor in Java Anti_Trend Software Development 7 25-10-2008 03:27 PM


All times are GMT +5.5. The time now is 09:37 AM.