120. Override Correctness Pitfalls

In embedded firmware, driver interfaces often define virtual functions that derived drivers must override to provide device-specific behavior.

Two common and dangerous mistakes occur in real systems:

  • A derived driver accidentally changes the function signature and forgets to use override, causing a silent runtime bug where the base implementation executes.
  • A derived driver uses override with an incorrect function signature, causing a compile-time error.

Both situations are critical to understand and avoid in production firmware.

In this problem, you are given two drivers with different override mistakes:

  • SPI driver demonstrates a silent override failure (signature mismatch without override)
  • I2C driver demonstrates a compile-time detected override mismatch (incorrect signature with override)

Your task is to fix the program so that:

  • Both drivers correctly override the base virtual function
  • The program compiles successfully
  • Runtime polymorphism works correctly for both drivers

This problem enforces professional override discipline required in production embedded C++.

Input / Program Flow:

One integer value is read from standard input.

Driver selection rules:

  • If input is 0 → use the SPI driver
  • If input is 1 → use the I2C driver

Program flow:

  • Read one integer value
  • Create the selected driver object
  • Store its address in a base-class pointer
  • Call the virtual function through the base-class pointer

Output:

If input is 0, the program must print:

SPI driver processing

If input is 1, the program must print:

I2C driver processing

Output requirements:

  • Exactly one line of output
  • Exact text and spacing must match
  • Output must be produced via a polymorphic call

Constraints:

  • Language standard: C++11
  • Do NOT change function names
  • Do NOT change how the base-class pointer is used
  • Do NOT add new virtual functions
  • Do NOT use dynamic memory allocation
  • You MUST use the override keyword correctly
  • You MAY correct function signatures in derived classes
  • Output must match exactly

 

 

 

Loading...

Input

0

Expected Output

SPI driver processing