← Back to C++ Course | Chapter 8: OOP Core | Lesson 7 of 14

C++ के Destructors

एक destructor वह clean-up step है जो एक object चले जाने पर चलता है, एक room से निकलते समय lights बंद करने जैसा। यह जो भी object इस्तेमाल कर रहा था उसे release करता है।
Syntax
cpp
class ClassName {
public:
  ~ClassName() {
    // cleanup code, runs when the object is destroyed
  }
};

Destructor क्या है?

एक destructor एक special member function है जो एक object scope से बाहर जाने या explicitly delete होने पर automatically चलता है।

यह class का नाम एक tilde (~) prefixed के साथ share करता है, कोई arguments नहीं लेता, और कोई return type नहीं है, और इसका काम यह clean करना है कि object क्या hold कर रहा था।

उदाहरण: What is a Destructor?

cpp
#include <iostream>

class Box {
public:
	~Box() { std::cout << "Box destroyed" << std::endl; }
};

int main() {
	Box b;
	return 0;
} // destructor runs here

Destructors और Resource Cleanup

Destructors एक object के own किए resources release करने के लिए essential हैं — सबसे common new से allocated memory।

अगर आप destructor के अंदर वह memory free नहीं करते, यह उस class के हर object destroy होने पर leak करती है, क्योंकि कुछ भी इसे कभी reclaim नहीं करेगा।

उदाहरण: Destructors and Resource Cleanup

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Buffer
class Buffer {
	int *data;
// Members below can be accessed from outside the class
public:
	Buffer() { data = new int[10]; }
	~Buffer() { delete[] data; std::cout << "Memory freed" << std::endl; }
};

// Program execution starts in main()
int main() {
	Buffer b;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Implicit बनाम Explicit Destructors

अगर आप खुद एक destructor नहीं लिखते, compiler automatically एक implicit एक देता है।

वह default simple classes के लिए ठीक है, लेकिन जिस moment आपकी class heap memory या एक file handle जैसे एक raw resource manage करे, आपको इसे सही तरीके से release करने के लिए एक explicit destructor लिखना ज़रूरी है।

उदाहरण: Implicit vs Explicit Destructors

cpp
#include <iostream>

class Simple {
	int value = 5; // no destructor written: compiler generates an implicit one
};

int main() {
	Simple s;
	std::cout << "No custom cleanup needed" << std::endl;
	return 0;
}

Destructor Order of Execution

जब कई objects साथ destroy होते हैं, जैसे local variables एक function के अंत में scope से बाहर जाना, वे उनकी creation के exact reverse order में destroy होते हैं — बनाया गया last object हमेशा destroy होने वाला पहला है, यह mirror करते हुए कि एक stack कैसे unwind होता है।

उदाहरण: Destructor Order of Execution

cpp
#include <iostream>

class Item {
	int id;
public:
	Item(int i) : id(i) {}
	~Item() { std::cout << "Destroying item " << id << std::endl; }
};

int main() {
	Item a(1);
	Item b(2);
	return 0; // b destroyed first, then a (reverse of creation order)
}

Pointers के साथ Destructors

new से heap पर बनाए objects scope से बाहर जाने पर automatically destroy नहीं होते, क्योंकि एक raw pointer scope से बाहर जाना सिर्फ pointer को destroy करता है, जिसकी ओर यह point करता है उसे नहीं।

डेस्ट्रक्टर trigger करने और वह memory free करने के लिए आपको explicitly delete call करना होगा।

उदाहरण: Destructors with Pointers

cpp
#include <iostream>

class Box {
public:
	~Box() { std::cout << "Box destroyed" << std::endl; }
};

int main() {
	Box *ptr = new Box();
	delete ptr; // must delete manually -- a raw pointer going out of scope does NOT call the destructor
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. obj.~Class() से manually एक destructor call करना, जो object के scope से बाहर जाने पर इसे दो बार चलने की ओर ले जाता है।
  2. constructor में new से allocated memory को delete करना भूल जाना, एक leak cause करते हुए।
  3. base destructor virtual न होने पर एक base pointer के जरिए एक derived object delete करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.