class A
{
public:
virtual void foo();
};
class B: public A
{
private:
virtual void foo();
};
Is there any problem with the code above? If not then what will happen?
There are different different scenarios are given as in above code for virtual functionality. What should be the basics behind all such questions to answer?
If you are declaring a virtual function of a class when you should declare the class destructor also as virtual.
Public inheritance means the relation "is as". So if the base class has a public method then a derived class should have the same public method that to be "as base".
In your example there is some contradiction. For example
I think part of what he's asking if class B can declare A's virtual function as a virtual too.
You certainly can, although it won't make any difference. Once you've declared a method as virtual in a base class, then all methods that override it in derived classes are considered virtual, regardless of whether you use the virtual keyword in the declarations of those methods in the derived classes. If it's virtual in the base class, it's virtual from then on.
I would say that it's good practice to put the virtual keyword in there anyway, as it makes it obvious at a glance that it's virtual, without needing to look at the base class.