Destructor

#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
Last edited on
I just executed the code in TC 3.0 and got the same result
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.
Last edited on
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

Thank You.

@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;
}



hope i have used the static var c correctly

thanks :)
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream.h>
#include<conio.h>

using namespace 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)
  static unsigned 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.
Last edited on
@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 ;)
Ah yes, you should change the snippet int num::c = 0; to: unsigned num::c = 0;.

No prob, helpin' bro. ;)
Topic archived. No new replies allowed.