You cannot use the value of the (variable)
c
: it is a local variable (its lifetime is scoped in
Base
constructor, that is, after constructor execution,
c
doesn't exist anymore). Try:
class base
{
public:
int c;
base()
{
int a =10;
int b = 20;
c = a + b;
}
};
int main()
{
base a;
cout << "c = " << a.c << endl;
}