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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: