Destructors
Destructors
What is a destructor?
It is a member function which deletes an object.
"Destructor" functions are the inverse of
For example :The destructor for the class Sample will bear the
name ~ Sample().
Here the symbol ~ refers to a NOT i.e a
destructor is a NOT constructor.
For Example:Class Sample
{
int i , j ;
public:
Sample (int a, int b) // constructor
{i=a ; j=b;}
~ Sample ()
{
cout<<destructor at work \n; } };
int main ( )
{ Sample s1 (3,4); // object S1 constructed with
values 3 &4 Sample ( ) constructer
.
.
. // automatically s1 is destructed at the end of
the block using destructor ~ Sample ( )
}
Generally a destructor should be defined under
the public section of a class, so that its objects
can be used and then destroyed in any
function.
dtor is a abbreviation for destructors.
If you fail to define a destructor for a class the
compiler automatically generates one.and that
destructor is called default destructor.
CONSTRUCTOR
DESTRUCTOR
Syntax of constructor:
Syntax of Destructor:
class class_name
{
class class_name
{
clas_sname(){}
class_name(argulist){}
};
~class-name( ){}
};
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor.
1. Default Destructor.
Class ABC
{
int x= 10;
float y
ABC ( )
{
y=5
}
~()
{
cout << Destructor
}
void main ( )
{
ABC a1,a2 ;
}
Thank You