#include<iostream>
class sample
{
int a, b;
public:
sample()
{
a=1;
b=2;
cout<<"a = "<<a << " b = "<<b <<endl;
}
sample(int x)
{
a = x;
b = x+1;
}
sample(int x, int y)
{
a = x+y;
b = y+10;
}
~sample()
{
cout<<"Destructor "<<endl;
}
void disp()
{
cout<<"a = "<<a << " b = "<<b <<endl;
}
};
main()
{
sample P;
sample A(3);
A.disp();
sample B(5,5);
B.disp();
}
The output of program will be
a = 1 b = 2
a = 3 b = 4
a = 10 b = 15
Destructor
Destructor
Destructor
Why the output is not like this First constructor does its job then destructor runs, then second constructor does its job then again destructor runs and so on.
like this
a = 1 b = 2
Destructor
a = 3 b = 4
Destructor
a = 10 b = 15
Destructor
How does it actually work what is the order
MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!
This post has been edited by JackOfAllTrades: 09 December 2012 - 04:48 AM

New Topic/Question
Reply



MultiQuote




|