Thursday, 16 March 2017

//Destructor destroy object in LIFO fashion (Last in First out) as object go out of the scope. 
//Destructor call by compiler when any object created by user goes out of the scope.
//Syntax of desctructor is same expect;
// There is no any parameter in destructor.
// ~ sign before the definition of function. please refer code for prototype of destructor. 
//Run the code and observe the output write your views on comments. 
#include<iostream>
using namespace std;

class Alpha
{
static int count;
public:
Alpha() // Default constructor
{
count++;
cout<<"Object created:"<<count<<endl;
}
~Alpha()
{
cout<<"Object Destroyed:"<<count<<endl;
count--;
}
};
int Alpha::count;
int main()
{
Alpha a1,a2,a3,a4;
{
Alpha a5;
}
{
Alpha a6;
}
}

No comments:

Post a Comment