I'm learning about C++ and I have a question.
I'm learning about virtual function, and I found saw why do we need virtual functions in cpp.
I tried the answer and now I know why do we need virtual function, but I don't understand that,
below code works fine.
class a
{
public:
virtual void eat(){ std::cout<<"Eating A food." << endl;
};
class b : public a
{
public:
void eat(){ std::cout<<"Eating B food." << endl;
}
void eatFunction(a * alpha){ alpha -> eat(); }
int main(void)
{
a * apple = new a;
b * banana = new b;
eatFunction(apple);
eatFunction(banana);
return 0;
}
But I just changed class b: public a
into class b : a
error occured:
error: 'a' is an inaccessible base of 'b'
eatFunction(banana);
When should I use 'public' inheritance parents class?
And what is different?