#include iostreamusing namespace std; class Base publicvirtual ~Base;;Base~Base cout Base destructor endl; class Derived public Basepublic Derived; ~Derived;private int p;;DerivedDerived p = new i
The output of the program will be:
Derived destructor Base destructor
Explanation:
- The program defines two classes: Base and Derived. Derived is a subclass of Base.
- Both classes define a destructor. The destructor of Base is virtual, which means it can be overridden by subclasses.
- The main() function creates a new Derived object and assigns it to a Base pointer. This is possible because Derived is a subclass of Base, and a subclass can be treated as its superclass.
- The fun() function takes a Base pointer as a parameter and deletes it. Since the Base destructor is virtual and the object pointed to by b is actually a Derived object, the Derived destructor is called first, followed by the Base destructor.
- Therefore, the output of the program is "Derived destructor" followed by "Base destructor"
原文地址: https://www.cveoy.top/t/topic/edsw 著作权归作者所有。请勿转载和采集!