I am new to c++ , what stuck me is if define a virtual function in the private part of the base class and same function is overridden in child class which is in public section still its giving compiler error below is the code
#include<iostream>
using namespace std;
class Base
{
int x;
virtual void fun()
{
}
public:
int getX() { return x; }
};
// This class inherits from Base and implements fun()
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};
int main(void)
{
Base *d = new Derived;
d->fun();
return 0;
}
When vtable is created it will contain the overridden function in it, so while calling it it should invoke the child class overridden function, which is public. Why is it giving an error?