📌 Destructor in C++

 


🔹 What is a Destructor?

A destructor is a special member function of a class that is automatically called when an object goes out of scope or is deleted.
🔹 Same name as the class, but prefixed with ~ (tilde).
🔹 No parameters and no return type.
🔹 Used to release resources (e.g., memory allocation).


📌 Example 1: Basic Destructor Example

🚀 Approach

  1. Define a class Demo with a constructor and a destructor.
  2. The destructor is automatically called when the object is destroyed.

🔹 C++ Code

#include <iostream>
using namespace std;

class Demo {
public:
    // Constructor
    Demo() {
        cout << "Constructor Invoked!" << endl;
    }

    // Destructor
    ~Demo() {
        cout << "Destructor Invoked!" << endl;
    }
};

int main() {
    Demo obj;  // Constructor called automatically
    return 0;  // Destructor called automatically at the end of scope
}

🔹 Output

Constructor Invoked!
Destructor Invoked!

📝 Explanation:

  • The constructor is called when the object obj is created.
  • The destructor is called automatically when obj goes out of scope.

📌 Example 2: Destructor with Dynamic Memory Allocation

🚀 Approach

  1. Allocate memory dynamically using new in the constructor.
  2. Free memory using delete in the destructor.

🔹 C++ Code

#include <iostream>
using namespace std;

class Student {
private:
    string* name;

public:
    // Constructor: Allocates memory
    Student(string n) {
        name = new string(n);
        cout << "Memory allocated for " << *name << endl;
    }

    // Destructor: Releases memory
    ~Student() {
        cout << "Memory deallocated for " << *name << endl;
        delete name;
    }
};

int main() {
    Student s1("Alice");
    Student s2("Bob");
    return 0;  // Destructor called automatically
}

🔹 Output

Memory allocated for Alice
Memory allocated for Bob
Memory deallocated for Alice
Memory deallocated for Bob

📝 Explanation:

  • Memory is allocated using new in the constructor.
  • Memory is released using delete in the destructor.

📌 Example 3: Destructor in a Class with Multiple Objects

🚀 Approach

  1. Create multiple objects in a block scope.
  2. Destructor will be called automatically when objects go out of scope.

🔹 C++ Code

#include <iostream>
using namespace std;

class Test {
public:
    Test() {
        cout << "Constructor Called" << endl;
    }

    ~Test() {
        cout << "Destructor Called" << endl;
    }
};

int main() {
    cout << "Creating objects..." << endl;
    {
        Test t1, t2;
    } // Objects go out of scope, destructor called
    cout << "Objects destroyed!" << endl;
    return 0;
}

🔹 Output

Creating objects...
Constructor Called
Constructor Called
Destructor Called
Destructor Called
Objects destroyed!

📝 Explanation:

  • t1 and t2 go out of scope at the end of {} block.
  • Destructor is called for both objects.

📌 Example 4: Destructor in an Array of Objects

🚀 Approach

  1. Create an array of objects.
  2. The destructor is called for each object when the program ends.

🔹 C++ Code

#include <iostream>
using namespace std;

class Example {
public:
    Example() {
        cout << "Constructor Called" << endl;
    }

    ~Example() {
        cout << "Destructor Called" << endl;
    }
};

int main() {
    Example arr[3]; // Creates an array of 3 objects
    return 0; // Destructor called for each object
}

🔹 Output

Constructor Called
Constructor Called
Constructor Called
Destructor Called
Destructor Called
Destructor Called

📝 Explanation:

  • The constructor is called 3 times for the array.
  • The destructor is called 3 times when objects go out of scope.

📌 Example 5: Destructor in Inheritance

🚀 Approach

  1. A derived class inherits from a base class.
  2. Destructor of the derived class is called first, then the base class destructor.

🔹 C++ Code

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        cout << "Base Constructor Called" << endl;
    }
    
    ~Base() {
        cout << "Base Destructor Called" << endl;
    }
};

class Derived : public Base {
public:
    Derived() {
        cout << "Derived Constructor Called" << endl;
    }

    ~Derived() {
        cout << "Derived Destructor Called" << endl;
    }
};

int main() {
    Derived obj;
    return 0; // Destructor of Derived -> Base called
}

🔹 Output

Base Constructor Called
Derived Constructor Called
Derived Destructor Called
Base Destructor Called

📝 Explanation:

  • Constructors are called in order: Base -> Derived.
  • Destructors are called in reverse: Derived -> Base.

📌 Summary

Example Description
Basic Destructor Example Destructor automatically invoked when an object goes out of scope.
Destructor with Dynamic Memory Releases dynamically allocated memory.
Destructor in Multiple Objects Called for each object when going out of scope.
Destructor in an Array of Objects Destructor is called for each object in an array.
Destructor in Inheritance Destructor of derived class is called first, then base class.

🚀 Need more examples? Let me know! 🚀

Post a Comment

0 Comments