class A
{
public:
virtual void fun()
{
cout << "A Fun";
}
};
class B:public A
{
private:
void fun()
{
cout << "B Fun";
}
};
class C:public B{};
int main()
{
//Case1
A *ptr = new C();
ptr->fun();
/*
OUTPUT : B Fun
*/
//Case 2
B *ptr2 = new C();
ptr2->fun();
/*
ERROR:
main.cpp: In function ‘int main()’:
error: ‘virtual void B::fun()’ is private within this context
ptr2->fun();
^
note: declared private here
void fun()
^~~
*/
return 0;
}
In case 1 : I am able to call the private fun() in class B , but why I am not able to call the private fun() in case 2? Why fun() in class B is having two different kind of behaviour? I mean to say that when I make pointer of type A then fun() of class B act as public function but when I make pointer of type B then fun() of class B act as private function.