C++ Program to demonstrate the Concept of Destructor

By | 31.12.2016

Destructor


Write a C++ Program to demonstrate the Concept of Destructor. Here’s a Simple Program to demonstrate an example of Destructors with Output in C++ Programming Language.


Destructors in C++ : :

These are the type of member function which are automatically executed when an object of that class is destroyed is called a destructor.

The destructors has no return type and its name is same as class name, it cannot accept any parameters. The tilde sign(~) is written before the destructor name.


Syntax : :

The Syntax of declaring destructors are as follows:

~name()

{

Destructors body

}

~name: It shows the name of a destructor.


Below is the source code for C++ Program to demonstrate the Concept of Destructor with Example which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to demonstrate the Concept of Destructor  */

#include<iostream>
using namespace std;

class test
{
    private:
        int a;
    public:
        test()
        {
            cout<<"Object is created............"<<endl<<endl;
        }
        ~test()
        {
            cout<<"Object is destroyed.........."<<endl<<endl;
        }
};

int main()
{
    test *p1= new test;
    test *p2= new test;

    delete p1;
    delete p2;

    return 0;
}

OUTPUT : :


/*  C++ Program to demonstrate the Concept of Destructors  */

Object is created............

Object is created............

Object is destroyed..........

Object is destroyed..........


Process returned 0

Above is the source code and output for C++ Program to demonstrate the Concept of Destructors which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

3 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments