C++ destructors

C++ Destructors

A Destructor, as the name implies, it is used to destroy the objects that have been created already by a constructor. same Like a constructor, and The destructor is also a member function, whose name is the same as the class name but it is preceded by a tilde symbol let us take example of the destructor for the class integer can be defined as shown in below

~integer()
{
}

A Destructor never takes any arguments and it is also not return any value. it will be invoked implicitly by the compiler on exit from the program or(block or function as the case may be) to delete(clean up) storage that is no longer accessible. It is a good practice to declare destructors in the programs since it releases memory space for the future use.

when new operator is used to allocate memory in the constructors. We should use delete operator to free(delete) that memory. for example the destroy fro the matrix class discussed above may be defined as follows

Student::~Student()
{
for(int i=0;i < d1;i++)
{
delete p[i];
delete p;
}

This is required because when the pointers to objects go out of scope, a destructor is not called implicitly

The example below show that the destructor has been invoked implicitly by the compiler

#include <iostream>
using namespace std;
int count=0;
class ABC
{
public:
ABC()
{
count++;
std::cout<< "\n number of object created" <<count;
}
~ABC()
{
std::cout<< "\n No. of objects Destroy" << count;
count--;
}
};
int main()
{
std::cout<< "\n\n Enter MAIN \n";
ABC a1,a2,a3,a4;
{
std::cout<< "\n Enter Block1 \n";
ABC a5;
}
{
std::cout<< "\n Enter Block2 \n";
ABC a6;
}
std::cout<< "\n\n Re-enter MAin";
}

The output: ENTER MAIN
no. of object created 1
no. of object created 2
no. of object created 3
no. of object created 4



Enter Block1
no. of object created 5
no. of object destroy 5
Enter Block2
no. of object created 6
no. of object destroy 6


RE-ENTER MAIN
no. of object destroy 4
no. of object destroy 3
no. of object destroy 2
no. of object destroy 1

Note: As the objects are created and destroyed, they increase the count, notice that after the first group of objects is created, A5 is created and then destroy, A6 is created and then destroyed. Finally the rest of the objects are also destroyed. When the closing brace of scope is encountered the destroyed of each object in the scope are called.
Note that the object are destroyed in the reverse order of creation.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise