Doubt on this pointer

Hi guys

Please explain me the output for this program

thanks

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* 
 * File:   main.cpp
 * Author: user
 *
 * Created on 10 November, 2014, 5:48 PM
 */
#include <iostream>
#include <string.h>
using namespace std;

class BB {
public:
    int x, y;
public:

    BB(int x1 = 0, int y1 = 0) {

        x = x1;
        y = y1;
    }
};

class BD : public BB {
private:
    BB bb;
public:

    BD(int x1, int y1) : BB(x1),bb(y1) {

        cout << this->x << " "
             << this->y << " "
             << bb.x    << " "
             << bb.y    << " ";

    }

};

int main() {

    BD bd(11, 22);
    return 0;

}



output:

11 0 22 0

Last edited on
1
2
3
4
5
6
7
8
//BD(int x1, int y1) : BB(x1),bb(y1) 
//is equivalent to
//BD(int x1, int y1) : BB(x1, 0),  bb(y1, 0)
//                      ^            ^
//                      |            |
//initializes base part (this->x/y)  |
//                                   |
//                     initializes bb (bb.x/y)) 
Topic archived. No new replies allowed.