#include<iostream.h>
#include<conio.h>
int c=0;
class num
{
public:
num()
{
c++;
cout<<"Object : "<<c<<endl;
}
~num()
{
cout<<"Object released Object : "<<c<<endl;
c--;
}
};
int main(void)
{
cout<<"In Main"<<endl;
num a,b;
cout<<"Again in Main"<<endl;
{
class num c;
}
getch();
return 0;
}
In this code how many times will the destructor be called and executed?
On executing the code i find that it is called only once.
If it is called only once then will it destroy all the objects?
I ran this code in Embarcadero C++ Builder XE
Thanking you,
Sworoop
It's called thrice, but there's a bit of a thing going on here. The objects are destroyed when they go out of scope; c is defined in it's own little scope, which is ended very soon - it's destructor is called. a and b, are declared in the main scope, when main ends, they are destroyed (this is AFTER your getch() call). To fix this, put the getch() call into your destructor:
1 2 3 4 5 6
~num()
{
cout<<"Object released Object : "<<c<<endl;
c--;
getch();
}
TIP:
You might want to look into static data members, concerning the variable c.
Thanks for the reply Kyon
Here is the O/p of the code that i get using both the IDEs
In Main
Object : 1
Object : 2
Again in Main
Object : 3
Object released Object : 3
If the destructor is called thrice then i shud get
O/p
In Main
Object : 1
Object : 2
Again in Main
Object : 3
Object released Object : 3
Object released Object : 2
Object released Object : 1
@Kyon
for the variable c i have taken global variable because my instructor is a mug up specialist.
she even by hearts the programs and she won't understand the use of the static variable and that wud cost me project marks ;)
using static variable c :
#include<iostream.h>
#include<conio.h>
class num
{
static int c;
public:
num()
{
c++;
cout<<"Object : "<<c<<endl;
}
~num()
{
cout<<"Object released Object : "<<c<<endl;
c--;
}
};
int Num::c=0;
int main(void)
{
cout<<"In Main"<<endl;
num a,b;
cout<<"Again in Main"<<endl;
{
class num c;
}
getch();
return 0;
}
You nearly did the static data member alright, the static int should be public, not private. Your instructor must be kind of.. - well, I will not say that here - to not see the use (or know about) of a static member. It's related to namespaces (or even scoping) closely, which is a widely used functionality of C++. Your code is good, but you should change the destructor like I told you:
#include<iostream.h>
#include<conio.h>
usingnamespace std;
class num
{
public:
// static int c;
// It's not useful to use an integer here, since c can normally not be negative, it's best to use an unsigned (int)
staticunsigned c;
num()
{
c++;
cout<<"Object : "<<c<<endl;
}
~num()
{
// cout<<"Object released Object : "<<c<<endl;
// c--;
// Both of the above lines can be transformed into one line:
cout << "Object released.\nObject: " << c << endl;
getch();
}
};
// int num::c=0;
// EDIT: No longer an integer, this is an unsigned (specified above)
unsigned num::c = 0;
// int main(void)
// It's not needed to add void in a parameter declaration, it can be omitted since void represents the absence of data
int main()
{
cout<<"In Main"<<endl;
num a,b;
cout<<"Again in Main"<<endl;
{
// class num c;
// It's not needed to add class in front of that:
num c;
}
return 0;
}
I commented out some lines and added alternatives underneath them, this could should work as you expect it too.
@Kyon
Thanks for the corrections :)
ur code worked fine except for the initialization of the static var(unsigned num::c=0). i had written int num::c=0;
Thanks again :)
PS: Yes my teacher really is an err....
u r right i shudn't tell that here ;)