0% found this document useful (0 votes)
195 views10 pages

Destructors

1. A destructor is a member function that deletes an object and cleans up its memory. It is called automatically when an object goes out of scope or is explicitly deleted. 2. Destructors have the same name as the class but preceded by a tilde (~), take no arguments, and return no value. Constructors are the inverse of destructors - they initialize objects. 3. Destructors are generally defined as public functions so objects can be destroyed after use. Destructors are invoked in the reverse order of constructor calls when multiple objects are destroyed.

Uploaded by

nihala2302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
195 views10 pages

Destructors

1. A destructor is a member function that deletes an object and cleans up its memory. It is called automatically when an object goes out of scope or is explicitly deleted. 2. Destructors have the same name as the class but preceded by a tilde (~), take no arguments, and return no value. Constructors are the inverse of destructors - they initialize objects. 3. Destructors are generally defined as public functions so objects can be destroyed after use. Destructors are invoked in the reverse order of constructor calls when multiple objects are destroyed.

Uploaded by

nihala2302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Destructors

What is a destructor?
It is a member function which deletes an object.
"Destructor" functions are the inverse of

constructor functions. They are called when


objects are destroyed (de allocated).
Destructor will get executed when an object of the
class goes out of scope i.e its scope gets over.
A destructor cleans up the storage (memory area
of the object) that is no longer accessible.
A destructor has:
(i) the same name as the class but is preceded by a tilde
(~)
(ii) It takes no arguments and return no values .

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.

If more than one object is being destructed ,


the destructors are invoked in the reverse
order in which the constructors were called.
for example :{
Sample s1;
//s1 constructed
Sample s2;
// s2 constructed
Sample s3;
// s3 constructed
.
.
// s3 destructed
.
// s2 destructed
// s1 destructed
}

CONSTRUCTOR

DESTRUCTOR

Constructor is Used to Initialize the Object.

Destructor is used to destroy the object that are


created in memory previously.

Constructor can takes arguments.

Destructor can not take any arguments.

Constructor overloading can be possible means


more than one constructors can be defined in
same class.

Destructor overloading can not be possible.

Constructor has same name as class name.

Destructor has same name as class name with


tilde operator.

Syntax of constructor:

Syntax of Destructor:

class class_name
{

class class_name
{

clas_sname(){}
class_name(argulist){}

};

~class-name( ){}

};

Constructor are of following:

Destructor has only one type.

1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor.

1. Default Destructor.

Answer the question after going through the following program :


class science
{
char Topic [20];
int weightage;
public :
science ( )
// Function 1
{
strcpy ( Topic , optics );
weightage = 30 ;
cout << Topic activated;
}
~ science ( )
// Function 2
{
cout << Topic Deactivated ;
}
};
(1) What is Function 1 and Function 2 ?
(2) When will they executed ?

Find the errors:-

Class ABC
{
int x= 10;
float y
ABC ( )
{
y=5
}
~()
{
cout << Destructor
}
void main ( )
{
ABC a1,a2 ;
}

What will be the output of following code?


#include <iostream.h>
#include <conio.h>
class A
{
public:
A()
{
cout<<constructor A\n;
}
~A( )
{
cout<<Destructor A\n;
}};
class B
{ public :
B ()
{
cout<<constructor B\n;
}
~B( )
{
cout<<Destructor B\n;
} };
class C
{
A ob1 ,ob2;
B ob3 ;
Public:
c()
{
cout<<constructor c\n;
}
~c( )
{
cout<<Destructor c\n;
}};
Void main ( )
{
C oc;
B ob ;
A oa ;
}

Thank You

You might also like