C++ Destructor And Difference Between Constructor And Destructor

C++ Destructor

Destructors:

C++ Destructor– When an object is destroyed, a special member function of that class is executed automatically. This member function is called the destructor function or destructor. The C++ destructor function has the same name as the name of a class but a tilde sign (~) is written before its name. it is executed automatically when an object comes to the end of its life.

Like constructors, C++ destructors do not return any value. They also do not take any arguments.

For example, a local object is destroyed when all the statements of the function in which it is declared are executed. So at the end of the function, the C++ destructor function is executed.

Similarly, global objects (objects that are declared before the main function) or static objects are destroyed at the end of the main function. The lifetime of these objects ends when the program execution ends. So at the end of the program, the destructor function is executed. The C++ destructor functions are commonly used to free the memory that was allocated for objects.



The following example explains the concept of constructors and destructors.

Example how to use C++ constructor and C++ destructor:

Output:


Passing objects as Arguments in C++ Destructor:

Objects can also be passed as argument to member function. When an object is passed as an argument to a member function:

  • Only the name of the object is written in the argument.
  • The number of parameters and their types must be defined in the member function to which the object is to be passed. The object that are passed are treated local for the member function and are destroyed when the control returns to the calling function.

Example how to use passing objects as arguments in c++ destructor:

Output:


Programming Explanation:

In the above program, the class “data” has one data member “name” of string type and two member function “get_data” and “show_data”. The  “show_data” function has an argument of class “data” type.

The object “temp” and “abc” are declared of class “data”. The member function gets the name in object “temp” and store it into the data member “name”.  the member function “show_data” for object “abc” is called by passing argument of object “temp” when the control shifts to the member function “show_data” a copy of “temp” is created as a local object in the show_data function with name “s”



Another Example:

For example, a class called data will have the destructor ~data(). Additionally, C++ destructors have neither parameters nor return types. As stated above, a C++ destructor for an object is called whenever the object’s lifetime ends. If the object was created as an automatic variable, its lifetime ends and the C++ destructor is called automatically when the object goes out of scope. Because C++ does not have garbage collection, if the object was created with a new statement (dynamically on the heap), then its destructor is called when the delete operator is applied to a pointer to the object. Usually that operation occurs within another C++ destructor, typically the destructor of a smart pointer object.

In inheritance hierarchies, the declaration of a virtual destructor in the base class ensures that the C++ destructors of derived classes are invoked properly when an object is deleted through a pointer-to-base-class. Objects that may be deleted in this way need to inherit a virtual destructor.

A C++ destructor should never throw an exception

Output:


Difference Between Constructors And Destructors:

Constructors:

A constructor, in programming, allocates the amount of memory to the program that the object needs to take up for as long as it is useful. It essentially constructs a new object in memory…and can maybe set values in the object, do some logic, etc. similar to any method. This is a pretty standard fixture in most object-oriented languages, such as Java or c++.

  • A Constructor is a special function that gets called automatically when the object of a class is created
  • A constructor is used to initialize the instance of a class.
  • A constructor can have arguments.
  • Constructor overloading can be possible means more than one constructor can be defined in the same class. e.g. default, parameterized, and copy constructor.
  • The constructor has the same name as the class name.
  • Constructors can be used to dynamically allocate memory.
  • Syntax of the constructor:

class OOP
{
OOP(){}
OOP(argulist){

//Body of Constructor

}
};

Destructors:

A C++ destructor is the opposite, it “frees” the memory used by your program for that object by deleting its values in memory and unallocating it to the application. It destructs (or unconstructs) the object from memory. This is not such a common fixture in object-oriented languages because much higher level ones have built-in data management schemes, so it automatically destructs objects as needed.

  • Destructor is used to destroy the object that is created in memory.
  • C++ destructor can not take arguments.
  • C++ destructor overloading is not possible.
  • C++ destructor also has the same name as class name but with (~) tiled operator.
  • C++ destructor releases the memory.
  • Destructor is called when an instance of a class is deleted or released.
  • Syntax of C++ destructor:

class OOP
{

~OOP(){}
};



Virtual Destructors

  • A C++ destructor can be declared to be virtual, and usually should be for a class with a virtual function.

  • The reason we need a virtual destructor is that an object usually manipulated through the interface provided by a base class is often also deleted through that interface like

 

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: