Virtual base class
Polymorphism inheritance, when some or all of the direct base classes of a derived class are derived from another common base class, the members inherited from the previous common base class have the same name. In the object of a derived class, the data member of the same name has multiple copies in the memory period. The same member function has multiple mappings and has two meanings.Indirect ambiguity.
The solution is to set the common base class to a virtual base class.
At this time, the members with the same name inherited from different paths have only one copy in the memory, and the same function name has only one ing.
The definition of the virtual base class is as follows:
Class derived class name: base class name in virtual inheritance mode
In the case of multi-inheritance, the scope of the virtual base class keyword is the same as that of the inheritance mode keyword, and only applies to the base class following it.
For example:
Use virtual base class definition
Class car: virtual public automobile // Passenger Car Type
Class wagon: virtual public automobile // minivan class
At this time, the power, show () inherited from different ways in Automobile ()
There is only one copy in sationwago.
In C #, yesThis multi-inherited, But allowMulti-interface implementation
++ ++
Virtual Functions
It is a very important feature introduced by C ++. It provides a "dynamic binding" mechanism, which makes the meaning of inheritance relatively clear. For the users of the inheritance system, the diversity within the inheritance system is "Transparent ". It does not have to care about its inheritance details. It processes a group of "objects" with consistent overall behavior for it.
Features
The base class abstracts general data and operations. For data, if the data member needs to be used in each derived class, it needs to be declared in the base class; for the operation, if this operation is meaningful to each derived class, whether or not its semantics is modified or extended, it needs to be declared in the base class.
Definition Method
Virtual function type function (parameter table)
{
Function body;
}
It can also be implemented at runtime to form a dynamic Association. Using Virtual functions is the basis for implementing dynamic Association. Three conditions must be met for Dynamic Association and editing.
1. compatible type rules should be met
2. Define virtual functions in the base class and redefine virtual functions in the derived class.
3. You must use a member function or pointer to access a virtual function.
Note: virtual functions cannot be static member functions or friend functions, because static member functions and friend functions do not belong to a certain object.
Pure virtual functions (defined in abstract classes)
Is a virtual function described in the base class. It does not define a specific implementation in the base class, requiring each derived class to define the function implementation according to actual needs.A pure virtual function is used to provide a consistent interface for a derived class and implement polymorphism.
Definition Method
Vitual
Function Name (parameter table) = 0;
======================================
The above are some of the methods for implementing multiple dynamics in C ++. How is it implemented in C #?
C #
(The default values are derived classes of objects)
YesUseBaseKeywords
To reference the constructors of the base class.
Declare a virtual base class or (virtual method) by using the virtual keyword. You can also define abstract classes and abstract methods, and then use override, Derived class
To override the base class method.
Open scaling,Disable modification and other principles
Abstract class features
Abstract classes cannot be instantiated.
Abstract classes can contain abstract methods and abstract accessors
The abstract class cannot be modified by the sealed Modifier
Abstract method features
The abstract method is the implicit method of the hermit by default.
Only abstract methods can be declared in abstract classes.
Polymorphism uses heavy load and overwrite.
For details, see
Click the image chain