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 2 of 2.

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.

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.

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.


Post your comments here:

Your comments will be displayed after verification.