Use virtual: If the method is called by a reference type or pointer rather than an object, it determines which method to use. If the keyword irtual is not used, the program chooses the method based on the reference type or pointer type, and if irtual is used, the program chooses the method based on the type of object that the reference or pointer points to. For a function Viewacct (), if VIEWACCT () is not virtual, the program behaves as follows:
Behavior with Non-virtual Viewacct ()
Method chosen according to reference type
Brass Dom ("Dominic Banker", 11224, 4183.45);
Brassplus dot ("Dorothy Banker", 12118, 2592.00);
Brass & b1_ref = dom;
Brass & b2_ref = dot;
B1_ref. Viewacct (); Use BRASS::VIEWACCT ()
B2_ref. Viewacct (); Use BRASS::VIEWACCT ()
The reference variable is of type brass, so Brass::viewaccount () is selected.
When you use the brass pointer instead of a reference, the behavior is similar.
If Viewacct () is virtual, the behavior is as follows:
Behavior with Virtual Viewacct ()
Method chosen according to object
Brass Dom ("Dominic Banker", 11224, 4183.45);
Brassplus dot ("Dorothy Banker", 12118, 2592.00);
Brass & b1_ref = dom;
Brass & b2_ref = dot;
B1_ref. Viewacct (); Use BRASS::VIEWACCT ()
B2_ref. Viewacct (); Use BRASSPLUS::VIEWACCT ()
Both of the referenced types are brass, but b2_ref refers to a Brassplus object, so Brassplus::viewacct () is used. When you use the brass pointer instead of a reference, the behavior is similar.
Virtual destructor: The base class declares a virtual destructor. This is done to ensure that the destructor is called in the correct order when the derived object is disposed.
3. Demonstrating the behavior of virtual methods
Assuming that you want to manage both the brass and Brassplus accounts, it would be helpful if you could use the same array to hold brass and Brassplus objects, but this is not possible. The types of all elements in an array must be the same, while brass and brassplus are different types. However, you can create an array of pointers to brass. This way, each element has the same type, but because the public inheritance model is used, the brass pointer can point to either the Brass object or the Brassplus object. Therefore, you can use an array to represent multiple types of objects. This is polymorphism.
4. Why virtual destructors are required
In Listing 13.10, using Delete to release the code for the object allocated by new illustrates why the base class should contain a virtual destructor, although sometimes it does not seem to require a destructor. If the destructor is not virtual, the paper is carved with a destructor that corresponds to the type of the pointer. For program listing 13.10, this means that only brass destructors are called, even if the pointer is pointing to a Brassplus object. If the destructor is virtual, the destructor for the corresponding object type is called. Therefore, if the pointer is pointing to a Brassplus object, the destructor for the Brassplus is called, and the destructor for the base class is automatically called. Therefore, using a virtual destructor ensures that the correct destructor sequence is called!
C + + uses virtual in inheritance