Please look at the following code listing:
#include <iostream>
using namespace std;
class Base {
public:
virtual void Message() = 0;
};
class Intermediate : public Base {
};
class Final : public Intermediate {
void Message() {
cout << "Hello World!" << endl;
}
};
int main() {
Final final;
/* Wont work (obviously):
Final* finalPtr = &final;
finalPtr->Message();
*/
// Works:
Intermediate* finalPtr = &final; // or Base* finalPtr = &final;
finalPtr->Message();
return 0;
}
Take note of the following:
Question: If you run the program, the line finalPtr->Message(); successfully invokes Final's implementation of message() function though its private. How does that happen? Does a base class override or ignore a derived class' access restrictions?
Related Question: In relation to (2.) above, what is the right way to define Intermediate class? Does one need to re-declare the pure virtual function message() from the base class bearing in mind that Intermediate class is not intended to provide an implementation.
NOTE: Code was tested with both Digital Mars Compiler (dmc) and Microsoft's Visual Studio Compiler (cl) and works just fine in both
How does that happen? Does a base class override or ignore a derived class' access restrictions?
With Public Inheritance all public members of the Base class become public members of the derived class. So Yes Message()
is public function in Intermediate
.
The function is called on a base class(Intermediate
) pointer the function is public in base class. The dynamic dispatch(i.e the actual call to Derived class function) only happens at runtime hence this works.
The above is due to the fact that at runtime, the access specifiers have no meaning, the access specifier rules are resolved and effective only at compile time.
If you call the function on the derived class pointer then at compile time the compiler detects that Message()
is declared private
in Final
and hence it gives the error.
While deriving from an Abstract class, the derived class MUST provide definition for ALL the Pure virtual functions of the Base class, Failing to do so will result in the Derived class also being an Abstract class.
Here Intermediate
class is an Abstract class and as long as you do not need to create objects of this class, it will work fine. Note that you can create a pointer to an Abstract class.
In C++, virtual and access specifiers are mutually exclusive. That is the reason why in C++, the access can be narrowed for virtual methods whereas in C# or Java that is not possible.
When you try to access the virtual function through base class pointer, the compiler compiles the code since the base class' virtual function is public.
In your commented code, the virtual function having restricted access is called via the Final class pointer. Hence the compilation error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With