The derived class D attempts to access the private data field b of the base class. But it has no right to access it. A solution is to create a new member function for class B called get_b() which returns the value of the private data b.
class B
{
public:
B();
B(int n);
void print() const;
int get_b() const; /* return the value of b */
private:
int b;
};
B::B() { b = 0; }
B::B(int n) { b = n; }
void B::print() const { cout << "B: " << b << "\n"; }
int B::get_b() const { return b; }
class D : public B
{
public:
D();
D(int n);
void print() const;
private:
};
D::D() {}
D::D(int n) : B(n) {}
void D::print() const { cout << "D: " << get_b() << "\n"; }