Q:

C++ Program to demonstrate the Concept of Destructor

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to illustrate Order of Constructor Inv... >>
<< Write a C++ Program to show Example of Default cop...