117. Polymorphic Driver Shutdown

You are given a firmware-style driver framework with a common base driver interface and two derived drivers:

  • SPI driver
  • I2C driver

A driver object is created at runtime based on input and destroyed through a base-class pointer.

The program compiles and runs, but the shutdown sequence is incorrect:
part of the driver-specific cleanup logic is not executed.

Your task is to correct the class design so that the shutdown sequence is complete and correct for all driver types.

 

Input / Program Flow:

One integer value is read from standard input.

Input meaning:

  • 0 → Create SPI driver
  • 1 → Create I2C driver

Program flow:

  • Read one integer value
  • Create the corresponding driver object
  • Store its address in a base-class pointer
  • Destroy the object through the base-class pointer

Output:

If input is 0, the program must print exactly:

SPI driver shutdown
Base driver shutdown

If input is 1, the program must print exactly:

I2C driver shutdown
Base driver shutdown

Output requirements:

  • Exactly two lines of output
  • Exact text and order must match
  • Output must be produced during object destruction

 

Constraints:

  • Language standard: C++11
  • Do NOT change function names or signatures
  • Do NOT change how the object is destroyed
  • Do NOT use STL containers
  • You MAY modify class design
  • Output must match exactly

 

 

 

Loading...

Input

0

Expected Output

SPI driver shutdown Base driver shutdown