Destructors
Destructors
Let's take an example:
CODE:
OUTPUT:
- Destructor is to destroy the objects that have been created by the constructors.
- Like the constructor Destructor is also a member function of the class with same name of class.
- But it preceded by tilde sign(~).
- Destructor never takes any argument.
- Destructor never return any value.
- It will invoke implicitly.
Let's take an example:
CODE:
#include<iostream>
using
namespace std;
class
NumberInitialization
{
int
x,y;
public:
NumberInitialization(); //constructor
~NumberInitialization(); //destructor
};
NumberInitialization::NumberInitialization()
{
cout<<"default constructor
calling"<<endl;
x=1;
y=2;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
NumberInitialization::~NumberInitialization()
{
cout<<"Destructor
calling"<<endl;
x=0;
y=0;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
int main()
{
NumberInitialization n1;
cout<<"Program over\n";
return 0;
}
OUTPUT:
0 Comments