I can't understand the output of this code. I expect 10 to be printed, but the output is 85. Can someone explain what's happening?
#include <iostream>
using namespace std;
class A
{
public:
int x=3;
A(int a) : x(a) {}
};
class B: public A
{
public:
int y = 10;
B() : A(y) {}
};
int main()
{
B b;
cout << b.x << endl;
return 0;
}
But, changing it to:
class B: public A
{
public:
int y = 10;
B(int s) : A(s) {}
};
int main()
{
B b(4);
cout << b.x << endl;
return 0;
}
It works as expected (prints 4).