Example of Base Constructors and Derivation


/************************************************************************
*

The following example shows how to call base constructors from derived classes:

                                                                        *
************************************************************************/

class B1
{
      int b;
public:
      B1();
      B1(int i) : b(i) { /* ... */ }
};
class B2
{
      int b;
protected:
      B2();
      B2(int i);
};
B2::B2(int i) : b(i) { /* ... */ }
class B4
{
public:
      B4();         // public constructor for B4
      int b;
private:
      B4(int);      // private constructor for B4
};
//      .
//      .
//      .

class D : public B1, public B2, public B4
{
      int d1, d2;
public:
      D(int i, int j) : B1(i+1), B2(i+2) ,
                        B4(i) {d1 = i; d2 = j; }
            // error, attempt to access private constructor B4()
      D(int i, int j) : B1(i+1), B2(i+2) {d1 = i; d2 = j;}
            // valid, calls public constructor for B4
};