0% found this document useful (0 votes)
8 views9 pages

Lecture 4 - Destructor

In C++, a destructor is a special member function that is automatically called when an object is destroyed, used for cleaning up resources like memory and file handles. It has the same name as the class prefixed with a tilde (~), cannot take parameters, and can only be defined once per class. Destructors are crucial in inheritance to ensure proper cleanup, especially when using virtual destructors in base classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Lecture 4 - Destructor

In C++, a destructor is a special member function that is automatically called when an object is destroyed, used for cleaning up resources like memory and file handles. It has the same name as the class prefixed with a tilde (~), cannot take parameters, and can only be defined once per class. Destructors are crucial in inheritance to ensure proper cleanup, especially when using virtual destructors in base classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Destructor

In C++, a destructor is a special member function of a class that is automatically called when an object of
that class is destroyed. It is used to clean up resources that were allocated to the object during its lifetime,
such as dynamically allocated memory, file handles, or other system resources.
Key Features of Destructors:

Syntax: A destructor has the same name as the class, prefixed with a tilde (~), and it does not have any return
type or parameters.
class MyClass {
public:
~MyClass() {
// Destructor code
}
};
1. Automatic Invocation: The destructor is automatically invoked when an object goes out of scope (for stack-allocated objects) or when it is explicitly deleted
(for heap-allocated objects).
2. No Arguments: A destructor cannot take parameters, and it cannot be overloaded. You can only have one destructor in a class.
3. Purpose: Destructors are generally used for:
o Releasing dynamically allocated memory (using new or malloc).
o Closing file handles, network connections, or database connections.
o Cleaning up any resources the object holds.
Example:
#include <iostream>
using namespace std;

class MyClass {
public:
MyClass() {
// Constructor
cout << "Constructor: Object is created" << endl;
}

~MyClass() {
// Destructor
cout << "Destructor: Object is destroyed" << endl;
}
};

int main() {
{
MyClass obj; // Destructor will be called when 'obj' goes out of scope
}
// 'obj' is destroyed here
return 0;
}
Output:
Constructor: Object is created
Destructor: Object is destroyed
Important Notes:
 If a class allocates resources dynamically (e.g., with new), it's important to free those resources in the
destructor to avoid memory leaks.
 Destructors are called automatically when an object goes out of scope for local objects or when the
delete operator is used for objects created with new.
Destructor in Inheritance:
 If a derived class has a destructor, and the base class has a destructor, the base class destructor is called
automatically when the derived class object is destroyed.
 If the base class destructor is virtual, it ensures that the correct destructor is called for objects deleted
through a base class pointer.
class Base {
public:
virtual ~Base() { // Virtual Destructor
cout << "Base Destructor" << endl;
}
};

class Derived : public Base {


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

int main() {
Base* b = new Derived();
delete b; // Calls Derived's destructor, then Base's destructor
return 0;
}
Output:
Derived Destructor
Base Destructor
By using a virtual destructor in the base class, we ensure that the
derived class's destructor is called before the base class's destructor,
which is important for proper resource cleanup in inheritance
hierarchies.

You might also like