115. Runtime Driver Selection

You are given a firmware-style driver design where different hardware drivers must be selected at runtime but accessed through a common base-class pointer.

The program currently compiles and runs, but it always executes the base driver behavior, regardless of which concrete driver is selected.

Your task is to correct the class design so that the appropriate derived driver behavior executes at runtime, while still invoking the function through a base-class pointer.

The final solution must demonstrate true runtime polymorphism using Embedded C++ principles.

 

Input / Program Flow

  • One integer value is read from standard input.

Driver selection rules:

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

Program flow:

  1. Read one integer value
  2. Create the appropriate derived driver object based on input
  3. Store its address in a base-class pointer
  4. Call a function through the base-class pointer

 

Output

  • If input is 0, the program must print:

    SPI driver started
    
  • If input is 1, the program must print:

    I2C driver started
    

Output Requirements

  • Exactly one line of output
  • Exact text and spacing
  • Output must be produced only via the base-class pointer call

 

Constraints

  • Language standard: C++11
  • Do NOT change function names or function signatures
  • Do NOT change how the base-class pointer is used
  • Do NOT use dynamic memory allocation
  • Do NOT use STL containers
  • You MAY modify the class design to enable correct runtime behavior

 

 

 

Loading...

Input

0

Expected Output

SPI driver started