I'm trying to override virtual methods of super class in the bottom most derive class in multilevel inheritance scenarios. I have two cases here-
Case 1: Sub class(bottom most) overriden function is protected
class A
{
public:
virtual void f1()
{
cout<<"A::f1() called"<<endl;
}
};
class B: public A
{
public:
};
class C: public B
{
protected:
void f1()
{
cout<<"C::f1() called"<<endl;
}
};
int main()
{
A* a= new C;
a->f1();
}
This is working fine even when we calling a protected function C::f1() from main(). But the second case is giving me compilation issue, as i'm making A::f1() as protected.
Case 2:Super class virtual function is protected
class A
{
protected:
virtual void f1()
{
cout<<"A::f1() called"<<endl;
}
};
class B: public A
{
public:
};
class C: public B
{
public:
void f1()
{
cout<<"C::f1() called"<<endl;
}
};
int main()
{
A* a= new C;
a->f1();
}
Please help me why in second case its compilation error while the case:1 f1() is called fine?