1. in multi-inheritance, if a class inherits both Class A and Class B, and both Class A and Class B have a function called foo, how to explicitly specify the parent class Foo () of override in the subclass ()?
You must specify the name of the parent class, for example, C. A: Foo (), c. B: Foo ().
2. in multi-inheritance, such as level 3 and virtual functions, "Overwrite" occurs. The principle is that if the parent has an interface, the parent interface is called first, if the parent node does not have any related interfaces, call the grandparent interface.
3. when the C ++ class inherits, the default class is private inheritance. Private inheritance cannot inherit and use the public variables in the parent class function. therefore, you must add the public Keyword: Class derive: Base-> class derive: public base.
(Because private Inheritance and Protection inheritance restrict subclass objects of derived objects to access the base class, some people think that they only have a place in technical discussion)
4. When the base class constructor contains parameters, if the child class inherits them, you need to explicitly call the base class constructor to complete initialization. For example:
Class base
{
Protected: int I;
Public: Base (int x) {I = x ;}
};
Class derived: public Base
{
PRIVATE: int I;
Public: derived (int x, int y): Base (x) // you must ensure that the base type can be constructed smoothly. Otherwise, an error is returned!
{I = y ;}
Void printtotal ()
{
Int Total = I + base: I;
Cout <total <Endl;
}
};
5. When calculating the number of bytes occupied by the inheritance class, there are the following rules:
(1) If there are virtual functions in the class (one or more of them are the same), this class should be 4 more in addition to the number of bytes occupied by members (virtual function pointer)
(2) If the base class has virtual functions, when the inherited class is not virtual, the size of the inherited class is the size of the base class (including the virtual function pointer ), add the size of the data member. the pointer size of the virtual function of the inherited class is not repeated.
(3) If the inheritance class is virtual inheritance, then on the basis of the previous calculation, each level of the inheritance class requires four more bytes (the size of the virtual class pointer )!
Example:
Class {
Public:
Char K [3];
Virtual void AA (){};
Virtual void AAA (){};
}; // The size is 8 bytes.
Class B: Public Virtual {
Public:
Char BK [3];
Int;
Virtual void BB (){};
}; // The size of B is 8 (SIZE OF A) + 8 (number of member variable bytes) + 4 (size of virtual class pointer)
Class C: Public B {
Public:
Char ik [3];
Virtual void CC (){};
}; // The size of C is 20 (the size of B) + 4 (the number of bytes of the member variable)