Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Destructor and Its Implementation

A destructor in C++ is a special member function of a class that is


executed whenever an object of that class's type goes out of scope or is
explicitly deleted. The destructor is used to release resources that the
object may have acquired during its lifetime. A key feature of
destructors is that they have the same name as the class but are
preceded by a tilde (~).

Key Points About Destructors


1. No Parameters and No Return Type: Destructors cannot have
parameters and do not return any value.
2. One Destructor per Class: There can only be one destructor per
class.
3. Automatic Invocation: The destructor is automatically called
when an object goes out of scope or is deleted.

Basic Syntax
class ClassName {
public:
~ClassName() {
// Destructor code
cout<<”This is destructor.”<<endl;
}
};
Properties of C++ Destructor

1. When objects are destroyed, the destructor function is


automatically named.
2. It's not possible to declare it static or const.
3. There are no arguments for the destructor.
4. It doesn't even have a void return form.
5. A member of the union cannot be an entity of a class with a
destructor.
6. In the public section of the class, a destructor should be declared.
7. The destructor's address is inaccessible to the programmer.

Rules for C++ Destructors

1. According to C++ destructor the statement should begin with a


tilde () and the same class name.
2. Destructors are not equipped with parameters or a return form.
3. Destructors are invoked automatically and cannot be invoked
manually from a program.
4. Destructors cannot be overloaded.
5. Which can be a virtual destructor.
6. The Destructor will execute the reverse order of object creation.
7. Destructor always present only in the public section.

Below is the example provided in which we just demonstrate the


creation and destruction of an object.
Example

Output
Example explanation: This destructor will print the message
"Destructor called!" when it is invoked, which happens
automatically when the object obj of class OOP goes out of scope.
Although this example does not involve resource allocation, in a
more complex class, the destructor would handle the cleanup of
resources.
The destructor ensures that any final actions are taken before the
object is completely removed from memory.

Why Use Destructors?

Resource Management: If your class allocates resources like


memory, file handles, or network connections, you need a
destructor to release these resources when the object is no longer
needed.
Avoid Memory Leaks: Without a destructor to free memory, your
program could run out of memory.

Conclusion

C++ destructors are class members that remove an object. They


are named when the class object is no longer in view, for example,
when a method, a program, or a delete variable is called.
Destructors vary from member functions in that they do not
accept any arguments and do not return anything. Destructors are
also given the same name as their class.

You might also like