since I don't feel super comfortable with protected, private inheritance in C++ I googled it up and came up with this stackoverflow answer: Difference between private, public, and protected inheritance . Nice! I thought, let us try this out. So I wrote a small example program to test this and wrote the exptected output as comment.
class Person{
public: virtual void publicInterface() {}
protected: virtual void protectedInterface() {}
private: virtual void privateInterface() {}
};
class Professor : public Person {};
class Teacher : protected Person {
public: void teachPublic(){publicInterface();}
public: void teachProtected(){protectedInterface();}
public: void teachPrivate(){privateInterface();} // not compiling
};
class Student : private Person {
public: void learnPublic(){publicInterface();}
public: void learnProtected(){protectedInterface();}
public: void learnPrivate(){privateInterface();} // not compiling
};
int main()
{
Person* p = new Person(); // ok is-a
Person* pro = new Professor(); // ok is-a
Person* t = new Teacher(); // not compiling! No is-a relat.
Person* s = new Student(); // not compiling! No is-a
Teacher* t2 = new Teacher(); // ok
Student* s2 = new Student(); // ok
pro->publicInterface(); // ok
t2->publicInterface(); // not compiling
s2->publicInterface(); // not compiling
t2->teachPublic(); // ok
t2->teachProtected(); // ok
t2->teachPrivate(); // not compiling
s2->learnPublic(); // ok
s2->learnProtected(); // not compiling <-- compiles, but why?
s2->learnPrivate(); // not compiling
}
Running this it does mostly exactly what I would expect. However, the second last line seem to compile - which is kind of not exptected from the described behaviour of private inheritance.
Has someone an idea why this is compiling?