In embedded firmware, multiple peripheral drivers often require the same mandatory initialization steps, such as:
This logic must be written once and reused by multiple derived drivers to avoid duplication and inconsistency.
Your task is to model this design using inheritance.
Step 1: Base Driver
Create a BaseDriver class that:
initBase()When called, prints exactly:
Base driver init start
Base driver init complete
This function represents shared hardware initialization that all drivers must perform.
Step 2: Derived Drivers
Create two derived driver classes:
SpiDriverI2cDriverEach derived class must:
BaseDriverinitSpi() for SpiDriverinitI2c() for I2cDriverinitBase()Required output messages:
SPI driver initialized
I2C driver initialized
Step 3: main()
In main():
modemode == 0:SpiDriverinitSpi()mode == 1:I2cDriverinitI2c()This selection simulates choosing different peripherals at runtime while reusing the same base initialization logic.
Input
One integer mode
0 → SPI driver1 → I2C driverProgram Flow (Mandatory Order)
mode
Example 1
Input:
0
Output:
Base driver init start
Base driver init complete
SPI driver initialized
Example 2
Input:
1
Output:
Base driver init start
Base driver init complete
I2C driver initialized
Constraints (Strict)
BaseDriverinitBase() must be implemented only once in BaseDriverinitBase()new, malloc)
Input
0
Expected Output
Base driver init start Base driver init complete SPI driver initialized