Destructor In C++
Destructor
is a member function which destructs or deletes an object.
A destructor function is called automatically when the object goes out of
scope:
the function ends the program ends a block containing local variables ends a
delete operator is called Destructors have same name as the class preceded
by a tilde (~) Destructors don’t take any argument and don’t return anything.
Fig: destructor in c++
Synatx:
class
classname
{
----
----
public:
~classname();
-----
-----
-----
};
Program:
#include<iostream.h>
#include<conio.h>
class
BCA
{
int
a,b;
public:
BCA();
{
Cout<<”constructor
create”;
}
~BCA()
{
cout<<”Destructor
create”;
}
void
input()
{
cout<<”input
value”;
cin>>a>.b;
void
output()
{
cout<<”a=”<<a;
cout<<”b=”<<b;
}
};
void
main()
{
BCA
a1;
a1.input();
a1.output();
getch();
}
Output:
Constructor
create 2 2
22
Feature Of Destructor In C++
1. It is invoked when object goes out of the scope
2. Like constructor, it can also have
parameters
3. It can be virtual
4. It can be declared in private
section
No comments:
Post a Comment