P
PSN
Hi all ..
can any one please explain the output of the following code ..
class A {
public:
int a1;
int a2;
A(int a1, int a2)
{
cout << "i am in AAA" << endl;
this->a1 = a1;
this->a2 = a2;
}
};
class B : public A {
public:
int b1;
int b2;
B(int b1, int b2, int a1, int a2) : A(b1, a2)
{
cout << "i am in BBB" << endl;
this->b1 = b1;
this->b2 = b2;
}
};
int main()
{
B test(10, 20, 30, 40);
cout << test.b1 << " " << test.b2 << " " << test.a1 << " " << test.a2
<< endl;
}
The result is:
I am in AAA
I am in BBB
10, 20, 10, 40
The constructor of A is called first .. therefore when i pass an
uninitialized value, "b1", to the constructor of the base, i was
expecting a crash or some junk value .. but i see that a1 is
initialized to 10. But when i comment out the initialization statement
of b1 (this->b1 = b1), i see a junk value for the variable a1 ... Can
someone please explain me this ..
thanks ...
can any one please explain the output of the following code ..
class A {
public:
int a1;
int a2;
A(int a1, int a2)
{
cout << "i am in AAA" << endl;
this->a1 = a1;
this->a2 = a2;
}
};
class B : public A {
public:
int b1;
int b2;
B(int b1, int b2, int a1, int a2) : A(b1, a2)
{
cout << "i am in BBB" << endl;
this->b1 = b1;
this->b2 = b2;
}
};
int main()
{
B test(10, 20, 30, 40);
cout << test.b1 << " " << test.b2 << " " << test.a1 << " " << test.a2
<< endl;
}
The result is:
I am in AAA
I am in BBB
10, 20, 10, 40
The constructor of A is called first .. therefore when i pass an
uninitialized value, "b1", to the constructor of the base, i was
expecting a crash or some junk value .. but i see that a1 is
initialized to 10. But when i comment out the initialization statement
of b1 (this->b1 = b1), i see a junk value for the variable a1 ... Can
someone please explain me this ..
thanks ...