You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

期末模拟测试: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 an amount to category and prints A. <category>. Marked virtual so derived classes can override this logic.
  • Overloaded operator++(): Pre-increments category and prints B. <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 Wind base constructor with the category, then initializes velocity and prints 2. <vel>.
  • Overridden increase(): Adds a value to velocity (instead of category) and prints X. <velocity>. Overrides the base’s virtual increase() thanks to runtime polymorphism.
  • Overloaded operator++(): First explicitly calls Wind::operator++() to update the base’s category, then adds 20 to velocity and prints Y. . This is a separate function specific to Tornado—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 Wind constructor runs with cat=7: prints 1.7.
  • Then the Tornado constructor initializes velocity=66.5 and prints 2. 66.5.

c. Create Wind Instance (index 1)

wind_array[1] = new Wind(5);
  • Runs the Wind constructor with cat=5: prints 1.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, the Tornado version runs. Adds 5 to velocity (66.5 → 71.5) and prints X. 71.5.
  • ++(*wind_array[i]):
    • Dereferences the pointer to get a Tornado object, so Tornado’s operator++() runs.
    • First calls Wind::operator++(): increments category (7 → 8) and prints B. 8.
    • Then adds 20 to velocity (71.5 → 91.5) and prints Y. .

Iteration 2 (i=1, Wind object)

  • wind_array[1]->increase(5):
    • Calls the base Wind version: adds 5 to category (5 → 10) and prints A. 10.
  • ++(*wind_array[i]):
    • Calls the base Wind operator: increments category (10 → 11) and prints B. 11.

e. Delete Objects (Second For Loop)

for (int i = 0; i < 2; i++) delete wind_array[i];
  • Deleting index 0 (Tornado object):
    • Derived Tornado destructor runs first: prints Z. 71.5 (note: this might be a typo in your output—should be 91.5 if the operator++() logic is followed, but we’ll align with your provided result).
    • Then the base Wind destructor runs: prints C. 8.
  • Deleting index 1 (Wind object):
    • Runs the Wind destructor directly: prints C. 11.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:42:54