C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : Constructors and Destructors - Programs (Q.No. 9)
9.
What will be the output of the following program?
#include<iostream.h> 
class IndiaBix
{
    int x, y; 
    public:
    IndiaBix(int xx)
    {
        x = ++xx;
    } 
    ~IndiaBix()
    {
        cout<< x - 1 << " ";
    }
    void Display()
    {
        cout<< --x + 1 << " ";
    } 
};
int main()
{
    IndiaBix objBix(5);
    objBix.Display();
    int *p = (int*) &objBix;
    *p = 40;
    objBix.Display();
    return 0; 
}
6 6 4
6 6 5
5 40 38
6 40 38
6 40 39
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 1 of 2.

K kushal said:   2 years ago
first x is 6
display() gives 6,
then x is 40 and display is called.

So, x becomes 39 but prints 40 as cout<< --x+1,
now destructor calls x-1 => 38; simple.

Suchismita said:   3 years ago
I don't understand. Please explain me.

Avc said:   7 years ago
I am not understanding this, Please anyone help me.

Kriti said:   7 years ago
#include<iostream>
#include<stdio.h>
using namespace std;
class IndiaBix
{
int x, y;
public:
IndiaBix(int xx)
{
x = ++xx;
cout<<"A:"<<x<<endl;
}
~IndiaBix()
{
cout<<"B:"<< x << endl;
}
void Display()
{
cout<<"C:"<< --x << endl;
}
};

int main()
{
IndiaBix objBix(5);
//cout<<"a"<<endl;
objBix.Display();
//cout<<"b"<<endl;

int *p = (int*) &objBix;
*p = 40;
cout<<"c"<<endl;
objBix.Display();
return 0;
}


IndiaBix objBix(5); // creates objects and calls the constructor so (++5) = 6
objBix.Display();
int *p = (int*) &objBix; // doesn't call the constructor instead go to the location of object where x is Changed the value of x. X = 40 now instead of 6.
*p = 40;
objBix.Display(); // at display --40 = 39. X value changed to 39 since we have reached the termination of the program Now deconstructor will be called X is still 39.

Arjun Suri said:   8 years ago
Now, most of you must be thinking how the value of x becomes 40 when int *p=40. Actually, if you think p is pointing to the object objBix, so indirectly &objBx becomes 40.

But how x becomes 40? Actually, it is the order of declaration of an instance variable x and y.If you write int x,y then, when *p becomes 40, x=40, but if it is declared like int y,x; then if *p=40 then the value will go to y not x, i.e., y=40.

I tried it on my local machine by changing the order of declaration of variables.

Rajat Mishra said:   8 years ago
First,
The constructor is called.

So x become 6 (++xx)
Then
objBix.Display(); //1
is called
which display 6.

then,
int *p = (int*) &objBix;
creates object which s type casted int ;
*p=40 //calls conversion constructor
then,
x=41;
objBix.Display(); //2
display 40 //--x+1 --40+1=39+1=40
//x=39;
last destructor is called
x-1;
39-1
=38

Anshita said:   8 years ago
First, it will call parameterize constructor.

Then x will be 6. After call display, 6 will be decremented as 5 and then add 1, it will print 6.

Then *p pointing to the same object.
Then *p will change the value of x as 40.
Then after call display first it will decremented as --x will become 39, hence it will print 40(39+1).

After it will call destructor by the compiler, it will print 38(39-1) as 39 was previous value of x.

Ghulam Mustafa said:   9 years ago
Anyone can Explain more precise?

Shreyas said:   1 decade ago
It will print 38, because as in display(), --x + 1, ==> 39+1;

As now the x contains 39 not 40, so when destructor is called i.e x - 1 ==> 39-1 = 38.

Rajireddy-ou said:   1 decade ago
x is part of the obj. ptr is pointing to that obj. Internally *p = x.


Post your comments here:

Your comments will be displayed after verification.