What is the use of Destructors
Just like constructors are used to allocate memory to an object, a destructor is used to deallocate or delete the objects. A destructor, like a constructor is also a special function whose primary function is to deallocte the memory occupied by an object. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name but are prefixed with the "~" sign called the "tilda" operator or simply the "wave" sign. A destructor is called just before the closure of a program.
Normally, you don't need to program or define a constructor since operating system now a days are capable enough to delete the objects.
General Syntax of Destructors
~ classname();
The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.
Properties
Properties
- Destructors take the same name as the class name.
- Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.
- The Destructor does not take any argument which means that destructors cannot be overloaded.
- No return type is specified for destructors.
0 comments:
Post a Comment