I'm trying to experiments with inheritance in c++. I've written the following code:
#include <stdio.h>
class A
{
public:
virtual void foo();
};
class B: A
{
void foo();
};
void B::foo()
{
printf("Derived class");
}
void A::foo()
{
printf("Base class");
}
int main()
{
A *a= new B();
a->foo();
}
But I've an error described as
test.cpp: In function ‘int main()’: test.cpp:26:14: error: ‘A’ is an inaccessible base of ‘B’
It works fine if I replace the line class B: A
to class B: public A
. But using this fact I really don't understand in what case private
and protected
inheritance may be needed. It's useless for me now.