Dynamic binding
In C ++, dynamic binding occurs when a virtual function is called through the reference/pointer of the base class.
The reference or pointer of a base class can point to both the base class object and the derived class object, which is the key to dynamic binding.
The virtual function called by reference or pointer is determined at runtime. The called function is defined by reference or the actual type of the object currently referred to by the pointer.
Access Control
Public and private labels
User code can be a public member of the category class but cannot access a private member. Private Members can only be accessed by members and friends of the base class.
Protected.
A protected member can be accessed by a derived class object but cannot be accessed by a common user of this type.
Define a derived class.
To define a derived class, use the "class derived list" to specify the base class. Format:
class classname: access-label base-class
Derived classes and virtual functions
A derived class generally redefines the inherited virtual function. If a virtual function is not redefined, the version defined in the base class is used.
The Declaration of the virtual function in the derived class must exactly match the definition method in the base class. But there is one exception:
If the virtual function in the base class returns a reference or pointer to the base class, the redefinition version in the derived class can return a base class reference or pointer, or a derived class reference or pointer.
When to use dynamic binding?
Function calls in C ++ do not use dynamic binding by default. To trigger dynamic binding, two conditions must be met:
First, only member functions specified as virtual functions can be dynamically bound. Use the keyword virtual in class declaration,
Second, the function must be called through the reference or pointer of the base class.
Conversion from a derived class to a base class.
Because each derived class object contains the base class part, you can bind the base class reference to the base class part of the derived class object, or point the base class pointer to the derived class object.
Example:
// Function with an item_base reference parameter
Double print_total (const item_base &, size_t );
Base class reference
Item_base item; // object of base type
// OK: Use Pointer or reference to item_base to refer to an item_base object
Print_total (item, 10); // passes reference to an item_base object
Item_base * P = & item; // P points to an item_base object
The base class reference is bound to the base class of the derived class object.
Bulk_item bulk; // object of derived type // ok: can bind a pointer or reference to Item_base to a Bulk_item object print_total(bulk, 10); // passes reference to the Item_base part of bulk p = &bulk; // p points to the Item_base part of bulk
Overwrite the virtual function mechanism
If you want to override the dynamic binding mechanism of the virtual function and force the function to call a specific version of the virtual function, you can use the scope operator.
Item_base *baseP = &derived; // calls version from the base class regardless of the dynamic type of baseP double d = baseP->Item_base::net_price(42);