Chapter 13 Inherited and combined
First, inheritance and combination
Combination: Simply create a new class containing existing class objects, which is called a combination because this new class is combined by an object already existing. For the new class's public interface function, it contains the use of embedded objects, but does not have to imitate the interface of the embedded object.
Inherit: Create a new class as a type of existing class, take this existing class, add code to it, but does not modify it.
Second, the order of constructor and secting functions
The construct begins at the most root of the class level, and at each layer, first call the base class constructor, then call the member object constructor. Calling the destructor is strictly in the opposite order of constructor - it is important because there is a potential correlation. For members objects, the order of constructor calls is completely unfolded in the order of the initialization expressions in the constructor. This order is determined by the order declared by member objects in the class.
Third, non-automatic inheritance function
The constructor and the destructive function are used to handle the creation and destructors of the object, they only know what to do with objects in their special hierarchy. Therefore, all constructor and destructuring functions in the entire level must be called, that is, constructors and destructor cannot be inherited. Operator = cannot be inherited because it completes activities similar to constructor.
Fourth, the choice of combination and inheritance
The combination usually uses when there is existing class performance inside the new class, and does not want the class as an interface as it. That is to say, embed an object that plans to implement new class performance, and the new class of users see the newly defined interface instead of the interface from the old class.
Because it is a new class that is being existing classes, and I hope this new class has strict interfaces with existing classes (I hope to add any other member functions we want to join), so I have used this. Use this new class anywhere in the existence, which is where inheritance must be used.
V. Private inheritance membership
Chapter 14 Polymorphism and Virtual Functions
How to achieve evening bundle of C
The compiler creates a table (called vtable) for each class that contains virtual functions. In vtable, the compiler places a virtual function address of a particular class. In each class with virtual functions, the compiler secretly places a pointer, called vpointer, pointing to the VTABLE of this object. When you make a virtual function call via the base class (That is, when doing polymorphism calls), the compiler is static inserted into this VPTR, and the code of the function address is found in the vTable table, which can call the correct function to make the evening bundle . Set VTABLE for each class, initialize VPTR, invoke code for virtual functions, all of which are automatically occurring, so we don't have to worry about it. Using virtual functions, the appropriate function of this object can be called, even if the compiler does not know the specific type of object.
If there is no data member, the C compiler will force this object to be non-zero length because each object must have a cross-distinguishing address. If we imagine an index in an array of zero-length objects, we will understand this. A "dumb" member is inserted into the object, otherwise this object has zero length.
C provides a mechanism for this called a pure virtual function. Here is its declaration syntax: Virtual void x () = 0; doing this, equal to telling the compiler to retain an interval for the function in V TA B L E, but do not place the address in this particular interval. As long as there is a function being declared in the class as a pure virtual function, V Ta B L e is incomplete. A class containing a pure virtual function is called a pure abstract base class. What does the compiler do when a class V TA B L E is incomplete? When someone tries to create this class object, what is the compiler? Since it does not securely create a pure abstract class object, if we try to make a pure abstract class object, the compiler issues an error message. In this way, the compiler guarantees the purity of the abstract class, and we don't have to worry about misuse. The pure virtual function prevents V Ta B L E, but this does not mean that we don't want to generate a function of other functions. We often want to call a base class version of a function, even if it is virtual. This is a good idea to put the public code as close as possible to our class level. This not only saves the code space, but also allows the transmission of changes to easily.
Object slice:
When the object is processed polymorphically, the transmission address is significantly different from the pass value. All examples already seen here and the examples that will see are allocated, not pass values. This is because the address has the same length, the address of the transmittive type (which is typically slightly slightly larger) objects and biopsy class (which is usually small) object's address is the same. If object is used instead of using address or reference Metaping, what happens will surprise us: this object is "slice" until the remaining sub-objects suitable for purposes.
#include
Class base {public: base (int J) {cout << "Base ()" << endl; i = j;} base (base & b) {cout << "Base &) << Endl; i = 100;} Virtual int sum () {RETURN I;}; private: INT i;
Class Derive: Public Base {Public: Derive (int M, INT K): base (k) {cout << "Derive () << endl; j = m;} int sum () {return (base :: sum () j);} private: int J;};
Void call (base b) {cout << b.sum ();
MAIN () {
Base A (1); Derive B (1, 2); Call (b);
}
Third, the construction function call order All base class constructors are always called in the inheritance class constructor. Because the constructor has a special work: ensuring that the object is established correctly. Derived class only access its own member, without accessing the base class, only the base class constructor can properly initialize its own members. If the base class constructor is explicitly called in the constructor initialization expression table, it calls the default constructor. If there is no default constructor, the compiler will report an error. When inherited, we must fully know that the base class and any public and protected members can access the base class. This means that when we are in derived classes, you must affirm that all members of the base class are valid. In the usual member function, the structure has occurred, so members of all parts of this object have been established. However, in the constructor, you must find ways to ensure that all our members have been established. To ensure that its unique way is to let the base class constructor first call. This way, when we are in the derived class constructor, all members we can access in the base class have been initialized. In the constructor, "must know that all member objects are valid" is also the reason for the following: As long as it may, we should initialize all members objects in this constructor (in the synthetic class) . As long as we follow this practice, we can guarantee that all base class members and members of the current object have been initialized. Chapter 15 Templates and Packages
First, package container ownership issues
The objects pointed to by a pointer included in a package container are only used for the package container itself. In this case, the ownership problem is simple and intuitive: the package container has the object points to which these pointers are directed. Since most of them are all described above, the case where the package container is fully owned by the object points to the pointer to the package container is defined as the default situation. The best way to handle ownership issues is to choose from user programmers. This often uses a parameter of the constructor, which defaults to ownership (for a typical ideal simple program). There are also read and setup functions to view and correct ownership of the package container. If there is a function of deleting an object in the package container, the status of the ownership of the package container affects deletion, so we can also find options for controlling the destructor in the delete function. We can add ownership information to each member in the bag container, so that each location knows if it needs to be destroyed, this is a reference count variable, here is a package container instead of an object knows the reference to the object number.