Advertisement
//C++ An example of destructor
#include<iostream>
using namespace std;
class A
{
public:
//A no-argument constructor
A()
{
cout << "Constructor called" <<"\n";
}
//A destructor
~A()
{
cout <<Destructor called" <<"\n";
}
};
int main()
{
// Calling a no-argument constructor to create the objects of the class A
A ob1, ob2, ob3;
}
Constructor called
Constructor called
Constructor called
Destructor called
Destructor called
Destructor called
Advertisement
//C++ Another example of destructor
#include<iostream>
using namespace std;
class A
{
public:
//A no-argument constructor
A()
{
cout << "Constructor called" <<"\n";
}
//A destructor
~A()
{
cout << "Destructor called" <<"\n";
}
void fun()
{
A ob;
}
};
int main()
{
// Calling a no-argument constructor to create a object of class A
A ob1;
//Calling a user-defined function
ob1.fun();
//A local block
{
A ob2;
}
}
Constructor called
Constructor called
Constructor called
Destructor called
Destructor called
Destructor called
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement