What are the use of destructor function in class? What are its characteristics? Can a destructor accept arguments?
Destructor are used to destroy the objects that have been created by a constructor during the execution of C++ program. It has the same name as that of the class but is preceded by a tilde sign.
For example, If A is a class then A() is known as its default constructor and ~A() as its destructor.
class A // Class declaration with class_name A
{
//declaration of data members.
public:
A() // declaration of class constructor
{
statement 1...n;
}
~A() // declaration of class destructor
{
statement 1...n;
}
};
Characteristics of destructor in C++ language:
1. A destructors is executed automatically by the compiler when program terminate.
2. A destructors releases memory space for future use after execution.
3. A destructors never accepts any argument or don't return anything.
4. It is not possible to take the address of a destructor.
5. A destructors can not be inherited in the derived classes.
6. A destructors cannot be overloaded.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: