Subject name: Object oriented programming
Assignment # 02
What is a destructor? Write its syntax and rules, and provide an
example?
Answer:
A destructor is a special member function of a class in object-oriented
programming. It is automatically called when an object is destroyed or goes out of scope. The
main purpose of a destructor is to free resources that were allocated to the object during its
lifetime, such as memory, file handles, or database connections.
Syntax of destructor:
The syntax of a destructor is similar to that of a constructor but with a few
key differences:
The destructor has the same name as the class, preceded by a tilde (~).
A destructor does not take any arguments and does not return a value.
Syntax code:
1- Destructor definition inside the class:
class ClassName {
public:
~ClassName() {
// Destructor code
};
2- Destructor definition outside the class:
class ClassName {
public:
1|Page
Subject name: Object oriented programming
~ClassName(); // Destructor declaration
};
ClassName::~ClassName() {
// Destructor code
Note:
Similar to constructor, the destructor name should exactly match with the class name. A
destructor declaration should always begin with the tilde(~) symbol as shown in the syntax
above.
Destructor Example # 01:
#include <iostream>
using namespace std;
class HelloWorld{
public:
//Constructor
HelloWorld(){
cout<<"Constructor is called"<<endl;
//Destructor
~HelloWorld(){
cout<<"Destructor is called"<<endl;
}
2|Page
Subject name: Object oriented programming
//Member function
void display(){
cout<<"Hello World!"<<endl;
};
//Object creation
int main(){
//Object created
HelloWorld obj;
//Member function called
obj.display();
return 0;
Output:
Constructor is called
Hello World!
Destructor is called
Destructor example # 02:
Here’s a simple example demonstrating the use of a destructor:
#include <iostream>
Using namespace std;
class FileHandler {
3|Page
Subject name: Object oriented programming
public:
// Constructor
FileHandler() {
file = fopen("data.txt", "w");
if (file) {
cout << "File opened." << endl;
// Destructor
~FileHandler() {
if (file) {
fclose(file);
cout << "File closed." << endl;
private:
FILE* file;
};
int main() {
FileHandler fh; // Constructor is called here
// Do something with the file
return 0; // Destructor is called here, closing the file
4|Page
Subject name: Object oriented programming
}
Explanation of code:
When the object fh is created, the constructor opens a file.
When the object goes out of scope (at the end of the main function), the destructor
automatically closes the file.
This ensures that resources (such as file handles) are properly released when the object is no
longer needed.
When does the destructor get called?
A destructor is automatically called when:
1) The program finished execution.
2) When a scope (the { } parenthesis) containing local variable ends.
3) When you call the delete operator.
Destructor rules:
1) Name should begin with tilde sign (~) and must match class name.
2) There cannot be more than one destructor in a class.
3) Unlike constructors that can have parameters, destructors do not allow any parameter.
4) They do not have any return type, just like constructors.
5) When you do not specify any destructor in a class, compiler generates a default destructor
and inserts it into your code.
Uses of C++ destructor:
Custom Cleanup Logic
Preventing Resource Leaks in Exception Handling
Smart Pointers
Logging and Debugging
Resource Release
Memory Deallocation
5|Page