Destructor in C++
C++ Programming

Destructors in C++

 

In C++, a destructor is a special member function of a class that is automatically called when an object of that class goes out of scope or is explicitly destroyed. It is used to clean up any resources or perform any necessary actions before the object is destroyed.

The syntax for a destructor in C++ is similar to that of a constructor but with a tilde (~) followed by the class name.

Here’s an example:

class MyClass {

public:

    // Constructor

    MyClass() {

        // Initialization code

    }

    // Destructor

    ~MyClass() {

        // Cleanup code

    }

    // Other member functions and data members

};

The destructor does not have any parameters, and you can only have one destructor per class. The name of the destructor is always the same as the class name, preceded by a tilde.

The destructor(tilde operator(~)) is automatically called when an object is destroyed. This can happen when the object goes out of scope, such as when a local object is created inside a function and the function ends.

Here’s an example :

#include <iostream>

class MyClass {

public:

    MyClass() {

        std::cout << “Constructor called” << std::endl;

    }

    ~MyClass() {

        std::cout << “Destructor called” << std::endl;

    }

};

int main() {

    MyClass obj;  // Create an object of MyClass

    // Other code…

    return 0;  // Object obj goes out of scope and the destructor is called

}

In the above example, when the object obj goes out of scope at the end of the main function, the destructor MyClass is automatically called. The destructor prints a message indicating that it has been called.

Example with output

#include<iostream.h>

#include<conio.h>

class abc

{

public:

abc()

{

cout<<“constructor”;

}

~abc()

{

cout<<“destructor”;

}};

main()

{

clrscr();

abc ob1;

getch();

}

Destructor in C++

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment