What is a Destructor in C++

Estudies4you
Q20. What is a Destructor? Explain.
Answer: 

Destructor
A destructor is a special member function that is used to destroy the objects created by constructor. It takes the same name as the class but with a ‘tilde’ (~) at the beginning. It doesn’t have any return type. It is called automatically at the end of program execution to free up the acquired storage.
Memory allocation is done by using new operator in a constructor whereas delete operator is used in a destructor to deallocate the previously allocated memory.

Example
#include<iostream.h>
#include<conio.h>
class Demo
{
private:
int data;
public:
Demo( )
{
cout<<endl<<“Inside constructor”;
}
~Demo( )
{
cout<<endl<<“Inside destructor”:
}
};
void main( )
{
Demo d;
getch( );
}


Output
Constructors and Destructors in C++,define Constructor in c++,syntax for Constructor in c++,use of Constructor in c++,types of Constructors in c++,Default constructor in c++,Parameterized constructor in c++,copy Parameterized constructor in c++,Constructor with default arguments in c++,examples of constructors in c++,define destructor in c++,difference between constructor and destructor in c++,Explicit Constructor Calling in c++, Implicit Constructor Calling in c++,types of Parameterized Constructor in c++,use of constructors in c++,define class in c++,syntax for class,class syntax,data abstraction in c++,data hiding in c++,what is object,difference between class and object,how to declare a object to class,what is scope of class in c++,types of scopes in c++,estudies4you,pointers in c++,static members of a class in c++,Static Member Function in c++, constructor and destructors in c++,difference between constructor and destructor,define constructor,define destructor,static member functions,data abstraction definition,short notes on data abstraction,types of abstraction in c++,what is ADT in c++,abstract data type in c++,information hiding in c++,

To Top