First, let's look atCode:
# Include <iostream> using namespace STD; class base {public: Virtual void fun () {cout <"base" <Endl ;}}; class son: public base {public: void fun () {cout <"son" <Endl ;}}; void DIS (base & S) {S. fun () ;}int main () {base A; DIS (a); son s; DIS (s); Return 0 ;}
Output: No problem with base son
If the following code is used:
# Include <iostream> using namespace STD; class base {public: Virtual void fun () {cout <"base" <Endl ;}}; class son: public base {PRIVATE: // modify void fun () {cout <"son" <Endl ;}}; void DIS (base & S) {S. fun () ;}int main () {base A; DIS (a); son s; DIS (s); Return 0 ;}
The virtual function is declared as private in the subclass, but the result is still the above and there is no compilation error.
Next:
# Include <iostream> using namespace STD; class base {public: Virtual void fun () {cout <"base" <Endl ;}}; class son: private base // {public: // note that public void fun () {cout <"son" <Endl ;}}; void DIS (base & S) {S. fun () ;}int main () {base A; DIS (a); son s; DIS (s); Return 0 ;}
Compilation error, Main. cpp: 29: 10: Error: 'base' is an inaccessible base of 'son'
It can be seen that the accessibility of the virtual functions in the derived class is related to the Inheritance Method and the declaration method (public or private) of the virtual functions in the base class) it has nothing to do with the method declared by the derived class (such as public inheritance, but declared as private, but still accessible)