|
//Assume that there are classes Base and Drived as follows:
class Base
{
private:
char name[10];
};
class Drived:public Base
{
//Whether it is public inheritance or private inheritance, the memory layout of the Drived object
//The same as Drived_likeness below, the difference is accessibility and operation on name.
//Because the name here comes from Base, if name is declared private in Base,
//So, although there is a position of name in the Drived object, it cannot be accessed directly.
//The operation depends on the non-private functions of the base class.
//If name is declared as protected or public in Base, functions of this class can be
//Accessed. If it is private inheritance, the accessibility of name becomes private, and public inheritance is
//name accessibility is unchanged (but if name is private in the base class, it is not accessible).
//Wrong view 1: If it is a private parent class member, it is impossible for subclasses to inherit it.
//This seems to be saying that there is no name position in Drived, so wrong.
//Wrong view 2: After subclass inheritance is equivalent to a reference, the definition of parent class is still used when calling
//How did you get the reference?
//Emphasize again: the subclass object is also a whole, including the inherited part from the parent class (such as the name in this example)
//Add a new part with yourself (such as age in this example)
private:
int age;
};
class Drived_likeness
{
private:
char name[10];
int age;
}; |
|