In embedded firmware, framework or middleware code often borrows a driver interface (via a base-class pointer or reference) without owning the driver object itself.
If object lifetime is not managed carefully, the framework may call into a driver after the driver object has already gone out of scope, resulting in undefined behavior.
You are given a firmware-style C++ program where:
Although the program compiles and may appear to run correctly, the design is unsafe and can result in a dangling pointer.
Your task is to correct the program design so that:
Input / Program Flow
Driver selection rules:
0 → use the SPI driver1 → use the I2C driverProgram flow:
Output
If the input is 0, output:
SPI transfer completed
If the input is 1, output:
I2C transfer completed
Output Requirements
Constraints
new, delete)
Input
0
Expected Output
SPI transfer completed