i read that constructors are not inherited when we derive a class from another class
That is correct. However, you seem to have misunderstood the meaning of that.
Let's say you have:
struct A
{
A(int) {}
};
struct B : A
{
B() : A(0) {}
};
Given the above, you won't be able to use:
B b(10);
since A(int)
is not inherited by B
.
That's the crux of your misunderstanding.
then why creation of c is invoking constructors from b and a
However, when you construct a B
, a constructor of B
is called to initialize its members. A constructor of A
must also be called so that the sub-object of B
that corresponds to A
can be initialized.
There are couple of ways to initialize the A
-part of B
.
You can use a constructor of A
explicitly in the member initialization list by using the syntax:
B() : A(0) {}
Leave the member initialization empty, in which case the default constructor of A
is called.
B() {}
That is equivalent to:
B() : A() {}
In the example I presented, that will result in a compiler error since the default constructor of A
has been deleted by providing another constructor that is different than the default constructor.
Coming back to your implementation of the default constructor of C
, you have:
C() { cout << "C's constructor called" << endl; }
That is equivalent to
C() : B(), A() { cout << "C's constructor called" << endl; }
B::B()
and A::A()
are called when an instance of C
is constructed.