I haven't written an article for a long time. I have been reading other people's blogs. I also wrote an article today. One is to reward the knowledge of netizens, and the other is to improve myself.
Disclaimer: Due to my limited knowledge, if any, direct correction to avoid misleading you!
Overload: The function name is the same, and the return type and parameter can be different. When the compiler links, the parameter type check will be added (C does not check the function parameter type, so C does not overload)
Overwrite: When a class is inherited, the base class function does not work. Note that the base class pointer or reference is used to access the derived class (an important feature of the virtual function, the embodiment of polymorphism, rtti Technology)
Hide: It appears in function overload. This can happen if a non-virtual function is used.
Check the code and run the following code:
Class firstbase
{
Public:
Virtual void fun1 () {cout <"base: fun1" <Endl ;}
Void fun2 () {cout <"base: fun2" <Endl ;}
// Void fun3 {cout <"base: fun3" <Endl ;}
};
Class drivedfrombase: Public firstbase
{
Public:
Virtual void fun1 () {cout <"drivedfrombase: fun1" <Endl ;}
Void fun2 () {cout <"drivedfrombase: fun2" <Endl ;}
Void fun2 (int x) {cout <"drivedfrombase: fun2" <x <Endl ;}
Void fun3 () {cout <"drivedfrombase: fun3" <Endl ;}
};
Class secondbase
{Public:
// Void fun1 () {cout <"secondbase: fun1" <Endl ;}
// Void fun2 {cout <"secondbase: fun2" <Endl ;}
Void fun3 () {cout <"secondbase: fun3" <Endl ;}
};
Class derivedfromsecondandfirstbase: Public drivedfrombase, public secondbase
{
Public:
Void g () {cout <"derivedfromsecondandfirstbase: fun1" <Endl ;}
Void H () {cout <"derivedfromsecondandfirstbase: fun2" <Endl ;}
// Void fun3 {cout <"derivedfromsecondandfirstbase: fun3" <Endl ;}
};
The inheritance relationship is:
Firstbase-> drivedfrombase->
Derivedfromsecondandfirstbase.
Secondbase->
Int main ()
{
// Ambiguity, non-Overload
Derivedfromsecondandfirstbase * P = new derivedfromsecondandfirstbase ();
// P-> fun3 (); // This sentence will return an error, which may be mistaken for function overloading. However, fun3 is of a different class, and therefore cannot be overloaded, resulting in ambiguity.
// Overwrite
Drivedfrombase * DB = new drivedfrombase ();
DB-> fun1 (); // use a virtual function to overwrite the fun1 of the base class.
// Hide
DB-> fun2 (2 );
Firstbase * fp = dB;
FP-> fun2 (); // hide a function that can be accessed through pointer transformation.
// Unable to call
// FP-> fun3 (); // If a function is not a virtual function, you cannot use the base class pointer to access a part that is not a base class, such as fun3.
Return 0;
}
Of course, this part is still very complicated and cannot be explained from the bottom layer for the time being.
Running result: