15.2.2 protected member
It can be considered that the protected access label is a combination of private and public:
Like private members, protected members cannot be accessed by class users.
Like a public member, a protected member can be accessed by a derived class of the class.
A derived class can only access the protected member of its base class through a derived class object. The derived class has no special access permission to the protected member of its base class object.
The interface provided to the derived type is a combination of protected members and public members.
15.2.3 derived class
To define a derived class, use the class derivation list to specify the base class. The class derivation List specifies one or more base classes.
You only need to know the access label to determine the access permissions of the inherited members. If you want to inherit the interface of the base class, you should perform public derivation.
The derived class inherits the members of the base class and can define its own additional members. Each derived class object contains two parts: Members inherited from the base class and custom members. Generally, a derived class only defines aspects that are different from the base class or extend the behavior of the base class.
1. Define a derived class
Class Bulk_item: public Item_base {
Public:
Double net_price (size_t) const;
Private:
Size_t min_qty;
Double discount;
};
2. Derived classes and virtual functions
Although this is not required, the derived class generally redefines the inherited virtual function. If the derived class does not redefine a virtual function, use the version defined in the base class.
The derived type must be declared by each inherited Member of the object to be redefined.
The Declaration of the virtual function in the derived class must exactly match the definition method in the base class, but there is an exception: the virtual function that returns a reference (or pointer) to the base class. A virtual function in a derived class can return a reference (or pointer) of a derived type returned by a base class function ).
Once a function is declared as a virtual function in the base class, it is always a virtual function, and the fact that a derived class cannot change this function to a virtual function. When a derived class redefinition of a virtual function, you can use the virtual reserved word, which is not required.
3. The derived class object contains the base class object as the sub-Object
A derived class object consists of multiple parts: a (non-static) member defined by the derived class and a sub-object composed of a base class (non-static) member.
The C ++ language does not require the compiler to arrange the basic class parts and derived parts of the object in sequence.
4. Functions in the derived class can use the members of the base class.
Like any member function, a derived class function can be defined inside or outside the class.
Double Bulk_item: net_price (size_t cnt) const
{
If (cnt> = min_qty)
Return cnt * (1-discount) * price;
Else
Return cnt * price;
}
Because each derived class object has a base class, the class can access the public and protected members of its base class, just as those members are their own members of the derived class.
5. The class used as the base class must be defined
Each derived class contains and can access its base class members. To use these Members, the derived class must know what they are. This rule implies that it is impossible to derive a class from the class itself.
6. Use a derived class as the base class
The base class itself can be a derived class.
The bottom-layer derived class object contains the sub-objects of each of its direct and indirect base classes.
7. Declaration of a derived class
If you need to declare a derived class, the Declaration contains the class name but does not contain the derived list.
From xufei96's column