We know that classes in C ++ can be inherited in multiple ways, which is also in line with the actual situation. For example, a beautiful woman around a tall green hat man may have several identities at the same time, wife, third child, and mother child. in this case, the beauty must inherit these three classes at the same time. however, there may be some defects due to inheritance, such as the ambiguity. in addition, although it is convenient to inherit more ideas directly, it is not impossible to do without it. in fact, if there are fewer classes and fewer object-oriented objects, there is no way to do anything. For example, the C language and no class are not well used. so many things in this world are not so essential. for example, if you sacrifice the world with honor tomorrow, it will still work.
Considering the defects of Multi-inheritance, classes in C # simply do not allow multi-inheritance. However, an exception is that interfaces can be inherited multiple times. The following describes the multi-inheritance of interfaces.
C # interface multi-Inheritance
Assume there are two interfaces: iwife and ilover.
Internal interface iwife
{
Void doit ();
Void cleanhouse ();
}
Internal interface ilover
{
Void doit ();
}
Then there is a class girl that inherits the two interfaces.
/* We found that the interface modifier is internal, but the class modifier is public. if you do not inherit an interface or inherit an internal class, an error is returned. because the permission of the subclass cannot be greater than that of the parent class. why is the inheritance interface an exception?
// I think the interface is empty and there is no protection. I don't want to limit it. in addition, if a class is internal, but the function can be public. then it is useless even if the function access permission is greater than the class. after all, you must use the class to compile functions in the class */
Public GIRL: iwife, ilover
{
Public void doit () // This function is available in both interfaces. This function can be implemented only when one interface is inherited.
{
Console. Write ("do something happy and interesting ");
}
Public void cleanhouse () // The Public modifier is essential, otherwise an error will be reported.
{
Console. Write ("clean house is boring ");
}
}
Functions with the same name in the interface
This is not a problem. however, you may see that the above two interfaces have the same function doit, which is inherited and processed as a function. what if I want to separate them?
In the above girl, add the following two functions.
Void iwife. doit () // Note whether the two functions can add any modifier. If the public function is added, an error occurs.
{
Console. Write ("Do something with husband ");
}
Void ilover. doit ()
{
Console. Write ("Do something with a handsome boy ");
}
After adding these two functions, we will find an interesting problem. the added functions cannot be used in the girl class. it cannot be called inside the class, and it cannot be called outside. because no modifier is private by default.
How can I add two functions in this way? Yes
Girl beautifulgirl = New Girl ();
(Iwife) beautifulgirl). doit (); // The result is do something with husband
(Ilover) beautifulgirl). doit (); // The result is do something with a handsome boy
Beautifulgirldoit (); // The result is do something happy and interesting.
The mechanism behind it is unclear. however, the results show that the doit function is a bit like a virtual function. in the subclass, prefix rewriting with the interface name is equivalent to implementing the corresponding doit function in the interface. however, the sub-class girl overwrites doit.
C ++ class multi-Inheritance
It is said that there is no interface in the Standard C ++, so here we will not talk about the multi-inheritance of interfaces, but the multi-inheritance of classes. the usage is similar to that of the C # interface. the following describes only the mistakes that are easy to make in multiple inheritance.
Class Object
{
Public:
Void doit () {cout <"hit bad boy." <Endl ;}
}
;
Class AA: Object
{
};
Class BB: Object
{
};
Class Arwen: AA, BB
{
};
The above is a simple example. There is a parent class object, and the Class AA and BB inherit from it respectively. Finally, the class Arwen inherits both AA and BB.
If you call the API as follows, the following error occurs: ambiguous access of "doit ".
Int _ tmain (INT argc, _ tchar * argv [])
{
Arwen weiwen;
Weiwen. doit ();
Return 0;
}
Because both AA and BB inherit the doit function, and Arwen inherits the doit function. then, when doit is called, you do not know whether to call the doit inherited from AA or the doit inherited from BB. An error is returned.
Well, what should we do? If all the classes such as object, AA, and BB are provided by class libraries, we can't change them.
Only a new doit function can be rewritten in the Arwen class. For example
Class Arwen: AA, BB
{
Void doit () {cout <"oh, my god. Do it myself." <Endl;
};
This is really a bit cool. If you want to inherit a function to play and the result is wrong, you have to write another one on your own.