112. Multilevel Driver Initialization

In embedded firmware, drivers are commonly layered to separate responsibilities and ensure correct hardware bring-up.
A typical structure looks like:

  • A core driver that initializes shared system resources
  • A communication driver that sets up protocol-level behavior
  • A device-specific driver that applies final configuration

In C++, multilevel inheritance guarantees a strict constructor execution order, which is critical in such designs.

Your task is to implement and verify constructor execution order in a three-level inheritance hierarchy that mimics a real firmware driver stack.

 

Scenario

You must implement the following class hierarchy:

CoreDriver
   ↓
CommDriver
   ↓
SpiDriver

Each class represents a firmware layer and performs initialization in its constructor.

 

Requirements

Step 1: CoreDriver

  • Define a class CoreDriver
  • Provide a constructor that prints exactly:

    Core driver initialized
    

Step 2: CommDriver

  • Define a class CommDriver
  • Publicly inherit from CoreDriver
  • Provide a constructor that prints exactly:

    Comm driver initialized
    

Step 3: SpiDriver

  • Define a class SpiDriver
  • Publicly inherit from CommDriver
  • Constructor requirements:
    • Accepts one integer parameter speed
    • Prints exactly:

      SPI driver initialized
      
    • Stores the speed value internally
  • Provide a member function that prints:

    SPI speed <speed>
    

Input

  • One integer value:

    speed 

 

Program Flow (Mandatory Order)

  1. Read integer speed from standard input
  2. Create a SpiDriver object using speed
  3. Constructor execution order must be:
    • CoreDriver
    • CommDriver
    • SpiDriver
  4. Call the function that prints the SPI speed

 

Example Input

8

Example Output

Core driver initialized
Comm driver initialized
SPI driver initialized
SPI speed 8 

 

Constraints (Strict)

  • Use multilevel inheritance only
  • Inheritance must be public
  • Do NOT use:
    • Virtual functions
    • Dynamic memory allocation (new, malloc)
    • Composition
  • Output text and order must match exactly
  • Use only standard input and output
  • No additional logging or formatting

 

 

 

Loading...

Input

8

Expected Output

Core driver initialized Comm driver initialized SPI driver initialized SPI speed 8