Sunday, 3 February 2013

Write a Simple C++ program using Constructor and Destructor.


Program:

#include<iostream.h>
#include<conio.h>
int count=0;
class Alpha
{
public:
Alpha();
~Alpha();
};
Alpha::Alpha()
{
count++;
cout<<"The number of Object created "<<count<<endl;
}
Alpha::~Alpha()
{
cout<<"The number of Object destroyed "<<count<<endl;
count--;
}
void main()
{
Alpha A1,A2,A3,A4;
{
cout<<"Enter in Block 1"<<endl;
Alpha A5;
}
{
cout<<"Enter in Block 2"<<endl;
Alpha A6;
}
cout<<"Reenter in Main"<<endl;
getch();
}

Output:
The number of Object created 1
The number of Object created 2
The number of Object created 3
The number of Object created 4
Enter in Block 1
The number of Object created 5
The number of Object destroyed 5
Enter in Block 2
The number of Object created 5
The number of Object destroyed 5
Return in main
The number of Object destroyed 4
The number of Object destroyed 3
The number of Object destroyed 2
The number of Object destroyed 1

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete