Can virtual functions be private in C++?
In C++, virtual functions can be private and can be overridden by the derived class. For example, the following program compiles and runs fine.
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
There are few things to note in the above program.
b_ptr is a pointer of Base type and points to a Derived class object. When ptr->print() is called, print() of Derived is executed.
This code works because base class defines a public interface and derived class overrides it in its implementation even though derived has a private virtual function.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above