inheritence

i don't understand the output of the program. please help me understand.
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
#include <iostream>
using namespace std;

class A{
protected:
	int i;
public:
	A(int n = 2) : i(n) {}
	~A() { cout << i; }
};

class B : public A {
	A a1;
	A a2;
	A a3;
public:
    B(int n) : a1(i + 1), a2(n), a3(n+1) {}
    ~B() {
        cout << i;
        --i;
    }
};

int main(){	
	B b(5); 
return 0;
}

/*OUTPUT:
26531
*/
closed account (D80DSL3A)
OK. Gonna try.
line 25, b is constructed.

Base object 1st, the inherited A object. The default ctor is used since we see no explicit call to it in the ctor, so inherited i=2
It's as if we have:
1
2
B(int n) : A(2),  a1(i + 1), a2(n),   a3(n+1) {} with n=5
//         b.i=2  b.a1.i=3  b.a2.i=5  b.a3.i=6 



Now for deconstruction, stuff in the dtor body 1st:
~B() {
cout << i;// -> 2
--i;// now b.i=1
}
Next, the members of B (a1,a2,a3) are destroyed, in opposite the order declared:
For refc: ~A() { cout << i; }

~a3 -> 6
~a2 -> 5
~a1 -> 3

Lastly, the base A object is destroyed:
~B::A b.i -> 1

That's 26531

EDIT: Isn't the ninja effect amazing? No replies for 88 mins, then 2 in 5 mins.
Last edited on
B's constructor calls A's constructor. (even when B has no A type objects in line 13 to15)

so there is a first "default" call, then three calls for a1, a2, a3.

~B() works before ~A()
in output, 2 is from line19. 1 from line20.
6,5,3 are from line 9.

notice the order of 6,5,3 it was not 3,5,6 (a1, a2, a3)
line 13 to 15: first declared variable is destructed last.
fun2code wrote:
It's as if we have:
1
2
B(int n) : A(2),  a1(i + 1), a2(n),   a3(n+1) {} with n=5
//         b.i=2  b.a1.i=3  b.a2.i=5  b.a3.i=6  


in line1,A() is called without argument. OP's A ctor has default value 2.

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>
using namespace std;

class A{
protected:
	int i;
public:
	A() : i(2) {cout << "a";}  ///
	A(int n) : i(n) {cout << "A";}  ///
	~A() { cout << i; }
};

class B : public A {
	A a1;
	A a2;
	A a3;
public:
    B(int n) : A(), a1(i + 1), a2(n), a3(n+1) {}  ///
    ~B() {
        cout << i;
        --i;
    }
};

int main(){	
	B b(5); 
return 0;
}


output:
aAAA26531
closed account (D80DSL3A)
@anup30 Possible misinterpretation of my post.
I was just trying to point out why i=2 when a1(i + 1) is reached in
1
2
B(int n) : a1(i + 1), a2(n), a3(n+1) {}// OPs actual code
B(int n) : A(2), a1(i + 1), a2(n), a3(n+1) {}// produces same result 

I wasn't suggesting that extra ctors be added.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class A{
protected:
	int i;
public:
	A(int n = 2) : i(n) {}
	~A() { cout << i; }
};

class B : public A {
	A a1;
	A a2;
	A a3;
public:
    B(int n) : A(2), a1(i + 1), a2(n), a3(n+1) {}// just showing why b.i = 2
    ~B() {
        cout << i;
        --i;
    }
};

int main(){
	B b(5);
return 0;
}

output:
26531

Counterpoint!
anup30 wrote:
in output, 2 is from line19. 1 from line20.

The 1 is produced by line 9 when the base object is finally destroyed.
Line 20 accounts for why i=1 then.
Last edited on
@fun, you got what i meant. there is no point of confusion as line 20 is --i; there is no cout or printf in that line - any beginner can understand, it just says 1 is i's reduced value, reduced in line 20.(so printed from what line? i assumed that OP will figure out, or if he can't he will question.)


why A(2) instead of A() is wrong?

line5, A(int n = 2) : i(n) {}
line14, B(int n) : A(2), a1(i + 1), a2(n), a3(n+1) {}
output: 26531

line5, A(int n = 8) : i(n) {}
line14, B(int n) : A(2), a1(i + 1), a2(n), a3(n+1) {}
output: 26531

line5, A(int n = 8) : i(n) {}
line14, B(int n) : a1(i + 1), a2(n), a3(n+1) {}
output: 86597

line5, A(int n = 8) : i(n) {}
line14, B(int n) : A(), a1(i + 1), a2(n), a3(n+1) {}
output: 86597
closed account (D80DSL3A)
@anup30. You are correct. I agree with your points.

Peace!
thank you fun2code and anup30
i have another problem.
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
#include <iostream>
using namespace std;

class A{
protected:
    int i;
public:
    A() : i(0) { }
};

class B{
protected:
    double d;	
public:
    B() : d(0.0) { }
};

class C : public A, public B{
    char c;
public:
    C() : c('a') { }
};

int main()
{
    C c;
    A *pa = &c;
    B *pb = &c;

    if( reinterpret_cast<char*>(pa) == reinterpret_cast<char*>(pb) ) 
    	cout << "equal";
    else  cout << "not equal";
}


why its "not equal" ?
Topic archived. No new replies allowed.