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 1 of 3.
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;
}
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.
Sujeeth said:
1 decade ago
No I am not agree with it @Ashwak.
Class member's default values is not zero. Static storage duration are zero-initialized at the program startup.
Class member's default values is not zero. Static storage duration are zero-initialized at the program startup.
Krish said:
1 decade ago
The void function was not called only display is invoked ? then how initialized y=x=(x=4*4).
How you say y=x=(x=4*4) is equal to 1?
How you say y=x=(x=4*4) is equal to 1?
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.
Tanmay said:
8 years ago
The void Function (void) will not be called simply.
If we have written I.B.Function then things would have been different.
If we have written I.B.Function then things would have been different.
Paanku said:
7 years ago
It is because we have a global object.
For global objects, all members are initialised to zero.
For global objects, all members are initialised to zero.
(1)
Neha said:
7 years ago
Because y is global variable for function display and global variable value by default is zero.
(1)
Nishant sharma said:
10 years ago
Since argument type of function is void therefore it takes no arguments. Hence answer is zero.
Swetha said:
8 years ago
Hello.
I'd like to assert that the main function is returning a 0,
hence, 0!.
I'd like to assert that the main function is returning a 0,
hence, 0!.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers