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;
}
Discussion:
13 comments Page 1 of 2.
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.
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)
Anonymous said:
1 decade ago
Why 45 is not incremented?
Bharat said:
1 decade ago
Bro 45 is incremented by 1 in constructor definition, it becomes 46. And also we decrements by 1 when we display the value of x, that time it becomes 45.
Swati said:
1 decade ago
What about 23?
:Lalith said:
1 decade ago
23 is converted into the binary digits according to the address and from that it will be printed.
Belal said:
1 decade ago
I think it's 22 because the casting.
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?
int *p = (int*)&objBT;
*p = 23;
By casting objBt into what do we are try to make of p?
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.
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.
Shitu pawar said:
10 years ago
Why ++XX use?
Sai said:
9 years ago
I cannot understand clearly. Can you explain properly?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers