Q:

Can virtual functions be private in C++?

0

Can virtual functions be private in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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;
}

OutputDerived Function

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions For Experienced

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is an abstract class in c++?... >>
<< Can we access private data members of a class with...