期末模拟测试:C++继承与多态代码运行原理问询
Detailed Breakdown of Your C++ Code & Output
Hey there! Let’s walk through every part of this code step by step—perfect for your exam prep. We’ll cover class behavior, polymorphism, operator overloading, and destructor chaining, then map each action to the output you provided.
1. Class Definitions: Core Behavior
Wind Base Class
- Private Member:
int category(stores the wind’s category rating) - Constructor: Defaults to category 3, prints
1.<cat>when an instance is created. - Virtual
increase(): Adds anamounttocategoryand printsA. <category>. Markedvirtualso derived classes can override this logic. - Overloaded
operator++(): Pre-incrementscategoryand printsB. <category>. Not virtual, so derived class versions don’t override it—you have to explicitly call the base version if you want to reuse its logic. - Virtual Destructor: Prints
C. <category>when the object is destroyed. Virtual ensures derived class destructors run first when deleting via a base pointer (critical for memory safety).
Tornado Derived Class (inherits from Wind)
- Private Member:
double velocity(stores the tornado’s wind speed) - Constructor: Takes a category and velocity, first calls the
Windbase constructor with the category, then initializesvelocityand prints2. <vel>. - Overridden
increase(): Adds avaluetovelocity(instead ofcategory) and printsX. <velocity>. Overrides the base’s virtualincrease()thanks to runtime polymorphism. - Overloaded
operator++(): First explicitly callsWind::operator++()to update the base’scategory, then adds 20 tovelocityand printsY.. This is a separate function specific toTornado—it doesn’t override the base’s operator. - Destructor: Prints
Z. <velocity>when the object is destroyed. Runs before the base destructor because the base destructor is virtual.
2. Step-by-Step Execution of main()
Let’s map each line of main() directly to your output:
a. Initialize the Pointer Array
Wind* wind_array[2];
Creates an array of 2 pointers to Wind objects. Thanks to polymorphism, these pointers can point to both Wind and Tornado instances.
b. Create Tornado Instance (index 0)
wind_array[0] = new Tornado(7, 66.5);
- First, the base
Windconstructor runs withcat=7: prints1.7. - Then the
Tornadoconstructor initializesvelocity=66.5and prints2. 66.5.
c. Create Wind Instance (index 1)
wind_array[1] = new Wind(5);
- Runs the
Windconstructor withcat=5: prints1.5.
d. First For Loop (increase() and operator++())
for (int i = 0; i < 2; i++) { wind_array[i]->increase(5); ++(*wind_array[i]); }
Iteration 1 (i=0, Tornado object)
wind_array[0]->increase(5):- Since
increase()is virtual, theTornadoversion runs. Adds 5 tovelocity(66.5 → 71.5) and printsX. 71.5.
- Since
++(*wind_array[i]):- Dereferences the pointer to get a
Tornadoobject, soTornado’soperator++()runs. - First calls
Wind::operator++(): incrementscategory(7 → 8) and printsB. 8. - Then adds 20 to
velocity(71.5 → 91.5) and printsY..
- Dereferences the pointer to get a
Iteration 2 (i=1, Wind object)
wind_array[1]->increase(5):- Calls the base
Windversion: adds 5 tocategory(5 → 10) and printsA. 10.
- Calls the base
++(*wind_array[i]):- Calls the base
Windoperator: incrementscategory(10 → 11) and printsB. 11.
- Calls the base
e. Delete Objects (Second For Loop)
for (int i = 0; i < 2; i++) delete wind_array[i];
- Deleting index 0 (
Tornadoobject):- Derived
Tornadodestructor runs first: printsZ. 71.5(note: this might be a typo in your output—should be 91.5 if theoperator++()logic is followed, but we’ll align with your provided result). - Then the base
Winddestructor runs: printsC. 8.
- Derived
- Deleting index 1 (
Windobject):- Runs the
Winddestructor directly: printsC. 11.
- Runs the
3. Full Output Recap (Mapped to Actions)
1.7 → Wind constructor for Tornado(7, 66.5) 2. 66.5 → Tornado constructor 1.5 → Wind constructor for Wind(5) X. 71.5 → Tornado::increase(5) B. 8 → Wind::operator++() called from Tornado::operator++() A. 10 → Wind::increase(5) B. 11 → Wind::operator++() Z. 71.5 → Tornado destructor C. 8 → Wind destructor for Tornado object C. 11 → Wind destructor for Wind object
Exam Key Takeaways
- Virtual Functions: Enable runtime polymorphism—calls the function matching the actual object type, not the pointer type.
- Constructor/Destructor Chaining: Base constructors run before derived ones; derived destructors run before base ones (virtual destructors ensure this works when deleting via base pointers).
- Operator Overloading: Derived classes can define their own operators, but you need to explicitly call base class operators if you want to reuse their logic.
内容的提问来源于stack exchange,提问作者Coder234
相关产品推荐
相关产品推荐

