If we have three classes A,B,C declared as follows:

class A{
protected:
    int x;
};
class B : A{
//...
};
class C : B{
//...
};

does class C see its member x?
It seems to me that it doesn't according to what the compiler says to me...

Since there is no inheritance type specified, the default is private. Therefore your public and protected members become private and your private members are not accessible to your derived class. Your protected variable from A is then becoming private in B, which is then not accessible from C.

I think the solution is to use public inheritance:

Class B: public A
{
};

Class C: public B
{
};

Edited 6 Years Ago by jonsca: base <->derived

Flip back (or forward) a chapter or check out this.

Essentially using one kind or another determines the access (public, private, or protected) of the data members in the derived class based on their respective access types in the parent class.

What is the public inheritance?

C++ has 3 levels of inheritance (private, protected, and public). Although they are partially-related, these do not have the same meaning as the internal access levels within a class. Think of this version as meaning "the members of the base class will be shifted so that the public members will have the access level (inheritance level) in the derived class".

The 'public' inheritance level is the one you want for "true" inheritance. This level keeps the access levels of the base class' members the same as in the base class.

Using protected or private inheritance is more restrictive and less like "true" inheritance.

This article has been dead for over six months. Start a new discussion instead.