116. Slicing Breaks Polymorphism

In embedded firmware systems, hardware drivers are often accessed through a common interface so that different peripherals can be managed uniformly.
This relies on runtime polymorphism, where derived driver implementations override virtual functions defined in a base driver interface.

However, passing polymorphic objects by value causes object slicing, which removes the derived portion of the object and breaks runtime dispatch.

Your task is to correct the given program so that runtime polymorphism is preserved, and the correct derived driver behavior executes when processed through the base interface.

You must fix the object slicing issue without redesigning the driver class hierarchy.

 

Input / Program Flow

  • One integer value is read from standard input.

Driver selection rules:

  • If the input value is 0, use the SPI driver
  • If the input value is 1, use the I2C driver

Program flow:

  1. Read one integer value
  2. Create the corresponding derived driver object
  3. Pass the driver object to a processing function
  4. The processing function calls the driver function through the base interface

Output

  • If input is 0, output:

    SPI driver running
  • If input is 1, output:

    I2C driver running

Output Requirements

  • Exactly one line of output
  • Exact text and spacing must match
  • Output must originate from the driver function call inside the processing function

 

Constraints

  • Language standard: C++11
  • Do NOT modify driver class data members
  • Do NOT remove polymorphism
  • Do NOT use dynamic memory allocation (new, delete)
  • Do NOT use STL containers
  • You MAY change how objects are passed to functions
  • Output must match exactly

 

 

 

Loading...

Input

0

Expected Output

SPI driver running