Actually I have tried to execute this code with little modification to make specify base
class virtual function in private section and I got error from compiler. because in compile
time first control goes to base class function but it is defined in private section so
compiler throw error . Whenever you declare a object of the derived class taking the
reference of base class. In compile time control goes to base class first and every class
makes its own virtual table and then run time the pointer object invoke derived class
function and no matters where it is declared private or public as I said earlier.
class A {
virtual void f() {std::cout << "A::f()" << std::endl;}
};
class B : public A {
private:
// Automatically virtual, because of base class
void f() {std::cout << "B::f()" << std::endl;}
};
int main(int argc, const char *argv[])
{
A *pB = new B();
pB->f();
return 0;
}