Can Virtual Functions be Private in C++?
A virtual function can be private as C++ has access control, but not visibility control. As mentioned virtual functions can be overridden by the derived class but under all circumstances will only be called within the base class.
printing with base class pointer base class constructor derived class constructor show() called on base class print() called on derived class derived class destructor base class destructor
Explanation: ‘b_ptr‘ is a pointer of Base type and points to a Derived class object. When pointer ‘ptr->print()’ is called, function ‘print()’ of Derived is executed.
This code works because the base class defines a public interface and the derived class overrides it in its implementation even though the derived has a private virtual function.