I use cpp.sh to compile and run this code. I expect the numberdefault value to be initialized with a value 0or some garbage value. However, in the constructor, although the condition ifis false, the value numberis still initialized with the value 10. Can someone explain to me what is going on?
#include <iostream>
class Number
{
int number;
public:
Number(std::string s)
{
if (s == "foo")
{
number = 10;
}
}
int getNumber()
{
return number;
}
};
int main()
{
Number n("bar");
std::cout << n.getNumber() << std::endl;
}
source
share