As already mentioned, you must initialize the elements Aand Bingin the relevant contructor initialization lists. You can also set A::dto the default, so you do not need two constructors in A. That is, you have:
class A {
public:
A(){d=2.2;cout<<d;}
A(double d):d(d){cout<<d;}
double getD(){return d;}
private:
double d;
};
You can rewrite it as:
class A {
public:
A(double d=2.2) : d(d) { cout << d; }
double getD() { return d; }
private:
double d;
};
source
share