C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 19)
19.
What will be the output of the following program?
#include<iostream.h> 
class Bix
{
    int x, y; 
    public:
    void show(void);
    void main(void);
};
void Bix::show(void)
{ 
    Bix b;
    b.x = 2;
    b.y = 4;
    cout<< x << " " << y;
}
void Bix::main(void)
{
    Bix b;
    b.x = 6; 
    b.y = 8;
    b.show();
}
int main(int argc, char *argv[])
{
    Bix run;
    run.main();
    return 0; 
}
2 4
6 8
The program will report error on Compilation.
The program will report error on Linking.
The program will report error on Run-time.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Abraham said:   4 years ago
Int x, y; is declared as private. Other objects can't access them like in the example above. It will give compile error.

Am I right? Please explain me.
(4)

Shelly said:   7 years ago
Yes, @Yash.

We can have a function name as main.

Yash said:   8 years ago
Is it possible to have a function name as main!?

Mitali said:   9 years ago
x means this->x and y means this->y.
So, when x and y are printed they refer to the calling function and in the calling function the values of x and y were assigned to 6 and 8.

Harsha said:   1 decade ago
In show() method x and y are local and they are global to main method.

So main method values will be printing. If in show method if we print like b.x and b.y in show then we get 2 4 as op.

Raghav said:   1 decade ago
Is there two different main functions? or both are same?

Bhairavee said:   1 decade ago
If we were to print b.x and b.y instead of x and y then result would have been 2 and 4 and not 6 and 8.

According to me, The object b which is created again when show is called is different from the object b accessing the show function. Hence, the values displayed are those defined by the object b of the class in the main function.

Lokesh said:   1 decade ago
Can you explain why it called the object of main rather than calling the object of show?

Satya said:   1 decade ago
Bix::main() creates an object and sets its variables x=6, and y=8. And then it calls Bix::show().

Bix::show() creates another object and sets its variables x=2, and y=4. And then it displays the object created by Bix::main() and not the one created by it.

Hence output is '6 8'.

Ash said:   1 decade ago
Why output is 6, 8?

Post your comments here:

Your comments will be displayed after verification.