Tell me properties of destructor in C++ as many as you can.

Answer includes properties / characteristics of destructor in C++ language for a class.

Answer:

Before listing characteristics of destructor, let’s see the declaration of destructor in  a class. Below ~MyClass() is the destructor of a class.

class MyClass{

public:
	MyClass(){}//Constructor

	~MyClass(){}//Destructor
};

Properties of destructor in C++:
  • It is called automatically when class object goes out of scope or when delete an object of a class. And cannot be invoked explicitly. ( calling delete can be consider as explicit destructor call, as whenever we call delete on object, destructor get called. but directly, destructor cannot be called.)
  • It does not receive any parameter, so, cannot be overloaded.
  • Destructors cannot be overridden and can’t inherit them.
  • Any class can have at most one destructor.
  • Destructors can be a virtual to maintain the hierarchy of destructors call in inheritance for polymorphic classes. Polymorphic class means, a class having at least one virtual function.

Related Posts