If I write f.do_nothing() obviously it will give me compilation error. In this case how is access specifier resolved? All I know is how virtual function is working but I don't get how access specifier is resolved because I made the function private so even after resolving how is d or dee accessing
do_nothing().
If I am wrong here someone will correct me. I believe it all comes down to inheritance. "derived_1" inherits from "derived" which inherits from "demo". If I have this correct "derived_1" would look something like this because of the inheritance:
1 2 3 4 5 6 7 8 9 10 11 12
class derived_1 : public derived
{
// Not actually coded here, but came from demo.
public:
voidvirtual do_nothing() = 0;
private:
void do_nothing()
{
cout << "DO NOTHING" << endl;
}
};
So, whether "void virtual do_nothing() = 0;" is inherited from "demo" or defined in "derived_1" it works the same. That is why "d" and "dee" both print the message.
I think the function is virtual in all cases. It's just public in classes demo and derived, and private in class derived_1. This is perfectly legal although a little strange.
a. Access to a member is checked at compile time; based on the compile-time type (static type).
In dee->do_nothing(); the type of dee is 'pointer to demo'. In the class demo, the member do_nothing() is public.
b. A call to a virtual functions (selected via unqualified name lookup) is dispatched at run time, based on the run-time type (dynamic type) of the object.
c. The accessibility of an overridden virtual function need not be the same as that in the base class.