An object of a class with a destructor cannot become a member of the union

A destructor is a special member function of a class that is called if an object of that class gets out of scope or when the delete expression is used on a pointer to that class's object. A destructor will have the same name as the class, but it will be prefixed with a tilde [], and it will not be able to return any value or take any parameters. Destructor can be extremely useful for releasing resizable objects.

Destructor is useful for releasing resources before exiting a program, such as closing files and releasing memories.

 Use of C++ Destructor

  • Object memory getting released.
  • The pointer variables' memory getting released.
  • Files and services getting closed.

 C++ Destructor Syntax

Class Name_of _class {

public:

 ~Name_of_class[] //this is known as Destructor

   {

   }

}

Explanation

~Name_of_class[] //this is known as Destructor: in this statement [~] followed by name of the class. This [~] symbol tilled represents destructor

Example Source Code:

// simple destructor program in C++

#include

class desprogram

{       

         public:

         ~desprogram[]

{

    std::cout

Chủ Đề