Can virtual functions be private in C++?
Answer:
Yes, the virtual function can be private. Let see an example code,
#include<iostream> using namespace std; class Base { public: void test(); private: //private virtual function virtual void fun() { cout << "Base Function"<<endl; } }; class Derived: public Base { public: void fun() { cout << "Derived Function"<<endl; } }; void Base::test() { Derived objDerived; Base *ptr = &objDerived; ptr->fun(); } int main() { Base Obj; Obj.test(); return 0; }
Output: Derived Function
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Answer:
Yes, the virtual function can be private. Let see an example code,
Output: Derived Function
need an explanation for this answer? contact us directly to get an explanation for this answer