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....
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....
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.
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
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
};
Bookmarks