In the following code I would expect to get an output of 5, but instead I get arbitrary trash. Why is x not being set to 5? Wouldn't the constructor be invoked when I declare the derived class object and set the value of x to 5?
#include <iostream>
using namespace std;
class Base{
public:
int x;
Base(){
}
Base(int arg)
{
x = arg;
}
};
class Derived: public Base{
public:
Derived():Base(5){
}
};
int main() {
Base obj2;
Derived obj1;
cout << obj2.x;
return 0;
}