C++ Programming - Objects and Classes - Discussion

Discussion Forum : Objects and Classes - Programs (Q.No. 4)
4.
What will be the output of the following program?
#include<iostream.h> 
class BixTeam
{
    int x, y; 
    public:
    BixTeam(int xx)
    {
        x = ++xx;
    }
    void Display()
    {
        cout<< --x << " ";
    }
};
int main()
{
    BixTeam objBT(45);
    objBT.Display();
    int *p = (int*)&objBT;
    *p = 23;
    objBT.Display();
    return 0; 
}
45 22
46 22
45 23
46 23
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
13 comments Page 1 of 2.

Ashish Sharma said:   2 years ago
Here, know about the conversion constructor, which gets automatically called in some specific situations.

int *p = (int*)&objBT;
*p = 23;

In above-mentioned code, it will get called and assigned a value 23 to x.

Prashant said:   6 years ago
In case if more than 1 variable is there in class than how the pointer will work to initialize the value? please explain me.

Mehul said:   8 years ago
Here, int *p =(int *)&objBT;

this is type casting and a class pointer is temporary converted into (int* )type and p is now pointing to object the first member and we have modified that location so data is change accordingly.

Sai said:   9 years ago
I cannot understand clearly. Can you explain properly?

Shitu pawar said:   10 years ago
Why ++XX use?

Saibee said:   10 years ago
int *p=(int*)&objBT;

This is creating an integer type pointer p which points to objBT means has the address of objBT so i.e. y &objBT and as this object is of class bixteam type. So its is converted to int first i.e y (int*).

*p=23; means pointer has pointing to object and the object has value 23. Now member value (x) is taken as its object value automatically and then display function displays it.

Ayush said:   1 decade ago
What happens when we do so:

int *p = (int*)&objBT;
*p = 23;

By casting objBt into what do we are try to make of p?

Rohit said:   1 decade ago
Please not that when we write,

int *p = (int*)&objBT;
*p = 23;

We are not creating new object of BixTeam (hence no constructor is called)
and hence when display() for 'p' is called, it just just decrements the value of x (rather than incrementing it first in constructor).

Hence 22.
(3)

Belal said:   1 decade ago
I think it's 22 because the casting.

:Lalith said:   1 decade ago
23 is converted into the binary digits according to the address and from that it will be printed.


Post your comments here:

Your comments will be displayed after verification.