Please help me to understnd the output. Why its printing 6 8 and not 2 4?

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 #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; 
}
First, let me establish a bit of notation so I can tell you about your objects. Let name(a, b) mean the Bix object called "name" with x equal to a and y equal to b.

Now, I'll go step by step through your program, showing the objects and their states after each step:

You start the program. [No objects yet]
You create a Bix called "run". [run(_, _)]
You enter the main() method of run. [run(_, _)]
You create a Bix called "b" (I'll call it b1) and initialize it's members. [run(_, _) b1(6, 8)]
You enter the show() method of b1. [run(_, _) b1(6, 8)]
You create another Bix called "b" (I'll refer to it as b2) and initialize it's members. [run(_, _) b1(6, 8) b2(2, 4)]
You print the x and y values of b1 (you are in its show method); note that they are 2 and 4, so that is what is printed.

Does this help?
Topic archived. No new replies allowed.