Can we have a private virtual method ?
Answer Posted / sriram
We can have the private virtual method. But it can be
accessed only through the derived class not through the
base class.
class A
{
private:
virtual void fun() { std::cout << "A::fun" <<
std::endl; }
};
class B : public A
{
public:
virtual void fun() { std::cout << "B::fun" <<
std::endl; }
};
int main(int argc, char* argv[])
{
A* pa = new A();
((B*)(pa))->fun();
}
Output : A::fun
| Is This Answer Correct ? | 4 Yes | 3 No |
Post New Answer View All Answers
Why is static class not inherited?
Why do we use class in oops?
What are the types of abstraction?
Why is abstraction used?
Why do while loop is used?
Why multiple inheritance is not allowed?
What is polymorphism and types?
How Do you Code Composition and Aggregation in C++ ?
why reinterpret cast is considered dangerous?
Can bst contain duplicates?
What is Difference Between Inheritance and creating object and getting data? means Class A extends B{ B.getMethod();} (OR) Class A{ b obj=new B(); obj.getMethod(); }
What is the problem with multiple inheritance?
What is overloading and its types?
What is class encapsulation?
What is the difference between encapsulation and polymorphism?