Environment: VS2010
Code:
// Public:
# Include <iostream>
Using namespace std;
Class {
Public:
Virtual ~ A (){}
Virtual void func (){
Printf ("A: func () \ n ");
};
};
Class B: public {
Private:
Virtual void func (){
Printf ("B: func () \ n ");
};
};
Int main (void ){
A * p = new B;
P-> func ();
Delete p;
}
// Private:
# Include <iostream>
Using namespace std;
Class {
Public:
Virtual ~ A (){}
Virtual void func (){
Printf ("A: func () \ n ");
};
};
Class B: private {
Public:
Virtual void func (){
Printf ("B: func () \ n ");
};
};
Int main (void ){
A * p = (A *) new B;
P-> func ();
Delete p;
}
Observe the output results of the above two programs.
1. shared inheritance. The virtual function of the derived class is private. The output result is as follows:
2. Private inheritance. The virtual function of the derived class is public. The output result is as follows:
The output results are consistent.
Summary: polymorphism is irrelevant to the access permission of member functions. the base class defines virtual functions and is public. therefore, no matter what access permissions (private, protect, and public) the override virtual function is attached to a subclass, the access permissions of the base class shall prevail, that is, public.
Author Wentasy