Inheritance Issue

This my code
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
47
48
49
50
51
52
53
54
55
//.h file code:

#include <string>
#include <iostream>
using namespace std;

	class One
	{
	public:
		int a;

		One();
		One(int a);
	};
	class Two
	{
	public:
		One *uno;
		int b;
		Two();
		Two(int b, int a);
	};


//.cpp file code:

One::One()
{
	a = 0;
}

One::One(int a)
{
	this->a = a;
}

Two::Two()
{
	uno = new One();
}

Two::Two(int b, int a)
{
	this->b = b;
uno = new One(a);
}

static void main()
{
	Two *t1 = new Two();
	Two *t2 = new Two(2,3);
	std::cout << t1->b << " " << t1->uno->a << std::endl;
	std::cout << t2->b << " " << t2->uno->a << std::endl;

}


My output:
-842150451 0
2 3
Press any key to continue . . .

Desired output:
0 0
2 3
Press any key to continue . . .

Why is t1->b not displaying 0 but -842150451?


Because you are not initializing it in the constructor
I am not following where am I not initializing the constructor?
and if so how do I initialize the constructor?
Oh i see Two *t1 = new Two(0,0);
Thank You
Last edited on
Topic archived. No new replies allowed.