15.6 pure virtual functions
Write = 0 after the function list to specify the pure virtual function.
Defining a function as a pure virtual function (pure virtual function) indicates that this function provides an interface that can be overwritten for future generations, but the version of this class will never be called. It is important that you cannot create objects of the type containing pure virtual functions.
Classes that contain (or inherit) one or more pure virtual functions are abstract base classes ). You cannot create an abstract object except as a part of the object of the derived class of the abstract base class.
Class Base
{
Public:
Virtual int Get () = 0;
};
Class Child: public Base
{
Public:
Int Get ()
{
Return 10;
}
};
Base & c1 = Child ();
Cout <c1.Get () <endl; // 10
15.7 container and inheritance
Because the derived class object will be "Cut off" when assigned to the Base class object, it is easy to not integrate well through inheritance-related types.
The only feasible option is to use the container to save the object pointer. This policy is feasible, but the cost is that you need to manage objects and pointers. You must ensure that the objects to be pointed exist as long as the container exists. If an object is dynamically allocated, you must ensure that the object is released properly when the container is eliminated. The reason is: The reference type and pointer static type and dynamic type can be different, the cornerstone of polymorphism.
15.8 handle class and inheritance
C ++ objects cannot be used to support object-oriented programming. Instead, pointers or references must be used.
Handle class storage and management base class pointer. The type of the object indicated by the pointer can be changed. It can be a base class object or a derived class object. You can use the handle class to access the operation at the inheritance level. Because the handle class uses pointers to perform operations, the behavior of virtual members will change according to the type of objects actually bound to the handle at runtime, handle users can obtain dynamic behaviors without having to worry about pointer management.
From xufei96's column