What is destructor? Write a program to show the destructor call such that it prints the message “memory is released”.

This answer is restricted. Please login to view the answer of this question.

Login Now

Destructors are members functions in a class that delete an object. They are called when the class object goes out of scope such as when the function ends, the program ends, a delete variable is called etc.

Destructors are different from normal member functions as they don’t take any argument and don’t return anything. Also, destructors have the same name as their class and their name is preceded by a tilde(~).

Let look an example of destructor that will delete an object and print message “Memory is released”.

#include <iostream>
using namespace std;

class CSIT
{
    private:
        int num1, num2;

    public:
        CSIT(int n1, int n2)
        {
            num1 = n1;
            num2 = n2;
        }

        ~CSIT()
        {
            cout << "Memory is released";
        }
};

int main()
{
    CSIT obj1(10, 20);
    return 0;
}
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .