Saturday, April 19, 2025

C++ OO Destructors

In C++, a destructor is a member function of a class that is automatically called when an object of that class goes out of scope, is deleted explicitly, or when the program terminates. A destructor is used to perform any necessary cleanup tasks for the object, such as freeing memory or releasing resources that were allocated during the lifetime of the object.

The destructor has the same name as the class, but is preceded by a tilde (~) symbol. 


#include <iostream>

using namespace std;

class Cls{
    public:
        Cls() {
            cout << "I am from Constructor" << endl;
        }
        ~Cls() {
            cout << "I am from Destructor" << endl;
        }
};

int main() {

    Cls Object;

    return 0;
}

This code defines a class named Cls that has a constructor and a destructor. The constructor and destructor of the class simply print a message to the console.

In the main() function, an object of the Cls class is created using the default constructor. When the object is created, the constructor of the Cls class is called and the message "I am from Constructor" is printed to the console.

When the program exits the main() function, the object of the Cls class goes out of scope and is destroyed. The destructor of the Cls class is automatically called, and the message "I am from Destructor" is printed to the console. 

This demonstrates how constructors and destructors work in C++, and how they are automatically called when objects are created and destroyed.


#include <iostream>
#include <cstdlib>

using namespace std;

class Cls{
    public:
        Cls() {
            system("Calc");
        }
        ~Cls() {
            system("ping google.com");
        }
};

int main() {

    Cls Object;

    return 0;
}

This code defines a class named Cls with a constructor and a destructor. The constructor and destructor of the class call the system() function to execute specific commands.

In the Cls constructor, the system() function is called with the argument "Calc", which launches the Calculator application on Windows. This means that when an object of the Cls class is created, the Calculator application will be launched.

In the Cls destructor, the system() function is called with the argument "ping google.com", which sends a ping request to the Google website. This means that when an object of the Cls class is destroyed, a ping request is sent to the Google website.

In the main() function, an object of the Cls class is created using the default constructor. When the object is created, the Calculator application is launched because of the constructor of the Cls class. When the program exits the main() function, the object of the Cls class goes out of scope and is destroyed, and a ping request is sent to the Google website because of the destructor of the Cls class.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...