Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!




Destructors


  • Destructor is defined as a special member function defined in public scope of the class.
  • Destructor, as the name suggests is used to destroy the objects created by a constructor.
  • Like constructor, the destructor also has the same name as that of the class with a prefix of tilde(~) sign.
  • Destructor function is called automatically by c++ whenever an object is getting destroyed.
  • A destructor never takes any argument nor does it return any value.
  • Destructor is used for garbage collection. It is also used for closing any open files.

    Following example shows the use of Destructor Function:

    #include < iostream.h >
    class Test
    {
    public:
    Test();
    ~Test();
    };
    Test :: Test()   //here is the definition of constructor
    {
    cout<<"Constructor of class Test called: << endl;
    }
    Test :: ~Test()   //here is the definition of destructor
    {
    cout<<"Destructor of class test called" << endl;
    }
    void main()
    {
    Test x;   //constructor is called while creating
    cout<<"terminating main()' << ;
    }   //object x goes out of scope, destructor is called

    OUTPUT:

    Constructor of class Test called
    Terminating main()
    Destructor of class test called






    CACKLE comment system





  • Programming Resources
    Computer Networking Fundamentals Android Application