#include <iostream>

class MyResource {
    private:
        int* resource;
    public:
        MyResource() {
            resource = new int[10];
            std::cout << "Resource allocated" << std::endl;
        }
        ~MyResource() {
            delete[] resource;
            std::cout << "Resource released" << std::endl;
        }
};

int main() {
    MyResource obj;
    // Use the resource
    std::cout << "Using the resource" << std::endl;
    obj.resource[5] = 10;
    std::cout << "Resource value: " << obj.resource[5] << std::endl;

    std::cout << "Leaving the scope of main function" << std::endl;
    return 0;
}

/*
In this example, the class MyResource has a constructor that allocates an array of
10 integers on the heap and assigns it to the resource variable, and a destructor
that releases the resource by calling delete[] operator on it.

When an object of this class is created, its constructor is called to allocate
the resource, and when the object is destroyed (for example, when it goes out of
scope or is deleted), its destructor is called to release the resource.

In the main function, the program creates an object obj of type MyResource and
uses the resource variable to assign a value to one of the array elements and 
print it. Then, when the program exits the main function the object's destructor 
is called, which releases the resource and prints "Resource released" to the
console.

It's worth noting that it's important to always release resources allocated on the
heap and not to rely on the destructor alone to do so, it's always a good practice
to release resources in a timely manner.

------------------------------------------------------------------------------

distructor in c++ with ex
In C++, a destructor is a special member function of a class that is automatically
called when an object of that class is destroyed. The main purpose of a destructor
is to release any resources that were acquired by the object during its lifetime.

A destructor has the same name as the class preceded by the tilde (~) symbol,
and it does not take any arguments and does not have a return type.

Here is an example of a class with a destructor:

Copy code
class MyResource {
    public:
        MyResource() {
            // Allocate resource
        }
        ~MyResource() {
            // Release resource
        }
};
In this example, the class MyResource has a constructor that allocates a resource and a destructor that releases the resource. When an object of this class is created, its constructor is called to allocate the resource, and when the object is destroyed (for example, when it goes out of scope or is deleted), its destructor is called to release the resource.

Here is a full program that demonstrates the use of a destructor:

Copy code
#include <iostream>

class MyResource {
    public:
        MyResource() {
            std::cout << "Resource allocated" << std::endl;
        }
        ~MyResource() {
            std::cout << "Resource released" << std::endl;
        }
};

int main() {
    {
        MyResource obj;
    }
    std::cout << "Back in main function" << std::endl;
    return 0;
}
In this example, the program creates an object obj of type MyResource inside a
block. The object's constructor is called, which prints "Resource allocated" to 
the console. Then, when the program exits the block, the object's destructor is
called, which prints "Resource released" to the console. Finally, the program
prints "Back in main function" to the console to indicate that it has returned
to the main function.

It's important to note that destructors do not take any arguments, and they are 
automatically called when an object of the class is destroyed. They are used to
release any resources that were acquired by the object during its lifetime and
perform any other cleanup tasks that are required.

*/

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: