C++ Programming - Objects and Classes - Discussion
Discussion Forum : Objects and Classes - Programs (Q.No. 6)
6.
What will be the output of the following program?
#include<iostream.h>
class India
{
public:
struct Bix
{
int x;
float y;
void Function(void)
{
y = x = (x = 4*4);
y = --y * y;
}
void Display()
{
cout<< y << endl;
}
}B;
}I;
int main()
{
I.B.Display();
return 0;
}
Discussion:
25 comments Page 2 of 3.
Priya said:
1 decade ago
Why answer is zero? How the function is evaluated?
Srinivas said:
1 decade ago
In above example, immediate to classes declarations, objects I and B are also constructed in global space so that I.B.Display() will give value any of its member initialized to default i.e here y is 0.
Note 1: By keeping declaration in global space and try to create objects I and B inside main() then I.B.Display() will give any of its member value as Garbage.
Note 1: Inside main(), declare class as static and then create objects also, in this case I.B.Display() will give value any of its member initialized to default i.e here y is 0.
#include <iostream>
using namespace std;
class Bix
{
int x;
float y;
public:
void Display()
{
cout<< y << endl;
}
}B;
int main()
{
static class Bbb
{
int x;
float y;
public:
void Display()
{
cout<< y << endl;
}
}Bb;
Bix B1;
B.Display();
B1.Display();
Bb.Display();
return 0;
}
Note 1: By keeping declaration in global space and try to create objects I and B inside main() then I.B.Display() will give any of its member value as Garbage.
Note 1: Inside main(), declare class as static and then create objects also, in this case I.B.Display() will give value any of its member initialized to default i.e here y is 0.
#include <iostream>
using namespace std;
class Bix
{
int x;
float y;
public:
void Display()
{
cout<< y << endl;
}
}B;
int main()
{
static class Bbb
{
int x;
float y;
public:
void Display()
{
cout<< y << endl;
}
}Bb;
Bix B1;
B.Display();
B1.Display();
Bb.Display();
return 0;
}
Tejas said:
1 decade ago
In this example, we are not calling function Function(). So, value of y & x are not changed that's why it's displaying 0.
Pooja said:
1 decade ago
Is it displaying the default value of y i.e.=0;
Hai Nguyen said:
1 decade ago
It's all about logical assignments.
y = x = (x = 4*4);
// Successful assignment " x=4*4 " returns.
// value 1 to x, in turn successful assignment " x = (...) ".
// returns value 1 to y, so y = 1; next.
y = --y * y; // --y is operated 1st so y = 0 now, then 0*0 = 0.
y = x = (x = 4*4);
// Successful assignment " x=4*4 " returns.
// value 1 to x, in turn successful assignment " x = (...) ".
// returns value 1 to y, so y = 1; next.
y = --y * y; // --y is operated 1st so y = 0 now, then 0*0 = 0.
Jeet said:
1 decade ago
Yes, but this is not structure please explain in detail.
Swati said:
1 decade ago
Why it's not 1?
Mukesh said:
1 decade ago
Yes, Structure members are initialized to zero if not defined explicitly.
Shantayya said:
1 decade ago
Structure members are initialized to zero if not defined explicitly.
Sonali said:
1 decade ago
Where it is declared global? please explain program in detail.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers