Destructor in C++ - Introduction, Syntax and Characteristics

A destructor in C++ is a special member function of a class that is automatically called when an object is destroyed. Its primary purpose is to perform cleanup operations, such as releasing dynamically allocated memory and other resources acquired by an object. Just as a constructor initializes an object when it is created, a destructor performs cleanup when the object's lifetime ends.

C++ में Destructor एक special member function होता है, जो object destroy होने पर automatically call होता है। इसका मुख्य कार्य cleanup operations करना होता है, जैसे dynamically allocated memory और अन्य resources को release करना।

1. What is a Destructor in C++?

A destructor is a special member function that is automatically invoked when an object is destroyed. It is mainly used to release resources that were acquired during the lifetime of an object. For example, if a class allocates memory dynamically using new, its destructor can release that memory using delete.

Definition: A destructor is a special member function of a class that is automatically called when an object is destroyed to perform necessary cleanup operations.

C++ में Destructor object के lifetime के अंत में automatically execute होता है। इसका उपयोग object द्वारा उपयोग किए गए resources को release करने के लिए किया जाता है।

Syntax of Destructor

class ClassName {
public:
~ClassName() {
// Cleanup statements
}
};

In this syntax:

  • ClassName is the name of the class.

  • The tilde (~) symbol is written before the class name.

  • The destructor has the same name as its class, preceded by ~.

  • It does not have a return type.

  • It does not accept any parameters.

Destructor के नाम से पहले tilde (~) symbol लगाया जाता है। इसका नाम class के नाम के समान होता है, लेकिन इसके पहले tilde लगाया जाता है।

2. Example of Destructor in C++

The following program demonstrates how a destructor is automatically called when an object goes out of scope.

C++ Program

#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Constructor Called" << endl;
}
~Demo() {
cout << "Destructor Called" << endl;
}
};
int main() {
cout << "Entering main function" << endl;
Demo obj;
cout << "Object is working" << endl;
cout << "Leaving main function" << endl;
return 0;
}

Output

Entering main function
Constructor Called
Object is working
Leaving main function
Destructor Called

Explanation

In this program:

  1. The Demo class contains a constructor and a destructor.

  2. When object obj is created, the constructor is automatically called.

  3. The message "Object is working" is displayed.

  4. When execution leaves the scope of main(), the local object obj is destroyed.

  5. Its destructor is automatically called.

इस program में obj object create होने पर constructor call होता है। जब main() function समाप्त होता है, तब local object destroy होता है और उसका destructor automatically execute होता है।

3. Characteristics of Destructors in C++

A destructor has several special characteristics that distinguish it from ordinary member functions.

1. Same Name as the Class

A destructor has the same name as its class, but a tilde (~) symbol is placed before the name.

Example:

class Student {
public:
~Student() {
cout << "Student object destroyed";
}
};

Destructor का नाम class के नाम के समान होता है, लेकिन उसके पहले tilde (~) लगाया जाता है।

2. No Return Type

A destructor does not have any return type, including void.

Example:

~Student() {
cout << "Destructor Called";
}

Destructor में void, int या कोई अन्य return type नहीं लिखा जाता।

3. No Parameters

A destructor cannot accept parameters.

The following declaration is invalid:

~Student(int x);  // Invalid

Destructor को कोई parameter नहीं दिया जा सकता।

4. Automatically Called

A destructor is automatically invoked when an object reaches the end of its lifetime.

For example:

#include <iostream>
using namespace std;
class Demo {
public:
~Demo() {
cout << "Object Destroyed" << endl;
}
};
int main() {
Demo d1;
return 0;
}

Output:

Object Destroyed

जब object का lifetime समाप्त होता है, तब उसका destructor automatically call होता है।

5. Only One Destructor Per Class

A class can have only one destructor. Unlike constructors, destructors cannot be overloaded because they cannot accept parameters.

एक class में केवल एक destructor declaration हो सकता है। Constructors की तरह destructors को overload नहीं किया जा सकता।

6. Used for Resource Cleanup

Destructors are commonly used to release resources such as:

  • Dynamically allocated memory

  • File handles and open files

  • Network connections

  • Locks and other acquired resources

Destructor का उपयोग object द्वारा acquire किए गए resources को release करने के लिए किया जाता है।

7. Can Be Virtual

A destructor can be declared as virtual. Virtual destructors are particularly important when objects of derived classes are deleted through base-class pointers.

Destructor को virtual declare किया जा सकता है। Inheritance और polymorphism में इसका विशेष महत्व होता है।

8. Cannot Be Static

A destructor cannot be declared as a static member function because it is associated with the destruction of an object.

Destructor को static declare नहीं किया जा सकता।

9. Can Be Public, Protected or Private

A destructor can have different access specifiers. However, its accessibility affects where and how an object can be destroyed.

Destructor को public, protected या private बनाया जा सकता है। इसका access specifier object destruction की accessibility को प्रभावित करता है।

10. Compiler Can Generate a Destructor

If a class does not define its own destructor, the compiler generally implicitly declares a destructor for it. The generated destructor performs the necessary destruction of its members and base classes.

यदि programmer destructor define नहीं करता, तो compiler सामान्यतः implicit destructor provide करता है। यह class के members और base classes के destruction का कार्य करता है।

4. When is a Destructor Called in C++?

A destructor is called automatically in different situations depending on the object's storage duration and lifetime.

1. When a Local Object Goes Out of Scope

A local object is destroyed when execution leaves the block in which it was created.

Example:

#include <iostream>
using namespace std;
class Demo {
public:
~Demo() {
cout << "Destructor Called" << endl;
}
};
int main() {
{
Demo obj;
cout << "Inside Block" << endl;
}
cout << "Outside Block" << endl;
return 0;
}

Output:

Inside Block
Destructor Called
Outside Block

The destructor is called when the inner block ends, not when the entire main() function ends.

जब execution inner block से बाहर आता है, तब उस block में बना object destroy हो जाता है और destructor call होता है।

2. When a Dynamically Created Object is Deleted

An object created using new is normally destroyed when the corresponding delete expression is executed.

Example:

#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Constructor Called" << endl;
}
~Demo() {
cout << "Destructor Called" << endl;
}
};
int main() {
Demo* ptr = new Demo;
cout << "Deleting Object" << endl;
delete ptr;
return 0;
}

Output:

Constructor Called
Deleting Object
Destructor Called

Here, delete ptr destroys the dynamically allocated object and invokes its destructor.

जब new से object बनाया जाता है, तो वह सामान्यतः तब तक जीवित रहता है जब तक उसे corresponding delete expression से destroy नहीं किया जाता।

3. When a Program Ends

Objects with static storage duration are destroyed during program termination, subject to the applicable initialization and destruction rules.

Example:

#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Constructor Called" << endl;
}
~Demo() {
cout << "Destructor Called" << endl;
}
};
Demo obj;
int main() {
cout << "Inside Main" << endl;
return 0;
}

Output:

Constructor Called
Inside Main
Destructor Called

Here, obj is a global object with static storage duration. Its constructor runs before main() begins, and its destructor runs during normal program termination.

Global और static storage duration वाले objects का destruction सामान्य program termination के दौरान होता है।

5. Difference Between Constructor and Destructor in C++

Constructors and destructors are both special member functions of a class, but they perform opposite responsibilities.

ConstructorDestructor
Initializes an object.Performs cleanup when an object is destroyed.
Has the same name as the class.Has the same name as the class preceded by ~.
Does not have a return type.Does not have a return type.
Can accept parameters.Cannot accept parameters.
Can be overloaded.Cannot be overloaded.
Is invoked during object initialization.Is invoked when the object's lifetime ends.
Can be declared with different access specifiers.Can also have different access specifiers.
Cannot be virtual.Can be virtual.
Cannot be static.Cannot be static.
A class may have multiple constructors.A class has only one destructor.

Example:

class Student {
public:
Student() {
// Constructor
}
~Student() {
// Destructor
}
};

Constructor object creation और initialization के लिए उपयोग होता है, जबकि Destructor object destruction और cleanup के लिए उपयोग होता है।

6. Advantages of Destructors in C++

Destructors provide several benefits in C++ programming:

  1. Automatic cleanup: They execute automatically when an object's lifetime ends.

  2. Memory management: They can release dynamically allocated memory.

  3. Resource management: They help release files, connections and other resources.

  4. Better program organization: Cleanup code can be placed inside the class itself.

  5. Resource safety: They form the foundation of RAII (Resource Acquisition Is Initialization), an important C++ resource-management technique.

Destructors automatic cleanup, memory management और resource management को आसान बनाते हैं। RAII के माध्यम से C++ programs में resources को सुरक्षित तरीके से manage किया जा सकता है।

7. Frequently Asked Questions (FAQs)

Q1. What is a destructor in C++?

A destructor is a special member function that is automatically called when an object is destroyed. It is used to perform cleanup operations.

Destructor एक special member function है, जो object destroy होने पर automatically call होता है।

Q2. What is the symbol of a destructor in C++?

A tilde (~) symbol is used before the class name to declare a destructor.

Destructor के नाम से पहले tilde (~) symbol लगाया जाता है।

Q3. Can a destructor have parameters?

No. A destructor cannot accept parameters.

नहीं, Destructor में parameters नहीं दिए जा सकते।

Q4. Can destructors be overloaded in C++?

No. A class can have only one destructor because destructors cannot accept parameters.

नहीं, Destructor को overload नहीं किया जा सकता।

Q5. When is a destructor automatically called?

A destructor is called when an object's lifetime ends, such as when a local object goes out of scope or a dynamically allocated object is deleted.

Object का lifetime समाप्त होने पर destructor call होता है।

Q6. Can a destructor be virtual?

Yes. A destructor can be virtual. It is especially important when deleting derived-class objects through base-class pointers.

हाँ, Destructor को virtual बनाया जा सकता है। इसका उपयोग inheritance और runtime polymorphism में महत्वपूर्ण होता है।

Q7. Is a destructor necessary in every C++ class?

No. If a class does not require custom cleanup, the compiler-generated destructor is generally sufficient.

नहीं, हर class में programmer द्वारा destructor लिखना आवश्यक नहीं है। यदि custom cleanup की आवश्यकता नहीं है, तो compiler-generated destructor पर्याप्त हो सकता है।

Q8. Can we call a destructor explicitly?

A destructor can be invoked explicitly in certain situations, but doing so on an ordinary object without carefully managing its lifetime can cause undefined behaviour if it is later destroyed again. For dynamically managed storage, explicit destruction must be coordinated with storage release.

Destructor को explicitly call किया जा सकता है, लेकिन ऐसा करने पर object के lifetime और दोबारा destruction होने की संभावना का ध्यान रखना आवश्यक है।

A destructor is an essential feature of C++ that helps manage the lifetime of objects and release resources when they are no longer needed. It is automatically invoked when an object's lifetime ends and plays an important role in memory management and RAII. Understanding destructors is necessary for learning advanced C++ concepts such as dynamic memory allocation, inheritance, virtual destructors and object lifetime.

C++ में Destructor object के lifetime के अंत में cleanup operations perform करता है। यह memory और अन्य resources को manage करने में महत्वपूर्ण भूमिका निभाता है। Destructors की अच्छी समझ Advanced C++ और Object-Oriented Programming सीखने के लिए आवश्यक है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Course - OOP and Modern C++

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)