C++ Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 





Destructors in C++


Tutorials Cpp

Topic

Using Destructors in C++?

How to destroy objects in C++?


Explanation

Destructors are a type of member functions used to destroy the objects of a class created by a "Constructor". The destructors have the same name as the class whose objects are intialized but with a "~" or "tilde" symbol preceding the destructor declaration.

"Destructors" dont take any arguments or neither pass any values. But it is used to free the space used by the program. The C++ compiler calls the destructor implicitly when a program execution is exited.

Example:

   #include <iostream.h>
   int cnt=0;
   class display
     {
       public:
         display()
          {
          	cnt++;
          	cout << "\nCreate Object::" << cnt;
          }
          ~display()
          {
          	cout << "\nDestroyed Object::" << cnt;
            cnt--;
          }
     };
  int main()
    {
      cout << "\nMain Objects x,y,z\n";
      display x,y,z;
         { 
     	 	cout << "\n\nNew object 4\n";
       	display B;
         }
      cout << "\n\nDestroy All objects x,y,z\n";
      return 0;
    }

Result:
   Main Objects x,y,z
   
   Create Object::1   
   Create Object::2
   Create Object::3
   
   New objects 4
   
   Create Object::4   
   Destroyed Object::4
   
   Destroy All objects x,y,z
   Destroyed Object::3
   Destroyed Object::2
   Destroyed Object::1

In the above example both a constructor "display()", destructor "~display()" is used. First three objects x,y,z are created, then a fourth object is created inside "{}". The fourth object is destroyed implicitly when the code execution goes out of scope defined by braces. Finally all the exisiting objects are destroyed.










Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting