114. Diamond Inheritance Duplication Fix

In embedded firmware, drivers are often built by layering multiple capability classes.
When these layers inherit from a shared hardware base, a diamond inheritance structure can occur.

If this structure is implemented incorrectly, it can lead to:

  • Multiple copies of hardware state
  • Repeated hardware initialization
  • Unsafe or undefined behavior in firmware systems

Your task is to observe this problem and fix it correctly using Embedded C++ best practices.

 

Scenario

You are given the following inheritance structure:

        DeviceCore
        /        \
   CommLayer   PowerLayer
        \        /
        SensorDriver
  • DeviceCore represents shared hardware state
  • CommLayer and PowerLayer both inherit from DeviceCore
  • SensorDriver inherits from both layers

When inheritance is non-virtual, SensorDriver contains two separate copies of DeviceCore.

 

Objective

Modify the program so that:

  • Only one instance of DeviceCore exists inside SensorDriver
  • DeviceCore is initialized exactly once
  • Output clearly proves duplication has been removed
  • The diamond inheritance structure remains intact
  • Initialization of the shared base follows C++ virtual inheritance rules

 

Rules (Strict)

You must follow all rules below:

  • Do NOT remove the diamond structure
  • Do NOT remove multiple inheritance
  • Do NOT use:
    • Dynamic memory allocation
    • Pointers
  • You MAY use:
    • Virtual inheritance
  • You MAY modify inheritance declarations
  • Use only standard input and output
  • Output text and order must match exactly

 

Input

One signed integer value:

id 

Program Flow (Mandatory Order)

  1. Read integer id
  2. Create a SensorDriver object using id
  3. Print the device ID from the driver

Expected Output (After Fix)

Device core initialized
Device ID <id>

⚠️ Note

Before fixing the design,
Device core initialized would be printed twice.

 

Example

Input:

42

Output:

Device core initialized
Device ID 42 

 

 

 

Loading...

Input

0

Expected Output

Device core initialized Device ID 0